【问题标题】:Formatting issue with Importing data from a DataSet to an Excel Sheet将数据从数据集导入 Excel 工作表的格式问题
【发布时间】:2014-09-10 13:51:16
【问题描述】:

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

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 并且超过 50000 rows

我认为问题在于将列列表声明为字符串,如下所示

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

cell.DataType = DocumentFormat.OpenXml.Spreadsheet.CellValues.String;

但不知道如何处理。

【问题讨论】:

    标签: c# excel dataset openxml


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2014-02-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-27
      相关资源
      最近更新 更多