When I try to apply black color to all cells except first cell my application throws an error of System.OutOfMemoryException,
It creates a huge file.
Here is my code:
//Instantiate a new Workbook.
Workbook workbook = new Workbook();
//Get the first worksheet in the book.
Worksheet worksheet = workbook.Worksheets[0];
//This works good-Creates file with size of 7kb
//worksheet.Cells.ApplyStyle(new Style() { BackgroundColor = Color.Black, Pattern = BackgroundType.Solid, ForegroundColor = Color.Black }, new StyleFlag() { All = true });
//This takes time and throws an exception - SystemOutOfMemory
Range r = worksheet.Cells.CreateRange(1, 1, 1048575, 16383);
r.ApplyStyle(new Style() { BackgroundColor = Color.Black, Pattern = BackgroundType.Solid, ForegroundColor = Color.Black }, new StyleFlag() { All = true });
//Save the excel file.
workbook.Save("D:\\output.xlsx");
Is there any other fast and good way to do this?
Thanks for your quick response.
Actually, When I apply same thing in excel, It creates file of 7kb only. And when open it and save it using Aspose, It creates same 7kb file.
I have attached file here.
Is there any other method to complete this task as fast as possible?
Why Aspose.Cells is creating a huge file while Excel is creating a small file with same formatting and styles?
Thanks,
Kishan
Hi,
Thanks for the template file.
Please see the following sample code that would accomplish your task efficiently. The sample code is processed instantly and the output file’s size is also minimized:
e.g
Sample code:
//Instantiate a new Workbook.
Workbook workbook = new Workbook();
//Get the first worksheet in the book.
Worksheet worksheet = workbook.Worksheets[0];
//First apply style to the whole sheet.
worksheet.Cells.ApplyStyle(new Style() { BackgroundColor = Color.Black, Pattern = BackgroundType.Solid, ForegroundColor = Color.Black }, new StyleFlag() { All = true });
//Now apply formatting to the first column (A) and first row (1) with normal style as per your needs.
worksheet.Cells.ApplyColumnStyle(0, new Style() { BackgroundColor = Color.Empty, Pattern = BackgroundType.None, ForegroundColor = Color.Empty }, new StyleFlag() { All = true });
worksheet.Cells.ApplyRowStyle(0,new Style() { BackgroundColor = Color.Empty, Pattern = BackgroundType.None, ForegroundColor = Color.Empty }, new StyleFlag() { All = true });
workbook.Save(“e:\test2\out1.xlsx”);
Hope, this helps a bit.
Thank you.