By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,087 Members | 1,508 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,087 IT Pros & Developers. It's quick & easy.

How to generate excel files at asp.net server side

P: n/a
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
Dec 6 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
"Maxwell2006" <al******@newsgroup.nospamwrote in message
news:Oj**************@TK2MSFTNGP03.phx.gbl...
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/Aspos...s/Default.aspx
Dec 6 '06 #2

P: n/a
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: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"&gt;
&lt;DocumentProperties
xmlns="urn:schemas-microsoft-com:office:office"&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:office:office"&gt;
&lt;RelyOnVML/&gt;
&lt;AllowPNG/&gt;
&lt;DownloadComponents/&gt;
&lt;/OfficeDocumentSettings&gt;
&lt;ExcelWorkbook xmlns="urn:schemas-microsoft-com:office: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>
Dec 6 '06 #3

P: n/a
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 (wa****@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.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/subscripti...t/default.aspx.
==================================================

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

Dec 7 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.