Spire.XLS is a professional Excel API that enables developers to create, manage, manipulate, convert and print Excel worksheets. Get free and professional technical support for Spire.XLS for .NET, Java, Android, C++, Python.

Mon Aug 14, 2023 11:56 pm

Good Afternoon,

I was searching for an answer to my current problem on the forums and found similar threads. My issue is somewhat related & I am hoping there might be a solution.

We currently have 2 ways of creating worksheets... we create a 2D object array and use the "InsertArray()" method. We have used this for a lot of our current reporting and with smaller reports it works well.

However, on our larger reports, the performance was not good. A report that previously took ~8 minutes to run with the Excel Interop, not takes closer to an hour. I have read your forums and read that the "InsertDataTable()" method is supposed to be faster. I have attempted to create logic that uses this method, but I have run into an issue where the formulae in my worksheet are not being calculated. This was an issue previously with the "InsertArray()" method, but a call to "workbook.CalculateAllValue()" fixed the problem... it does not work with this new data table approach.

I have included the method we use below, but I cannot provide an example of the worksheet as it has sensitive information.

Code: Select all
public void AddWorksheetFromDataTable(System.Data.DataTable table)
        {
            _currentWorksheet = _workBook.Worksheets.Add(table.TableName);

            _currentWorksheet.InsertDataTable(table, IncludeHeadings, 1, 1, true);

            if (!string.IsNullOrWhiteSpace(Format))
            {
                SetFormat(1, 1, table.Rows.Count, table.Columns.Count, Format);
            }

            if (!string.IsNullOrWhiteSpace(Background))
            {
                _currentWorksheet.GridLinesVisible = false;
                CellStyle style = _workBook.Styles.Add("NewStyle");
                style.Color = HexToColor(Background);
                _currentWorksheet.ApplyStyle(style);
            }
            if (Autosize)
            {
                _currentWorksheet.AllocatedRange.AutoFitColumns();
                _currentWorksheet.AllocatedRange.AutoFitRows();
            }

            if (Exists("Sheet1") && !_isAssemblyInfoWritten)
            {//this means we now have the newly added worksheet and the classic 'Sheet1' which we will use!
                string strVersion = Assembly.GetExecutingAssembly().GetName().Version.ToString();
                object[,] dataSheet1 = new object[2, 2];
                dataSheet1[0, 0] = $"Model Version:";
                dataSheet1[0, 1] = strVersion;
                dataSheet1[1, 0] = $"Executed:";
                dataSheet1[1, 1] = $"{DateTime.Now:g}";
                OverwriteWorksheet("Sheet1", dataSheet1);

                _currentWorksheet.Activate();//reselect current
                _isAssemblyInfoWritten = true;
            }

            MoveTabToSpecifiedIndexByWorksheetName(_currentWorksheet.Name, 1, true);
            _currentWorksheet = _workBook.Worksheets[table.TableName];
            _currentWorksheet.CalculateAllValue();
        }


We also call the "workbook.CalculateAllValue()" before saving the document, but when I open the Excel... the formulae have not been calculated. I have to Enable the document for editing, click into the cell formula & hit enter before it is calculated. What could the difference be between these 2 methods you provide?

Thank you!

Bandonia
 
Posts: 34
Joined: Tue Nov 16, 2021 9:40 pm

Tue Aug 15, 2023 8:09 am

Hello,

Thank you for your inquiry.
Based on the information you provided, I modified some of your code to make it runnable since I couldn't directly test the code you provided. After conducting preliminary testing, but I did not reproduce your issue, and I have attached the code and result file below. You can make adjustments to your project using the code provided in my attachment. If there are still issues with the test results, to help us investigate further, please provide the following information for our reference. You could attach them here or send them to us via email (support@e-iceblue.com). Thanks in advance.
1)The dataTable you filled in after referring to the dataTable format of the code we provided
2) Your Simplified input file and complete executable code.
3) Your application type, such as Console app (. Net Framework 4.5).

Futhermore, The main difference between InsertDataTable() and InsertArray() lies in their different data sources. The former inserts the entire DataTable object's data into a specified range of cells in an Excel worksheet, while the latter inserts the data from a two-dimensional array into a specified range of cells in an Excel worksheet.

Sincerely,
Wenly
E-iceblue support team
Attachments
attachment.zip
(19.39 KiB) Downloaded 392 times
User avatar

Wenly.Zhang
 
Posts: 149
Joined: Tue May 16, 2023 2:19 am

Tue Aug 15, 2023 10:28 pm

I have resolved this issue, calling the CalculateAllValues() method on the workbook didn't have any effect. I needed to call the CalculateAllValues() on the newly created worksheet right after I inserted the datatable to it.

Bandonia
 
Posts: 34
Joined: Tue Nov 16, 2021 9:40 pm

Wed Aug 16, 2023 1:23 am

Hello,

Thank you for your feedback.
Glad to hear that your issue has been resolved. If you encounter other issues related to our products in the future, please feel free to contact us.
Have a nice day!

Sincerely,
Wenly
E-iceblue support team
User avatar

Wenly.Zhang
 
Posts: 149
Joined: Tue May 16, 2023 2:19 am

Return to Spire.XLS