[英]Save datatable to large excel file having rows 100k in c#
I working with windows application and processing large excel file. 我使用Windows应用程序并处理大型excel文件。 I need to save 100k rows from datatable to excel file.
我需要将10万行从数据表保存到excel文件。
Currently my create excel function only support 65,500 rows only? 目前我的create excel函数仅支持65,500行吗?
But I need to save excel file more than that. 但是我需要保存的不仅仅是excel文件。 Is it possible?
可能吗?
If yes then, Kindly give the source? 如果是的话,请提供来源?
Here is my code 这是我的代码
public static void ExportDataSetToExcel(DataTable dt, int index, string strFilePathName)
{
Console.WriteLine("Creating Output Excel file");
string fileFomat = getExcelFileName(index) + (DateTime.Now.ToString("yyyyMMddTHHmmss"));
Microsoft.Office.Interop.Excel.Application objXL = null;
Microsoft.Office.Interop.Excel.Workbook objWB = null;
try
{
objXL = new Microsoft.Office.Interop.Excel.Application();
objWB = objXL.Workbooks.Add(1);
int sheetcount = 1;
Microsoft.Office.Interop.Excel.Worksheet objSHT = (Microsoft.Office.Interop.Excel.Worksheet)objWB.Sheets.Add();
Microsoft.Office.Interop.Excel.Range cells = objSHT.Cells;
cells.NumberFormat = "@";
//formatRange = objSHT.get_Range("b1",Type.Missing);
//formatRange.EntireRow.Font.Bold = true;
objSHT.Name = "RunOrderSheet";
for (int j = 0; j < dt.Rows.Count; j++)
{
for (int i = 0; i < dt.Columns.Count; i++)
{
//Condition to put column names in 1st row
//Excel work book indexes start from 1,1 and not 0,0
if (j == 0)
{
objSHT.Cells[1, i + 1] = dt.Columns[i].ColumnName.ToString();
}
//Writing down data
objSHT.Cells[j + 2, i + 1] = dt.Rows[j][i].ToString();
}
}
sheetcount++;
objWB.Saved = true;
objWB.SaveCopyAs(strFilePathName.Trim() + fileFomat.Trim() + ".xlsx");
objWB.Close();
objXL.Quit();
Console.WriteLine("Process done");
}
catch (Exception ex)
{
objWB.Saved = true;
objWB.Close();
objXL.Quit();
log.Error(ex.Message);
}
}
Excel and the COM interface with C# absolutely will support more than 65k lines. Excel和带有C#的COM接口绝对将支持超过65k行。 Just to prove that it's possible, run the following code:
只是为了证明有可能,请运行以下代码:
public static void ExportDataSetToExcel()
{
Excel.Application objXL = null;
Excel.Workbook objWB = null;
objXL = new Microsoft.Office.Interop.Excel.Application();
objXL.Visible = true;
objWB = objXL.Workbooks.Add(1);
Excel.Worksheet objSHT = objWB.Sheets.Add();
for (int row = 1; row < 100000; row++)
{
objSHT.Cells[row, 1].Value2 = row;
}
objWB.SaveAs("c:\test\test.xlsx", Excel.XlFileFormat.xlOpenXMLWorkbook);
objWB.Close();
objXL.Quit();
}
Which leads me to several possibilities as to what your issue might be, in order of my suspicions (first being what I think is the most likely): 根据我的怀疑,这使我对您的问题可能有几种可能性(首先是我认为最有可能的问题):
Since you have a try/catch block that is very nicely trapping errors and making sure that anything that happens within your loop while still saving the file, something is tripping the proverbial circuit breaker. 由于您有一个try / catch块,可以很好地捕获错误并确保循环中发生的所有事件,同时仍保存文件,因此某些事情会触发众所周知的断路器。 While I don't see any glaring errors, it could be anything.
虽然我看不到任何明显的错误,但可能有任何错误。 Put a breakpoint within the catch block and see what
ex
contains. 在catch块中放置一个断点,看看
ex
包含什么。 Also, make note of the specific record that is causing the error so you can step through just that one record and see where it occurs. 另外,请记下导致错误的特定记录,以便您可以单步浏览一条记录并查看错误发生的位置。
I noticed you are using SaveCopyAs
rather than SaveAs
. 我注意到您正在使用
SaveCopyAs
而不是SaveAs
。 SaveCopyAs
automatically saves in the exact same format as the original. SaveCopyAs
自动以与原始格式完全相同的格式保存。 I would think your Excel comes up with the default xlsx
, but I certainly can't guarantee that. 我认为您的Excel带有默认的
xlsx
,但是我当然不能保证。 Using SaveAs
with the explicit file format will guarantee it is saved in a format that supports more than 65k lines: . 将
SaveAs
与显式文件格式一起使用将确保将其保存为支持65k行以上的格式:。
objWB.SaveAs("c:\\test\\test.xlsx", Excel.XlFileFormat.xlOpenXMLWorkbook);
Without knowing how large your datatable is, it might simply be a memory issue also that's tripping the try/catch. 在不知道数据表有多大的情况下,它可能仅仅是一个内存问题,也使try / catch失败了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.