| | |
Excel Export
Please support our ASP.NET advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: May 2008
Posts: 1
Reputation:
Solved Threads: 0
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.
'Create a string builder to store the header of the file including printer set, titles, style, and sheet layout
'Render the table as html and write the response
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.
asp.net Syntax (Toggle Plain Text)
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
asp.net Syntax (Toggle Plain Text)
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
asp.net Syntax (Toggle Plain Text)
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()
Last edited by peter_budo; Jan 13th, 2009 at 5:41 pm. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks) and [icode] (inline code) tags.
I would suggest downloading one of the express versions of Visual Studio 200X and registering it, after registering you get access to registration benefits, one of these is a .NET component called SpreadsheetGear 2006, it's free and makes real Spreadsheets, admittedly there is a learning curve, but essentially you will be providing clients with a real .xls file instead of tricking the clients OS into saving html or CSV as a .xls file.
It enables you to do this:
It enables you to do this:
DataTable dt = /*get your data table here*/
IWorkbook workbook = Factory.GetWorkbook();
IWorksheet worksheet = workbook.Worksheets["Sheet1"];
IRange cells = worksheet.Range;
cells.CopyFromDataTable(dt, SpreadsheetGear.Data.SetDataFlags.NoColumnHeaders);
// Auto size all worksheet columns which contain data
worksheet.UsedRange.Columns.AutoFit();
// Stream the Excel spreadsheet to the client in a format
// compatible with Excel 97/2000/XP/2003/2007.
Response.Clear();
Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader("Content-Disposition", "attachment; filename=" yourfilename.xls");
workbook.SaveToStream(Response.OutputStream, FileFormat.XLS97);
Response.End(); Bit more detail about how to get your hands on SpreadsheetGear here:
http://web2asp.net/2009/01/generatin...om-aspnet.html
http://web2asp.net/2009/01/generatin...om-aspnet.html
![]() |
Similar Threads
- DataGrid to Excel Export as a ZIP file (VB.NET)
- XML to Excel (XML, XSLT and XPATH)
- excel+php (PHP)
- Help with Excel Export (VB.NET)
- help needed to show Excel graphs (only) in VB window (Visual Basic 4 / 5 / 6)
- Exporting Data from App To Excel c# (C#)
- How to export excel data to a search engine? (MS Access and FileMaker Pro)
Other Threads in the ASP.NET Forum
- Previous Thread: merging two text files
- Next Thread: WebRequest Capture Dynamic Content
| Thread Tools | Search this Thread |
.net 2.0 3.5 ajax appliances application asp asp.net beginner box browser businesslogiclayer button c# c#gridviewcolumn cac checkbox child class compatible confirmationcodegeneration content contenttype control countryselector courier database datagrid datagridview datalist deployment development dgv dialog dropdownmenu dynamic dynamically edit embeddingactivexcontrol feedback fileuploader fill findcontrol flash flv form forms gridview gudi homeedition hosting iis javascript jquery list menu mssql nameisnotdeclared news novell opera order parent problem radio ratings redirect registration relationaldatabases reportemail search security select serializesmo.table sessionvariables silverlight smoobjects software sql sql-server ssl tracking treeview typeof validatedate validation vb.net videos vista visual-studio visualstudio vs2008 web webapplications webarchitecture webdevelopment webprogramming wizard xml xsl





