Generated file size very large

I am writing data to excel file. It only has two rows the file size is approx. 6 MB in XLSX format, if I choose the XLS format then it come down to 2.5 MB but that is also unacceptable.

Whereas if I just copy paste the data and save it using MS Excel, the size is just 169KB. The dataset only has two rows nothing big, following is the code I am using. It is also causing an exception at server side which gets logged in the event logs

Exception type: HttpException
Exception message: Server cannot append header after HTTP headers have been sent.

dataSheet.Cells.ImportCustomObjects(IncidentListDS, caption, True, 0, 0, IncidentListDS.Length, False, "dd/mm/yyyy", False)

dataSheet.AutoFitColumns()

Dim telePhoneColumnRange As Range = dataSheet.Cells.CreateRange(12, 1, True)

Dim _style As Aspose.Cells.Style = export.Styles(export.Styles.Add())

_style.HorizontalAlignment = TextAlignmentType.Right

Dim _styleFlag As StyleFlag = New StyleFlag()

_styleFlag.All = True

telePhoneColumnRange.ApplyStyle(_style, _styleFlag)

Dim _excelSaveOption As SaveOptions = New XlsSaveOptions()

_excelSaveOption.SaveFormat = SaveFormat.Excel97To2003

export.Save(Response, SESSION_INCIDENT_LIST_DS + ".xls", ContentDisposition.Attachment, _excelSaveOption)

Hi,


Well, you are doing a slightest mistake in your code. Your line of code:

Dim telePhoneColumnRange As Range = dataSheet.Cells.CreateRange(12, 1, True)

This lines show that you are going to create a range based up to last row index in the sheet (i.e. 1048576 rows) for the XLSX file, which is a big range and to apply styles to all the cells in that range will consume more time and the generated file will be bigger.

Please change your code a bit, see my sample code below, it only creates 8kb XLSX file.

Sample code (See the bold line in the following code):

Dim book As New Workbook()
book.Worksheets.Clear()
Dim sheet As Worksheet = book.Worksheets.Add(“Data”)

Dim list As List(Of WeeklyItem) = New List(Of WeeklyItem)()
list.Add(New WeeklyItem(DateTime.Today.AddDays(7)) With {.AtYarnStage = 1, .InWIPStage = 2, .Payment = 3, .Shipment = 4, .Shipment2 = 5})
list.Add(New WeeklyItem(DateTime.Today.AddDays(14)) With {.AtYarnStage = 5, .InWIPStage = 9, .Payment = 7, .Shipment = 2, .Shipment2 = 5})
list.Add(New WeeklyItem(DateTime.Today.AddDays(21)) With {.AtYarnStage = 7, .InWIPStage = 3, .Payment = 3, .Shipment = 8, .Shipment2 = 3})
'I pick a few columns not all.
sheet.Cells.ImportCustomObjects(CType(list, System.Collections.ICollection), New String() { “Date”, “InWIPStage”, “Shipment”, “Payment” }, True, 12, 0, list.Count, True, “dd/mm/yyyy”, False)

book.Worksheets(0).AutoFitColumns()

Dim telePhoneColumnRange As Range = book.Worksheets(0).Cells.CreateRange(12, 0, sheet.Cells.MaxDataRow +1, sheet.Cells.MaxDataColumn+1)

Dim _style As Aspose.Cells.Style = book.Styles(book.Styles.Add())

_style.HorizontalAlignment = TextAlignmentType.Right

Dim _styleFlag As New StyleFlag()

_styleFlag.All = True

telePhoneColumnRange.ApplyStyle(_style, _styleFlag)


book.Save(“e:\test2\tstd_ImportCustomObjects.xlsx”)

I know what you are saying, but when you do the same thing using the Excel, the file size stays very small. Even when you put the style to all rows in the column, the size is very small. So why is it the file generated by Aspose is large?

And also, what's with exception that is getting logged on the server?

Hi,


1) As I said, if you do not specify the exact row/column cells range, all the possible rows in a sheet would be formatted in that range (currently). Please use my suggestion for your need. And,we will check if we can enhance it more.

2) I could not find your mentioned Http exception, may be it due to your code/configuration or browser type. Please try our latest verison (v7.0.3.x), if you still find the exception, please create a separate web application (runnable), zip it and post it here to reproduce the issue on our end, we will check it soon.

Thank you.