任务
将数据从excel
导入到DataTable
问题
不包含任何数据的单元格将被跳过,并且该行中具有数据的下一个单元格将用作空列的值。例如]
A1
为空A2的值为Tom
,然后在导入数据A1
时获得A2的值,而A2保持为空为了清楚起见,我在下面提供了一些屏幕截图
这是excel数据
这是从excel导入数据后的数据表
代码
public class ImportExcelOpenXml { public static DataTable Fill_dataTable(string fileName) { DataTable dt = new DataTable(); using (SpreadsheetDocument spreadSheetDocument = SpreadsheetDocument.Open(fileName, false)) { WorkbookPart workbookPart = spreadSheetDocument.WorkbookPart; IEnumerable<Sheet> sheets = spreadSheetDocument.WorkbookPart.Workbook.GetFirstChild<Sheets>().Elements<Sheet>(); string relationshipId = sheets.First().Id.Value; WorksheetPart worksheetPart = (WorksheetPart)spreadSheetDocument.WorkbookPart.GetPartById(relationshipId); Worksheet workSheet = worksheetPart.Worksheet; SheetData sheetData = workSheet.GetFirstChild<SheetData>(); IEnumerable<Row> rows = sheetData.Descendants<Row>(); foreach (Cell cell in rows.ElementAt(0)) { dt.Columns.Add(GetCellValue(spreadSheetDocument, cell)); } foreach (Row row in rows) //this will also include your header row... { DataRow tempRow = dt.NewRow(); for (int i = 0; i < row.Descendants<Cell>().Count(); i++) { tempRow[i] = GetCellValue(spreadSheetDocument, row.Descendants<Cell>().ElementAt(i)); } dt.Rows.Add(tempRow); } } dt.Rows.RemoveAt(0); //...so i'm taking it out here. return dt; } public static string GetCellValue(SpreadsheetDocument document, Cell cell) { SharedStringTablePart stringTablePart = document.WorkbookPart.SharedStringTablePart; string value = cell.CellValue.InnerXml; if (cell.DataType != null && cell.DataType.Value == CellValues.SharedString) { return stringTablePart.SharedStringTable.ChildElements[Int32.Parse(value)].InnerText; } else { return value; } } }
我的想法
我认为这有问题
public IEnumerable<T> Descendants<T>() where T : OpenXmlElement;
如果我想要使用Descendants
的列数
IEnumerable<Row> rows = sheetData.Descendants<<Row>(); int colCnt = rows.ElementAt(0).Count();
OR
如果我使用Descendants
获取行数,则>
IEnumerable<Row> rows = sheetData.Descendants<<Row>(); int rowCnt = rows.Count();`
在两种情况下,
Descendants
都将跳过空单元格[C0是否有其他选择。
非常感谢您的建议
PS:我也曾想过通过使用像A1,A2
这样的列名来获取单元格的值,但是要做到这一点,我必须获得使用[[ C0]功能。任务将数据从excel导入到DataTable中的问题不包含任何数据的单元格将被跳过,并且该行中具有数据的下一个单元格将用作空列的值。 ...
如果连续的所有单元格中都有数据,那么一切正常。一旦您连续有一个空单元格,事情就会变得一团糟。
为什么发生在第一位
for (int i = 0; i < row.Descendants<Cell>().Count(); i++)
{
Cell cell = row.Descendants<Cell>().ElementAt(i);
int actualCellIndex = CellReferenceToIndex(cell);
tempRow[actualCellIndex] = GetCellValue(spreadSheetDocument, cell);
}
尝试此代码,我做了很少的修改,对我有用。