[英]Error while exporting datatable to Excel
I am using below code to export excel using Microsoft.Office.Interop.Excel. 我正在使用下面的代码使用Microsoft.Office.Interop.Excel导出Excel。 [Ms office 2003].
[Office 2003女士]。 I need to export around 150000 rows can have maximum 300 columns.
我需要导出大约150000行,最多可以包含300列。
but getting error on Get_Range. 但是在Get_Range上出现错误。 [ Working fine for 50000 rows and 40 columns]
[适用于50000行40列的罚款]
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);
}
}
Getting error on below line of code 在下面的代码行出现错误
[{"Exception from HRESULT: 0x800A03EC"}] excelSheet.get_Range(excelRange, Type.Missing).Value2 = rawData;
[{“来自HRESULT的异常:0x800A03EC”}] excelSheet.get_Range(excelRange,Type.Missing).Value2 = rawData;
IS Get_Range()
has some limitation for rows/Columns? Get_Range()
对行/列有一些限制吗?
As I know for MS Office 2003 column's limit is 256, not sure about rows limt. 据我所知,对于MS Office 2003,列的限制为256,不确定行是否受限。
Thanks 谢谢
Excel 2003 can have a maximum of 65536 rows in a single worksheet ( specification ) Excel 2003在一个工作表( 规范 )中最多可以有65536行
Excel 2007 and newer can have 1048576 rows (specification: Excel 2007 , Excel 2010 and Excel 2013 ) Excel 2007及更高版本可以具有1048576行(规范: Excel 2007 , Excel 2010和Excel 2013 )
Excel 2003行限制为65,536- http://support.microsoft.com/kb/120596
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.