2013-03-20 121 views
0

我想导出一个数据表在我的应用程序excel。但是我的数据表包含超过2万条记录,而且我只能向excel写入65536条记录。导出数据表与超过二十万行excel

搜索对谷歌有很多之后,才知道,只有65536条记录可以被写入早期Excel版本为2007年相比

如果我们可以写超过65536条记录练成请让我知道

我不使用Microsoft Office的lib在我的应用我使用的是类我自己的这写 表脱颖而出

这里是我的excel类

公共类ExcelWrite { private stream 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(); 
     } 

    } 
} 
+1

如果您的行数很高,请考虑使用数据库。 Excel可以连接到sql server作为数据源 – nunespascal 2013-03-20 07:30:06

+0

您可以发布您正在使用的代码吗? – Dutts 2013-03-20 07:30:19

+0

该限制适用于较早版本的Excel(2003和更早版本)。考虑写入多张或使用较新的版本。 – NoviceProgrammer 2013-03-20 07:56:12

回答

3

这些是官方的限制。 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 - 1058576行
http://office.microsoft.com/en-gb/excel-help/excel-specifications-and-limits-HP010342495.aspx

+0

可以提供我的代码来创建.xlsx文件..没有使用任何第三党的DLL的 – Nagaraj 2013-03-20 12:13:21

+0

对不起@Cherry,我不能 - 我们使用了第三方工具,所以我们不必去自己写代码的麻烦:) – Richard 2013-03-20 12:15:05

+0

Dats ok @Stony thanx您的建议。 – Nagaraj 2013-03-20 12:17:15

0

请尝试将格式从“.xls”转换为“.xlsx” FileStream stream = File.Open(“C:/ Application /“+ filename +”.xlsx“,FileMode.Create);