簡體   English   中英

導出具有超過20萬行的數據表以實現卓越

[英]Export Datatable with more than two hundred thousand rows to excel

我正在嘗試導出數據表以在我的應用程序中表現出色。 但是我的數據表包含超過22萬條記錄,而我只能將65536條記錄寫入Excel。

在Google上搜索了很多之后,才知道,對於2007年之前的版本,只能將65536條記錄寫入excel。

如果我們可以寫超過65536條記錄以表現出色,請告訴我

我沒有在我的應用程序中使用Microsoft Office lib我正在使用自己的類,該類將表格寫入excel

這是我的excel課程

公共類ExcelWrite {私有Stream流; 私人BinaryWriter作家;

    private ushort[] clBegin = { 0x0809, 8, 0, 0x10, 0, 0 };
    private ushort[] clEnd = { 0x0A, 00 };


    private void WriteUshortArray(ushort[] value)
    {
        for (int i = 0; i < value.Length; i++)
            writer.Write(value[i]);
    }

    /// <summary>
    /// Initializes a new instance of the <see cref="ExcelWriter"/> class.
    /// </summary>
    /// <param name="stream">The stream.</param>
    public ExcelWrite(Stream stream)
    {
        this.stream = stream;
        writer = new BinaryWriter(stream);
    }
    public ExcelWrite()
    {

    }
    /// <summary>
    /// Writes the text cell value.
    /// </summary>
    /// <param name="row">The row.</param>
    /// <param name="col">The col.</param>
    /// <param name="value">The string value.</param>
    public void WriteCell(int row, int col, string value)
    {
        ushort[] clData = { 0x0204, 0, 0, 0, 0, 0 };
        int iLen = value.Length;
        byte[] plainText = Encoding.ASCII.GetBytes(value);
        clData[1] = (ushort)(8 + iLen);
        clData[2] = (ushort)row;
        clData[3] = (ushort)col;
        clData[5] = (ushort)iLen;
        WriteUshortArray(clData);
        writer.Write(plainText);
    }

    /// <summary>
    /// Writes the integer cell value.
    /// </summary>
    /// <param name="row">The row number.</param>
    /// <param name="col">The column number.</param>
    /// <param name="value">The value.</param>
    public void WriteCell(int row, int col, int value)
    {
        ushort[] clData = { 0x027E, 10, 0, 0, 0 };
        clData[2] = (ushort)row;
        clData[3] = (ushort)col;
        WriteUshortArray(clData);
        int iValue = (value << 2) | 2;
        writer.Write(iValue);
    }

    /// <summary>
    /// Writes the double cell value.
    /// </summary>
    /// <param name="row">The row number.</param>
    /// <param name="col">The column number.</param>
    /// <param name="value">The value.</param>
    public void WriteCell(int row, int col, double value)
    {
        ushort[] clData = { 0x0203, 14, 0, 0, 0 };
        clData[2] = (ushort)row;
        clData[3] = (ushort)col;
        WriteUshortArray(clData);
        writer.Write(value);
    }

    /// <summary>
    /// Writes the empty cell.
    /// </summary>
    /// <param name="row">The row number.</param>
    /// <param name="col">The column number.</param>
    public void WriteCell(int row, int col)
    {
        ushort[] clData = { 0x0201, 6, 0, 0, 0x17 };
        clData[2] = (ushort)row;
        clData[3] = (ushort)col;
        WriteUshortArray(clData);
    }

    /// <summary>
    /// Must be called once for creating XLS file header
    /// </summary>
    public void BeginWrite()
    {
        WriteUshortArray(clBegin);
    }

    /// <summary>
    /// Ends the writing operation, but do not close the stream
    /// </summary>
    public void EndWrite()
    {
        WriteUshortArray(clEnd);
        writer.Flush();
    }
    public void exporttoExcel(DataTable table,string filename)
    {
        DataTable dt = new DataTable();
        try
        {

            StringBuilder SB = new StringBuilder();

            dt = table;

            if (dt.Rows.Count > 0)
            {
                FileStream stream = File.Open("C:/Application/" + filename + ".xls", FileMode.Create);

                ExcelWrite excelWriter = new ExcelWrite(stream);
                //AB_MISreports.Util.ExcelWrite excelWriter = new AB_MISreports.Util.ExcelWrite(stream);
                excelWriter.BeginWrite();
                for (int i = 0; i < dt.Columns.Count; i++)
                {
                    excelWriter.WriteCell(0, i, dt.Columns[i].ColumnName.ToString());
                    for (int j = 0; j < dt.Rows.Count; j++)
                    {
                        excelWriter.WriteCell(j + 1, i, dt.Rows[j][i].ToString());
                    }
                }

                excelWriter.EndWrite();
                stream.Close();

                HttpContext.Current.Response.ContentType = "application/vnd.xls";
                HttpContext.Current.Response.AddHeader("content-disposition", "attachment; filename=" + filename + ".xls");
                HttpContext.Current.Response.TransmitFile("C:/Application/" + filename + ".xls");
                HttpContext.Current.Response.End();
                HttpContext.Current.Response.Flush();

            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {

            dt.Dispose();
        }

    }
}

這些是官方限制。 Excel 2007支持.xls和.xlsx格式文件,您確定要編寫較新的文件格式類型嗎? 對於您來說,這可能不是一個可行的選擇,但是我們使用Aspose.Cells來處理電子表格的創建。

Excel 2003 .xls-65,536行
http://office.microsoft.com/en-gb/excel-help/excel-specifications-and-limits-HP005199291.aspx

Excel 2007 .xlsx-1,048,576行
http://office.microsoft.com/en-gb/excel-help/excel-specifications-and-limits-HP010073849.aspx

Excel 2010 .xlsx-1,058,576行
http://office.microsoft.com/en-gb/excel-help/excel-specifications-and-limits-HP010342495.aspx

請嘗試將格式格式從“ .xls”更改為“ .xlsx”。FileStream流= File.Open(“ C:/ Application /” +文件名+“ .xlsx”,FileMode.Create);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM