PDA

View Full Version : creating an excel file from asp apge...


robojob
09-06-2006, 11:51 AM
i have a page that is asp and when opened creates an excel file to save using this Response.ContentType = "application/vnd.ms-excel" at the top of my code. What i would like to be able to do is set the page orientation, margins etc. is this possible with added asp code to that first line or would this have to be done each time in excel?

graficus
09-06-2006, 06:34 PM
I just saw a similar discussion here (http://www.experts-exchange.com/Web/Application_Providers/Q_20658116.html)

divide
09-12-2006, 02:56 PM
I'd reccomend using the microsoft xmlss format for generating your spreadsheet in ASP. You can define exactly how to format anything, and it also supports multiple worksheets within a spreadsheet. Below is a simple example:

Response.ContentType="application/vnd.ms-excel"
Response.AddHeader "Content-Disposition", "filename=whatever.xls;"

%><?xml version="1.0"?>
<ss:Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:x="urn:schemas-microsoft-com:office:excel"
xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:html="http://www.w3.org/TR/REC-html40">
<ss:Styles>
<ss:Style ss:ID="BigHeadings">
<ss:Alignment ss:Horizontal="Center" />
<ss:Font ss:Bold="1" ss:Size="20" ss:Underline="Single"/>
</ss:Style>
<ss:Style ss:ID="MidHeadings">
<ss:Alignment ss:Horizontal="Center" />
<ss:Font ss:Bold="1" ss:Size="14"/>
</ss:Style>
</ss:Styles>
<ss:Worksheet ss:Name="a work sheet">
<ss:Table>
<ss:Column ss:Index="1" ss:Width="90"/>
<ss:Column ss:Index="2" ss:Width="90"/>
<ss:Column ss:Index="3" ss:Width="90"/>
<ss:Column ss:Index="4" ss:Width="90"/>
<ss:Row>
<ss:Cell ss:MergeAcross="3" ss:StyleID="BigHeadings"><ss:Data ss:Type="String">Some title</ss:Data></ss:Cell>
</ss:Row>
</ss:Worksheet>
</ss:Workbook>

A full reference guide to the format can be found at http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnexcl2k2/html/odc_xmlss.asp

NOTE: Make sure that your ASP doesn't render anything in the output before it renders the <?xml version="1.0"?> bit, otherwise you'll get an XML parse error.