DocumentFormat.Openxml将数据表添加到工作表而不循环到行

时间:2015-08-25 14:24:10

标签: c# asp.net datatable export-to-excel openxml-sdk

有没有办法在不循环DataRows的情况下将数据表直接插入工作表。我有一个非常大的数据表,行数超过20万。因此,在执行导出Excel工作表时,循环遍历每一行需要时间。有人可以建议使用

的代码片段
for i in range(1,7)
if set(u) & set(s **1to6** ) 
    u = u - s **1to6** 
    print ( s **1to6** )

1 个答案:

答案 0 :(得分:1)

我必须做同样的事情。我需要传递数据表并让它创建电子表格。这对我有用。

http://lateral8.com/articles/2010/3/5/openxml-sdk-20-export-a-datatable-to-excel.aspx

using System.Data;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Spreadsheet;

public void ExportDataTable(
     DataTable table,
     string exportFile)
    {
        //create the empty spreadsheet template and save the file
        //using the class generated by the Productivity tool
        ExcelDocument excelDocument = new ExcelDocument();
        excelDocument.CreatePackage(exportFile);

        //populate the data into the spreadsheet
        using (SpreadsheetDocument spreadsheet =
            SpreadsheetDocument.Open(exportFile, true))
        {
            WorkbookPart workbook = spreadsheet.WorkbookPart;
            //create a reference to Sheet1
            WorksheetPart worksheet = workbook.WorksheetParts.Last();
            SheetData data = worksheet.Worksheet.GetFirstChild<SheetData>();

            //add column names to the first row
            Row header = new Row();
            header.RowIndex = (UInt32)1;

            foreach (DataColumn column in table.Columns)
            {
                Cell headerCell = createTextCell(
                    table.Columns.IndexOf(column) + 1,
                    1,
                    column.ColumnName);

                header.AppendChild(headerCell);
            }
            data.AppendChild(header);

            //loop through each data row
            DataRow contentRow;
            for (int i = 0; i < table.Rows.Count; i++)
            {
                contentRow = table.Rows[i];
                data.AppendChild(createContentRow(contentRow, i + 2));
            }
        }
    }

#region工作簿方法

    /// <summary>
    /// Gets the Excel column name based on a supplied index number.
    /// </summary>
    /// <returns>1 = A, 2 = B... 27 = AA, etc.</returns>
    private string getColumnName(int columnIndex)
    {
        int dividend = columnIndex;
        string columnName = String.Empty;
        int modifier;

        while (dividend > 0)
        {
            modifier = (dividend - 1) % 26;
            columnName =
                Convert.ToChar(65 + modifier).ToString() + columnName;
            dividend = (int)((dividend - modifier) / 26);
        }

        return columnName;
    }

    private Cell createTextCell(
        int columnIndex,
        int rowIndex,
        object cellValue)
    {
        Cell cell = new Cell();

        cell.DataType = CellValues.InlineString;
        cell.CellReference = getColumnName(columnIndex) + rowIndex;

        InlineString inlineString = new InlineString();
        Text t = new Text();

        t.Text = cellValue.ToString();
        inlineString.AppendChild(t);
        cell.AppendChild(inlineString);

        return cell;
    }

    private Row createContentRow(
        DataRow dataRow,
        int rowIndex)
    {
        Row row = new Row

        {
            RowIndex = (UInt32)rowIndex
        };

        for (int i = 0; i < dataRow.Table.Columns.Count; i++)
        {
            Cell dataCell = createTextCell(i + 1, rowIndex, dataRow[i]);
            row.AppendChild(dataCell);
        }
        return row;
    }

    #endregion