简体   繁体   English

将数据表保存到C#中具有100k行的大型Excel文件中

[英]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): 根据我的怀疑,这使我对您的问题可能有几种可能性(首先是我认为最有可能的问题):

  1. 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. 另外,请记下导致错误的特定记录,以便您可以单步浏览一条记录并查看错误发生的位置。

  2. 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.

相关问题 使用c#删除大量(&gt; 100K)文件,同时保持Web应用程序的性能? - Delete a large number (>100K) of files with c# whilst maintaining performance in a web application? 使用C#将超过100k的数据SQL Server导出到Excel - Export more than 100k data sql server to excel using C# 从C#的数据表中筛选100k行的值大约需要15分钟 - Filtering a value from Data Table in C# for 100k rows interatively is taking around 15 minutes 有效执行100K更新语句-C#和SQL Server - Efficiently execute 100K update statements - C# & Sql Server Windows Form(C#)-将大型Excel文件导出到数据表中 - Windows Form(C#)-export large Excel file into datatable C#-将上传的Excel文件保存到MemoryStream,然后使用它填充DataTable - C# - Save uploaded Excel file to MemoryStream, then use it to fill a DataTable 如何读取包含超过 200,000 行的大型 excel 文件并将该数据加载到 C# 中的数据表中 - How to read large excel files containing more that 200,000 rows and load that data in datatable in C# 通过C#将大型数据表导出到Excel 2003,而无需使用Excel COM - Export large datatable to excel 2003 by c# without excel COM 如何从DataTable C#中获取每隔100行 - How to Fetch Every Next 100 rows from DataTable C# 在弹性搜索中插入 100k 条记录 - Insert 100k records in the elastic search
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM