2013-05-09 104 views
1

我正在使用下面的代码使用Microsoft.Office.Interop.Excel导出excel。 [女士办公室2003]。 我需要导出约150000行可以有最多300列。将数据表导出到Excel时出错

但在Get_Range上出现错误。 excelSheet.get_Range(excelRange,类型:

public static void ExportToExcel(DataTable dt, string outputPath) 
    { 
     try 
     { 
      // Create the Excel Application object 
      ApplicationClass excelApp = new ApplicationClass(); 

      // Create a new Excel Workbook 
      Workbook excelWorkbook = excelApp.Workbooks.Add(Type.Missing); 

      int sheetIndex = 0; 

      // Copy each DataTable 


      // Copy the DataTable to an object array 
      object[,] rawData = new object[dt.Rows.Count + 1, dt.Columns.Count]; 

      // Copy the column names to the first row of the object array 
      for (int col = 0; col < dt.Columns.Count; col++) 
      { 
       rawData[0, col] = dt.Columns[col].ColumnName; 
      } 

      // Copy the values to the object array 
      for (int col = 0; col < dt.Columns.Count; col++) 
      { 
       for (int row = 0; row < dt.Rows.Count; row++) 
       { 
        rawData[row + 1, col] = dt.Rows[row].ItemArray[col]; 
       } 
      } 

      // Calculate the final column letter 
      string finalColLetter = string.Empty; 
      string colCharset = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; 
      int colCharsetLen = colCharset.Length; 

      if (dt.Columns.Count > colCharsetLen) 
      { 
       finalColLetter = colCharset.Substring(
        (dt.Columns.Count - 1)/colCharsetLen - 1, 1); 
      } 

      finalColLetter += colCharset.Substring(
        (dt.Columns.Count - 1) % colCharsetLen, 1); 

      // Create a new Sheet 
      Worksheet excelSheet = (Worksheet)excelWorkbook.Sheets.Add(
       excelWorkbook.Sheets.get_Item(++sheetIndex), 
       Type.Missing, 1, XlSheetType.xlWorksheet); 

      excelSheet.Name = "data"; 

      // Fast data export to Excel 
      string excelRange = string.Format("A1:{0}{1}", 
       finalColLetter, dt.Rows.Count + 1); 

      //excelSheet.get_Range(
      excelSheet.get_Range(excelRange, Type.Missing).Value2 = rawData; 

      // Mark the first row as BOLD 
      ((Range)excelSheet.Rows[1, Type.Missing]).Font.Bold = true; 


      // Save and Close the Workbook 
      excelWorkbook.SaveAs("C:\\Dashsrv\\data.Xls", XlFileFormat.xlWorkbookNormal, Type.Missing, 
       Type.Missing, Type.Missing, Type.Missing, XlSaveAsAccessMode.xlExclusive, 
       Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing); 
      excelWorkbook.Close(true, Type.Missing, Type.Missing); 
      excelWorkbook = null; 

      // Release the Application object 
      excelApp.Quit(); 
      excelApp = null; 

      // Collect the unreferenced objects 
      GC.Collect(); 
      GC.WaitForPendingFinalizers(); 
      MessageBox.Show("File created at"); 
     } 
     catch(Exception ex) 
     { 
      MessageBox.Show(ex.Message); 
     } 

    } 

获得下面的代码行

[{ “0x800A03EC从HRESULT异常”}]错误[50000行和列40工作细]。缺少).Value2 = rawData;

IS Get_Range()对行/列有一些限制吗?

正如我所知的MS Office 2003列的限制是256,不知道行限制。

感谢

回答

1

Excel 2003中可以在一个工作表中有一个最大的65536行(specification

Excel 2007中

和更新可以有1048576行(​​规格:Excel 2007Excel 2010Excel 2013

+0

感谢您快速回复Knagis。 – Elliana 2013-05-09 10:53:07

+0

请问您能否以其他方式快速导出excel?与上面相比? – Elliana 2013-05-09 10:59:06

+0

尝试使用OfficeOpenXML而不是Interop。 - http://excelpackage.codeplex.com/wikipage?title=Creating%20an%20Excel%20spreadsheet%20from%20scratch&referringTitle=Home – kgu87 2013-05-09 12:26:22

0

Excel 2003行数限制为65,536 - http://support.microsoft.com/kb/120596

+0

以上代码与MS OFFICE 2003, 兼容是否可以在我的系统上安装任何DLL或SDK,以创建可在2007及更高版本的MS Office中打开的Excel。 我不得不安装FilFormatCoverter http://support.microsoft.com/kb/924074 但需要一些DLL,它可以在我的C#应用​​程序被添加,使得在上面的代码中 excelWorkbook.SaveAs(“C: \\ Dashsrv \\ data.Xls“,XlFileFormat.xlWorkbookNormal,,,,,,,,,); 我可以将XlFileFormat设置为xlOpenXMLWorkbook。 – Elliana 2013-05-15 06:47:51