从Excel到C#中使用Open XML的DataTable
我正在使用Visual Studio 2008,我需要使用Open XML SDK 2.0从Excel工作表创build一个DataTable
。 我需要使用工作表的第一行的DataTable列创build它,并使用其余的值完成它。
有没有人有一个示例代码或链接,可以帮助我做到这一点?
我认为这应该做你在问什么。 如果你有共享string,我假设你在列标题中做的另一个function就是要处理。 不知道这是完美的,但我希望它有帮助。
static void Main(string[] args) { DataTable dt = new DataTable(); using (SpreadsheetDocument spreadSheetDocument = SpreadsheetDocument.Open(@"..\..\example.xlsx", 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-1)); } dt.Rows.Add(tempRow); } } dt.Rows.RemoveAt(0); //...so i'm taking it out here. } 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; } }
嗨上面的代码工作正常,除了一个变化
replace下面的代码行
tempRow[i] = GetCellValue(spreadSheetDocument, row.Descendants<Cell>().ElementAt(i-1));
同
tempRow[i] = GetCellValue(spreadSheetDocument, row.Descendants<Cell>().ElementAt(i));
如果你使用(i-1),它会抛出一个exception:
specified argument was out of the range of valid values. parameter name index.
Public Shared Function ExcelToDataTable(filename As String) As DataTable Try Dim dt As New DataTable() Using doc As SpreadsheetDocument = SpreadsheetDocument.Open(filename, False) Dim workbookPart As WorkbookPart = doc.WorkbookPart Dim sheets As IEnumerable(Of Sheet) = doc.WorkbookPart.Workbook.GetFirstChild(Of Sheets)().Elements(Of Sheet)() Dim relationshipId As String = sheets.First().Id.Value Dim worksheetPart As WorksheetPart = DirectCast(doc.WorkbookPart.GetPartById(relationshipId), WorksheetPart) Dim workSheet As Worksheet = worksheetPart.Worksheet Dim sheetData As SheetData = workSheet.GetFirstChild(Of SheetData)() Dim rows As IEnumerable(Of Row) = sheetData.Descendants(Of Row)() For Each cell As Cell In rows.ElementAt(0) dt.Columns.Add(GetCellValue(doc, cell)) Next For Each row As Row In rows 'this will also include your header row... Dim tempRow As DataRow = dt.NewRow() For i As Integer = 0 To row.Descendants(Of Cell)().Count() - 1 tempRow(i) = GetCellValue(doc, row.Descendants(Of Cell)().ElementAt(i)) Next dt.Rows.Add(tempRow) Next End Using dt.Rows.RemoveAt(0) Return dt Catch ex As Exception Throw ex End Try End Function Public Shared Function GetCellValue(document As SpreadsheetDocument, cell As Cell) As String Try If IsNothing(cell.CellValue) Then Return "" End If Dim value As String = cell.CellValue.InnerXml If cell.DataType IsNot Nothing AndAlso cell.DataType.Value = CellValues.SharedString Then Dim stringTablePart As SharedStringTablePart = document.WorkbookPart.SharedStringTablePart Return stringTablePart.SharedStringTable.ChildElements(Int32.Parse(value)).InnerText Else Return value End If Catch ex As Exception Return "" End Try End Function
如果行值为空或空值获取值错误的工作。
所有列填充数据,如果它是真实的。 但也许所有的行不