Microsoft.Office.Interop.Excel really slow

前端 未结 8 1513
执笔经年
执笔经年 2020-11-27 13:43

I am exporting a 1200 X 800 matrix (indexMatrix) to a excel file using the standard Microsoft.Office.Interop.Excel. The app works, just that it is really really really slow(

相关标签:
8条回答
  • 2020-11-27 14:15

    ClosedXML is a miracle, it's a great deal faster and easier to use.

    var workbook = new XLWorkbook();//create the new book
    
    var worksheet = workbook.Worksheets.Add("Computer Install");// Add a sheet
    worksheet.Cell(1,1).Value = "PC Name";// (Row, column) write to a cell
    
    workbook.SaveAs(@"LIC documents.xlsx");// Save the book
    

    You install using a nu Get package. https://www.nuget.org/packages/ClosedXML

    0 讨论(0)
  • 2020-11-27 14:17

    Turn off ScreenUpdating before writing any data, Application.ScreenUpdating = FALSE then turn on at end of code = TRUE

    0 讨论(0)
  • 2020-11-27 14:27

    I had similar problems when reading an extremely large excel file and it took over 2 hours using interop.

    I tried using ClosedXml and the process took less than 10 seconds. ClosedXml

    // To loop 
    Sheet.Row(y).Cell(x).Value
    

    Also keep in mind interop will not work on your server unless you have excel installed. ClosedXml does not need excel installed.

    0 讨论(0)
  • 2020-11-27 14:28

    Use Value2 to make it fast; Show excel before filling data

    0 讨论(0)
  • 2020-11-27 14:28

    I am answering a little bit late sorry. I was working on my project and we had to use interop excel. And our data was too big, which was taking more than 1 minute with interop excel. We tried something else which is copy the all content of datagridview to clipboard, open an excel worksheet using interop excel, and paste the content to excel. It takes less than 1 second and exports our data perfectly.

    DataGridView to string:

        var newline = System.Environment.NewLine;
        var tab = "\t";
        var clipboard_string = "";
    
        foreach (DataGridViewRow row in dgProjeler.Rows)
        {
            for (int i = 0; i < row.Cells.Count; i++)
            {
                if (i == (row.Cells.Count - 1))
                    clipboard_string += row.Cells[i].Value + newline;
                else
                    clipboard_string += row.Cells[i].Value + tab;
            }
        }
    

    and copy the string to clipboard

            Clipboard.SetText(clipboard_string);
    

    And open a worksheet, paste the content.

            Excel.Application app = new Excel.Application();
            app.Visible = true;
            Excel.Workbook wb = app.Workbooks.Add(1);
            Excel.Worksheet ws = (Excel.Worksheet)wb.Worksheets[1];
            // changing the name of active sheet
            ws.Name = "Exported from gridview";
    
            ws.Rows.HorizontalAlignment = HorizontalAlignment.Center;
            app.ActiveWindow.Activate();
    
    
            ws.Activate();
            ws.Paste();
    
            ws.Cells.EntireColumn.AutoFit();
    

    It works perfectly on me, I hope it helps people who still couldn't find the solution.

    0 讨论(0)
  • 2020-11-27 14:30

    Excel interop is never going to be fast. You're basically remote-controlling an instance of the Excel application. You might have more success by creating a CSV file and then using Excel interop to convert this to a .xls or .xlsx file

    0 讨论(0)
提交回复
热议问题