将数据从DataSet导入Excel工作表的格式问题

时间:2014-09-10 13:51:16

标签: c# excel dataset openxml

我正在处理DataSet中的一些数据,并尝试使用OpenXml中的C#将其导入excel文件,但我成功地将其导入{{1}我确实有一些DataSetDateTimeinteger类型的列,但我的代码是将所有列导入为纯文本,使我无法按照他们的排序对它们进行排序工作表上的值。我使用以下代码

double

我应该怎样做才能使这段代码正常工作,而不是将这些字段作为自己的类型导入? 当我使用public void ExportDataSet() { try { string fromFormat = "dd/MM/yyyy"; string toFormat = "MM-dd-yyyy"; DateTime newDate = DateTime.ParseExact(DateTime.Today.ToString(fromFormat), fromFormat, null); string filedate = newDate.ToString(toFormat); string destination = @"Z:\Physical DB Data " + filedate + ".xls"; using (var workbook = SpreadsheetDocument.Create(destination, DocumentFormat.OpenXml.SpreadsheetDocumentType.Workbook)) { var workbookPart = workbook.AddWorkbookPart(); workbook.WorkbookPart.Workbook = new DocumentFormat.OpenXml.Spreadsheet.Workbook(); workbook.WorkbookPart.Workbook.Sheets = new DocumentFormat.OpenXml.Spreadsheet.Sheets(); DataSet ds = new DataSet(); ds = GetPhysicalGrainReportAutomation(); foreach (System.Data.DataTable table in ds.Tables) { var sheetPart = workbook.WorkbookPart.AddNewPart<WorksheetPart>(); var sheetData = new DocumentFormat.OpenXml.Spreadsheet.SheetData(); sheetPart.Worksheet = new DocumentFormat.OpenXml.Spreadsheet.Worksheet(sheetData); DocumentFormat.OpenXml.Spreadsheet.Sheets sheets = workbook.WorkbookPart.Workbook.GetFirstChild<DocumentFormat.OpenXml.Spreadsheet.Sheets>(); string relationshipId = workbook.WorkbookPart.GetIdOfPart(sheetPart); uint sheetId = 1; if (sheets.Elements<DocumentFormat.OpenXml.Spreadsheet.Sheet>().Count() > 0) { sheetId = sheets.Elements<DocumentFormat.OpenXml.Spreadsheet.Sheet>().Select(s => s.SheetId.Value).Max() + 1; } DocumentFormat.OpenXml.Spreadsheet.Sheet sheet = new DocumentFormat.OpenXml.Spreadsheet.Sheet() { Id = relationshipId, SheetId = sheetId, Name = table.TableName }; sheets.Append(sheet); DocumentFormat.OpenXml.Spreadsheet.Row headerRow = new DocumentFormat.OpenXml.Spreadsheet.Row(); List<String> columns = new List<string>(); foreach (System.Data.DataColumn column in table.Columns) { columns.Add(column.ColumnName); DocumentFormat.OpenXml.Spreadsheet.Cell cell = new DocumentFormat.OpenXml.Spreadsheet.Cell(); cell.DataType = DocumentFormat.OpenXml.Spreadsheet.CellValues.String; cell.CellValue = new DocumentFormat.OpenXml.Spreadsheet.CellValue(column.ColumnName); headerRow.AppendChild(cell); } sheetData.AppendChild(headerRow); foreach (System.Data.DataRow dsrow in table.Rows) { DocumentFormat.OpenXml.Spreadsheet.Row newRow = new DocumentFormat.OpenXml.Spreadsheet.Row(); foreach (String col in columns) { DocumentFormat.OpenXml.Spreadsheet.Cell cell = new DocumentFormat.OpenXml.Spreadsheet.Cell(); cell.DataType = DocumentFormat.OpenXml.Spreadsheet.CellValues.String; cell.CellValue = new DocumentFormat.OpenXml.Spreadsheet.CellValue(dsrow[col].ToString()); // newRow.AppendChild(cell); } sheetData.AppendChild(newRow); } } } } catch (Exception ex) { MessageBox.Show(ex.Message); } } 循环并导入数据集时,它工作正常,但由于我的interop非常大,大约DataSet且大于15 columns,所以需要很长时间。< / p>

我认为问题在于将List列声明为String,如下所示

50000 rows

List<String> columns = new List<string>();

但不确定如何处理这个问题。

1 个答案:

答案 0 :(得分:0)

我建议您使用NPOI XSSF / HSSF以避免这种情况,因为它有助于将单元格值属性设置为整数,日期时间,公式等,并且它也很快。因此,您只需检查数据集值并根据类型设置单元格属性。 有关更多信息,请访问https://npoi.codeplex.com/SourceControl/latest