Hi everyone
Here is my problem. I have been tasked with creating an excel export from my asp page which I have done successfully. The problem comes when the file is being downloaded. The excel spreadsheet contains about 3,000 records but the file size is over 11MB. When I attempt to save the file, excel tries to save it as a web page which is why I think the file size is so big. When the file is saved as an excel file, the file size shrinks dramatically to about 700k which is more realistic...
I have investigated the file content code in my asp page but I can't see anything that would cause excel to think that the spreadsheet is a web page. My code is below, if anyone has any ideas I would really appreciate your comments.
Response.Clear()
Response.Buffer = True
Response.ContentType = "application/vnd.ms-excel"
Response.AddHeader("content-disposition", "attachment;filename=Management Report.xls")
Response.Charset = ""
EnableViewState = False
'Create a string builder to store the header of the file including printer set, titles, style, and sheet layout
Dim sb As New StringBuilder
sb.Append("<html xmlns:x=""urn:schemas-microsoft-com:office:excel"">" & vbCrLf)
sb.Append("<head><style>@page {mso-header-data:""" & "Management Report" & """; mso-footer-data:""&C&P of &N""; mso-page-orientation:landscape;}" & vbCrLf)
sb.Append("table {font-size:10.0pt; font-family:Arial, sans-serif;}" & vbCrLf)
sb.Append(".xlTitleCell {font-size:10.0pt; font-weight:bold; colour:blue; back-colour:red; text-align:middle}" & vbCrLf)
sb.Append(".xlNumberCell {text-align:center; mso-number-format:""\#\,\#\#0\.00""}" & vbCrLf)
sb.Append(".xlPercentCell {text-align:center; mso-number-format:""0\.00%""}" & vbCrLf)
sb.Append("</style><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>Report </x:Name><x:WorksheetOptions><x:FreezePanes/><x:FrozenNoSplit/>" & vbCrLf)
'Render the table as html and write the response
Dim sw As New StringWriter
Dim hw As New HtmlTextWriter(sw)
Page.FindControl("tblExcelExport").RenderControl(hw)
sb.Append("<x:SplitHorizontal>" + intRowsAdded.ToString() + "</x:SplitHorizontal><x:SplitVertical>6</x:SplitVertical><x:TopRowBottomPane>" + (5 + intRowsAdded).ToString() + "</x:TopRowBottomPane><x:LeftColumnRightPane>17</x:LeftColumnRightPane><x:ActivePane>0</x:ActivePane><x:Panes><x:Pane><x:Number>0</x:Number></x:Pane></x:Panes>" & vbCrLf)
sb.Append("<x:Print><x:Scale>65</x:Scale><x:ValidPrinterInfo/><x:PaperSizeIndex>9</x:PaperSizeIndex></x:Print></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook><x:ExcelName><x:Name>Print_Titles</x:Name><x:SheetIndex>1</x:SheetIndex><x:Formula>=!$1:$7</x:Formula></x:ExcelName></xml></head><body>" & vbCrLf)
sb.Append(sw.ToString() & "</body></html>")
Response.Write(sb.ToString())
Response.End()