How to generate excel files at asp.net server side

M

Maxwell2006

Hi,

We have Excel 2003 templates and we need to automatically generate excel
2003 xls files based on our SQL server database tables' data. Since we need
to generate the xls files at out asp.net server side, I assume we cannot use
excel automation based on the license agreement.

What would b the best way to do that?

Any help would be appreciated,
Max
 
M

Mark Rae

We have Excel 2003 templates and we need to automatically generate excel
2003 xls files based on our SQL server database tables' data. Since we
need to generate the xls files at out asp.net server side, I assume we
cannot use excel automation based on the license agreement.

You can use automation, but it's strongly discouraged, and Microsoft won't
support you if you do...
What would b the best way to do that?

If you just need plain text, export the data to CSV.

Otherwise, save it as XML in the Excel XML file format, or use a 3rd-party
utility like this: http://www.aspose.com/Products/Aspose.Cells/Default.aspx
 
K

Keith Patrick

I've always outputed it as an HTML table with an XLS file extension. Excel
is able to interpret it as a regular worksheet. However, if you want to
output a workbook (multiple tables across multiple sheets), you will need to
output to the Excel XML-based format and Strong.Format in the user ID,
company name, and workbook XML based on this template (it's escaped for
appearing in an XML-based .resx file) (and subsequently do a Response.Write
of the resulting string):

The response content header:
HttpResponse response = HttpContext.Current.Response;
response.ContentType = "application/vnd.ms-excel";
response.Charset = String.Empty;
response.AddHeader("Content-Disposition",
"attachment;filename=report.xlw");


The template:
<data name="ExcelWorkbook">
<value>&lt;?xml version="1.0"?&gt;
&lt;Workbook xmlns="urn:schemas-microsoft-com:eek:ffice:spreadsheet"
xmlns:eek:="urn:schemas-microsoft-com:eek:ffice:eek:ffice"
xmlns:x="urn:schemas-microsoft-com:eek:ffice:excel"
xmlns:ss="urn:schemas-microsoft-com:eek:ffice:spreadsheet"
xmlns:html="http://www.w3.org/TR/REC-html40"&gt;
&lt;DocumentProperties
xmlns="urn:schemas-microsoft-com:eek:ffice:eek:ffice"&gt;
&lt;Author&gt;{0}&lt;/Author&gt;
&lt;LastAuthor&gt;{1}&lt;/LastAuthor&gt;
&lt;Created&gt;{2}&lt;/Created&gt;
&lt;Company&gt;{3}&lt;/Company&gt;
&lt;Version&gt;10.3501&lt;/Version&gt;
&lt;/DocumentProperties&gt;
&lt;OfficeDocumentSettings
xmlns="urn:schemas-microsoft-com:eek:ffice:eek:ffice"&gt;
&lt;RelyOnVML/&gt;
&lt;AllowPNG/&gt;
&lt;DownloadComponents/&gt;
&lt;/OfficeDocumentSettings&gt;
&lt;ExcelWorkbook xmlns="urn:schemas-microsoft-com:eek:ffice:excel"&gt;
&lt;WindowHeight&gt;9720&lt;/WindowHeight&gt;
&lt;WindowWidth&gt;14235&lt;/WindowWidth&gt;
&lt;WindowTopX&gt;120&lt;/WindowTopX&gt;
&lt;WindowTopY&gt;135&lt;/WindowTopY&gt;
&lt;ActiveSheet&gt;0&lt;/ActiveSheet&gt;
&lt;ProtectStructure&gt;False&lt;/ProtectStructure&gt;
&lt;ProtectWindows&gt;False&lt;/ProtectWindows&gt;
&lt;/ExcelWorkbook&gt;
&lt;Styles&gt;
&lt;Style ss:ID="Default" ss:Name="Normal"&gt;
&lt;Alignment ss:Vertical="Bottom"/&gt;
&lt;Borders/&gt;
&lt;Font/&gt;
&lt;Interior/&gt;
&lt;NumberFormat/&gt;
&lt;Protection/&gt;
&lt;/Style&gt;
&lt;Style ss:ID="Header"&gt;
&lt;Font ss:Bold="1"/&gt;
&lt;/Style&gt;
&lt;Style ss:ID="ShortDateCell"&gt;
&lt;NumberFormat ss:Format="m/d/yyyy;@" /&gt;
&lt;/Style&gt;
&lt;/Styles&gt;
{4}
&lt;/Workbook&gt;
</value>
</data>
 
W

Walter Wang [MSFT]

Hi Max,

You might want to take a look at following KB articles:

#How To Export Data in a DataGrid on an ASP . NET WebForm to Microsoft
Excel
http://support.microsoft.com/kb/317719

#How to transform a DataSet to spreadsheet XML for Excel by using Visual
Basic .NET and ASP.NET
http://support.microsoft.com/kb/319180/


However, as I understand from your post, you're using an existing Excel
template to create new excel file, this might not work with using normal
exporting approaches.

Also, as Mark pointed, automation excel at server-side is not supported by
Microsoft:

#Considerations for server-side Automation of Office
http://support.microsoft.com/kb/257757/
Microsoft does not currently recommend, and does not support, Automation of
Microsoft Office applications from any unattended, non-interactive client
application or component (including ASP, DCOM, and NT Services), because
Office may exhibit unstable behavior and/or deadlock when run in this
environment.


Therefore, I'm afraid you either have to use third party components to
generate the excel file or you could use following workaround if the data
needs to be generated isn't complicated:

You can save the template file as xml format, and load it as xml document,
then use XPath expression to find existing cell data or add new cell data.



Sincerely,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications. If you are using Outlook Express, please make sure you clear the
check box "Tools/Options/Read: Get 300 headers at a time" to see your reply
promptly.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top