将DataTable导出到Excel文件(.xlsx)的最有效方法

时间:2017-02-24 10:57:42

标签: c# wpf excel visual-studio-2015 datatable

我想知道在速度方面将DataTable或DataSet导出到.xlsx文件的最有效方法是什么。

我有200K行的表,循环没用,所以我想像批量导出那样做。

任何易于实施的问题都能回答我的问题?

解: 如果有人需要,我最终通过这种方式使用OpenXml。它在大约1分钟内输出100k:

    private void ExportDataSet(DataSet ds, string destination)
    {
        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();

            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);
                }

            }
        }
    }

2 个答案:

答案 0 :(得分:1)

你必须循环记录,这就是为什么它太慢了。

尝试这样的事情。

var lines = new List<string>();

string[] columnNames = dataTable.Columns.Cast<DataColumn>().
                                  Select(column => column.ColumnName).
                                  ToArray();

var header = string.Join(",", columnNames);
lines.Add(header);

var valueLines = dataTable.AsEnumerable()
                   .Select(row => string.Join(",", row.ItemArray));            
lines.AddRange(valueLines);

File.WriteAllLines("excel.csv",lines);

或者,将DataTable转换为Excel文件。

XLWorkbook wb = new XLWorkbook();
DataTable dt = GetDataTableOrWhatever();
wb.Worksheets.Add(dt,"WorksheetName");

答案 1 :(得分:-1)

我个人更喜欢Syncfusion的Excel库,因为它非常直观,文档齐全,并且免费提供社区许可。

您可以在此处下载:XlsIO Product Page

可以在此处找到文档:XlsIO Documentation

如果您申请免费社区许可,则可以免费使用。

以下是将DataTable写入xlsx文件的代码示例。我不认为你的表的大小应该是一个问题,但我不是100%肯定,我用这个库写的最大的文件有大约90k行,60列,我没有任何问题。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using Syncfusion.XlsIO;
namespace ConsoleApp3
{
    class Program
    {
        static void Main(string[] args)
        {
            DataTable Table = new DataTable();
            Table.Columns.Add("Column1");
            Table.Columns.Add("Column2");
            Table.Columns.Add("Column3");
            Table.Rows.Add("Item1", "Item2", "Item3");

            ExcelEngine ExcelEngineObject = new Syncfusion.XlsIO.ExcelEngine();
            IApplication Application = ExcelEngineObject.Excel;
            Application.DefaultVersion = ExcelVersion.Excel2013;
            IWorkbook Workbook = Application.Workbooks.Create(1);
            IWorksheet Worksheet = Workbook.Worksheets[0];
            Worksheet.ImportDataTable(Table, true, 1, 1);
            Workbook.SaveAs("YourExcelFile.xlsx");
            Workbook.Close();
            ExcelEngineObject.Dispose();

        }
    }
}