Query |
Response |
Next, I need to change some styles of some columns.
With a large number of rows (156k+) and 51 columns, this almost always results in a OutOfMemoryException.
What is the best workflow to set the horizontal alignment or number format of a column?
|
Kindly look into the following link and try applying global styles instead of looping each cell and apply the same style to large range.
|
Also worksheet.UsedRange.AutofitColumns(); takes a long time (6 min.) |
Unfortunately, using AutoFit for the entire used range will reduce the performance. Please refer the following link.
|
worksheet.UsedRange.BorderAround(ExcelLineStyle.Thin, Color.FromArgb(128, 128, 128)); is already running for 1 hour, 3 min. |
Similarly, accessing each cell in the used range (with 156k+ rows and 51 columns) and setting borders will affect the performance. However, we are checking on it currently to give you the better solution. We will update further details on 20th March 2020. |
Query |
Response |
worksheet.UsedRange.BorderAround(ExcelLineStyle.Thin, Color.FromArgb(128, 128, 128)); is already running for 1 hour, 3 min. |
We have tried this code with sample input data (worksheet having sample data in 156k+ rows and 51 columns) and we got it run within 35 seconds. Please find the tried sample below.
The issue could be file specific, we guess. Kindly share us the input file or the data imported in the worksheet, so that we can provide you the prompt solution at the earliest.
|
ExcelEngine excelEngine = new ExcelEngine();
IApplication application = excelEngine.Excel;
IWorkbook workbook = application.Workbooks.Open("SampleData.xlsx");
IWorksheet worksheet = workbook.Worksheets[0];
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
//worksheet.UsedRange.BorderAround(ExcelLineStyle.Thin, Color.FromArgb(128, 128, 128));
// Below method can be called instead of the above commented method, where exception is thrown.
BorderAround(worksheet.UsedRange, ExcelLineStyle.Thick, Color.FromArgb(128, 128, 128));
stopwatch.Stop();
TimeSpan timeSpan = stopwatch.Elapsed;
MessageBox.Show(timeSpan.Seconds.ToString() + " Seconds");
workbook.SaveAs("../../Data/output.xlsx");
workbook.Close();
excelEngine.Dispose(); |
private void BorderAround(IRange range, ExcelLineStyle lineStyle, Color color)
{ int firstRow = range.Row; int lastRow = range.LastRow; int firstColumn = range.Column; int lastColumn = range.LastColumn; IRange range1 = range[firstRow, firstColumn, lastRow, firstColumn];
IRange range2 = range[firstRow, lastColumn, lastRow, lastColumn]; IRange range3 = range[firstRow, firstColumn, firstRow, lastColumn]; IRange range4 = range[lastRow, firstColumn, lastRow, lastColumn]; range1.Borders[ExcelBordersIndex.EdgeLeft].LineStyle = lineStyle;
range1.Borders[ExcelBordersIndex.EdgeLeft].ColorRGB = color; range2.Borders[ExcelBordersIndex.EdgeRight].LineStyle = lineStyle; range2.Borders[ExcelBordersIndex.EdgeRight].ColorRGB = color; range3.Borders[ExcelBordersIndex.EdgeTop].LineStyle = lineStyle; range3.Borders[ExcelBordersIndex.EdgeTop].ColorRGB = color; range4.Borders[ExcelBordersIndex.EdgeBottom].LineStyle = lineStyle; range4.Borders[ExcelBordersIndex.EdgeBottom].ColorRGB = color; } |