I have a number of generated html tables that I need to output as an Excel file. The site is codded in classic ASP. Is this possible? Could it be done by somehow using the Open Office libraries?
EDIT: Thus far, I have tried some of the suggestions, but it seems to fail. Ideally, I want the user to be able to click a link that will begin the download of a .xls file. This code:
<%# Language=VBScript %>
<% option explicit
Response.ContentType = "application/vnd.ms-excel"
Response.AppendHeader "content-disposition", " filename=excelTest.xls"
%>
<table>
<tr>
<th>Test Col 1</th>
<th>Test Col 2</th>
<th>Test Col 3</th>
<th colspan="2">Test Col 4</th>
<th>Test Col 6</th>
<th>Test Col 7</th>
</tr>
<tr>
<td>Data</td>
<td>Data</td>
<td>Data</td>
<td>Data</td>
<td>Data</td>
<td>Data</td>
<td>Data</td>
</tr>
</table>
seems to fail when IE7 is used to get the page. IE says that it "cannot download excelTest.asp" and that "The requested site is either unavailable or cannot be found."
It's AddHeader, not AppendHeader.
<%# Language=VBScript %>
<% Option Explicit
Response.ContentType = "application/vnd.ms-excel"
Response.AddHeader "Content-Disposition", "attachment; filename=excelTest.xls"
%>
<table>
<tr>
<td>Test</td>
</tr>
</table>
Update:
I've written a blog post about how to generate Excel files in ASP Classic. It contains a rather useful piece of code to generate both xls and csv files.
MS made a COM library called Office Web Components to do this. MSOWC.dll needs to be registered on the server. It can create and manipulate office document files.
How To Use the Spreadsheet Web Component with Visual Basic
Working with the Office Web Components
There's a 'cheap and dirty' trick that I have used... shhhh don't tell anyone. If you output tab delimited text and make the file name *.xls then Excel opens it without objection, question or warning. So just crank the data out into a text file with tab delimitation and you can open it with Excel or Open Office.
You must specify the file to be downloaded (attachment) by the client in the http header:
Response.ContentType = "application/vnd.ms-excel"
Response.AppendHeader "content-disposition", "attachment: filename=excelTest.xls"
http://classicasp.aspfaq.com/general/how-do-i-prompt-a-save-as-dialog-for-an-accepted-mime-type.html
You can always just export the HTML table to an XLS document. Excel does a pretty good job understanding HTML tables.
Another possiblitly is to export the HTML tables as a CSV or TSV file, but you would need to setup the formatting in your code. This isn't too difficult to accomplish.
There's some classes in the Microsoft.Office.Interop that allow you to create an Excel file programatically, but I have always found them to be a little clumsy. You can find a .NET version of creating a spreadsheet here, which should be pretty easy to modify for classic ASP.
As for .NET, I've always liked CarlosAG's Excel XML Writer Library. It has a nice generator so you can setup your Excel file, save it as an XML spreadsheet and it generates the code to do all the formatting and everything. I know it's not classic ASP, but I thought that I would throw it out there.
With what you're trying above, try adding the header:
"Content-Disposition", "attachment; filename=excelTest.xls"
See if that works. Also, I always use this for the content type:
Response.ContentType = "application/octet-stream"
Response.ContentType = "application/vnd.ms-excel"
I had the same issue until I added Response.Buffer = False. Try changing the code to the following.
Response.Buffer = False
Response.ContentType = "application/vnd.ms-excel"
Response.AddHeader "Content-Disposition", "attachment; filename=excelTest.xls"
The only problem I have now is that when Excel opens the file I get the following message.
The file you are trying to open, 'FileName[1].xls', is in a different format than specified by the file extension. Verify that the file is not corrupted and is from a trusted source before opening the file. Do you want to open the file now?
When you open the file the data all appears in separate columns, but the spreadsheet is all white, no borders between the cells.
Hope that helps.
the XLS file is not a real Excel file. The code create an HTML file with .an xls extension. When you try open the XLS file with Notepad, you can see the HTML code.
Related
I have the file testeexcel.asp used to generate excel file in classic asp:
<%# Language=VBScript %>
<%
Response.AddHeader "Content-Disposition", "attachment; filename=excelTest.xls"
Response.ContentType = "application/vnd.ms-excel"
%>
<table>
<tr>
<td>Test</td>
</tr>
</table>
but when open in excel, the file is blank. If opened in notepad++, saved and reopened in excel the file works. See the link to test 104.41.63.35/testeexcel.asp
How to configure the file or IIS to resolve this problem?
Environment: Windows server 2012 - IIS 8.5
Marco - I have had success for the last several years with exporting from my Classic ASP to Excel using the setup you show -- but this week my clients have started reporting the same issue you describe although nothing about our application has changed. I have also been looking for an answer and I believe another poster may be correct pointing out that a recent Microsoft update is the culprit.
Opening excel files from the internet opens a blank excel window
I do not have the option to uninstall the update and have found that if I select to "save and open" instead of just "open" (and select "yes" to the prompt to 'open anyway?') my spreadsheet was displayed. Hope that helps!
I have developed a web application using JSP which creates a page with
several page elements and a with multiple records.
I wanted to export the content in that into an excel file.
I used the code
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition", "inline; filename="
+ "excel.xls");
as described in this article http://www.quicklyjava.com/export-web-page-to-excel/
But it exports the whole page.
How can a export only the content in that particular table?
I cannot use the Apache POI library since i have to format(text colors,cell colors) the content in the JSP. And i need the same formatting in the excel file.
Can somebody help me with this? :)
Haven't found a way to export only a single table in a page.
But the intended task(create a excel sheet with formatted data) can be done by Apache POI HSSF library
Here are the links to learn how to & examples -
http://poi.apache.org/spreadsheet/how-to.html#user_api
http://poi.apache.org/spreadsheet/examples.html
I got an .xls file from my friend. When I try to open that in excel it is opening fine. But when I try to open the same file in Wordpad it was showing the file contents using table tags like following:
<table border=1 cellspacing="0" cellpadding="0" width="580">
<style>
<!--table
.xl31
{mso-style-parent:style0;
mso-number-format:"\#";
text-align:left;
border-top:none;
border-right:.5pt solid black;
border-bottom:.5pt solid black;
border-left:none;
white-space:normal;}
-->
<tr><td> By _______________ </td><td colspan=2 > By ____________ </td></tr>
<tr><td>(Authorized Signature) </td><td colspan=2 > (Authorized Signature)</td></tr>
</table></th></tr>
</table>
But when I created an excel file using microsoft office-2003 and open it in Wordpad it is showing some junk data.
Could someone help me to find what could be the difference between these files?
It's common practice in many web apps out there to "cheat" a little and create Excel downloads by formatting data as an HTML table and sending it with a header describing the content as an Excel file. Excel will still render the data OK, thought it might complain that what was in the download didn't look like a real Excel file.
That's likely how your friend got that file. If you try it with an Excel file you created from Excel its either going to be binary (xls) or a zipped up set of XML files (2007+) so its not going to have any content you can read directly using Wordpad.
I'd like to go about offering a "download to excel" functionality for several different sets of data available on different parts of my coldfusion website. I'm using coldfusion and would love to use freely available custom tags / libraries that can help me accomplish this instead of coding it all myself from scratch. I've been pointed to cflib.org but am not sure how to get started. Anyone have any ideas, comments, or resources about downloading coldfusion website data to excel?
An approach that I have used in the past for an intranet with a load of tabular statistical data is to take advantage of the fact that Excel can read HTML.
On pages that I intend to provide Excel links for, I write out the core table as HTML to a file and include a link to that file in the rendered page. You can then serve this page with an .xls extension, use meta headers or even use an intermediary page with CFCONTENT in order to have your browser fire up Excel to display the content.
This approach can be extended for an arbitrary number of tables per page. You would have to write a tag to contain the table which will then take care of writing the file and supplying the download link for all HTML content contained within.
Sorry for the lack of example code. If my description of the process has left you scratching your head then I'll provide some examples for you.
You can also use an html table, as in:
<cfquery name="data" datasource="whatever">
...
</cfquery>
<cfheader name="Content-Disposition" value="inline; filename=fileName.xls">
<cfcontent type="application/x-msexcel" reset="true">
<table border="0" cellspacing="0" cellpadding="0">
<thead>
<tr>
<td>Col1</td>
<td>Col2</td>
</tr>
</thead>
<tbody>
<cfoutput query="data">
<tr>
<td>#data.Col1#</td>
<td>#data.Col2#</td>
</tr>
</cfoutput>
</tbody>
</table>
hth,
larry
If you are on ColdFusion 9, you can always use cfspreadsheet:
http://www.cfquickdocs.com/cf9/#cfspreadsheet
http://help.adobe.com/en_US/ColdFusion/9.0/Developing/WS434C35B0-279B-4051-A61B-D84B5D76189C.html
Building on #Ciaran Archer's answer, I usually use CSV to accomplish this, which Excel will open up from the web if installed on a client's PC. Assuming the data you're working with is coming out of a database, use the QueryToCSV() from Ben Nadel's site as such:
<cfquery name="data" datasource="">
...
</cfquery>
<cfinclude template="fun_queryToCSV.cfm">
<cfheader name="Content-Disposition" value="filename=my-file-name.xls">
<cfcontent type="test/csv" reset="true">
<cfoutput>#querytoCSV(data)#</cfoutput>
The "reset" on cfcontent clears the response buffer so that the only thing inside of it is what comes after. Alternately, if you already have the data as file, cfcontent has a "file" attribute that will serve its contents up directly.
In the end, we used the Query2Excel function from cflib.org, and modified it slightly to suit our specific needs.
For those on Coldfusion 9+ and able to use cfspreadsheet, you can leverage SpreadsheetReadBinary() to make the spreadsheet downloadable without having to write the spreadsheet to a file first.
We have a legacy ASP page that writes content to an excel file by generating client-side VB script based on data from the database. It uses set app = CreateObject("Excel.Application") to initialize Excel.
The problem is that this is an "unsigned activex control", and some clients are now saying they wont change their IE settings to make it work.
Apart from re-writing the page in ASP.NET using Aspose.Cells (which would take a long time for this page), I was wondering if anyone knows any other options that could help?
Thanks!
Although this thread refers to ASP.Net, a lot of it may be of interest to you:
Send query results to Excel from ASP.NET website
It mentions creating HTML output and modifying the headers to output to Excel. This may also be of interest http://support.microsoft.com/kb/199841
Here is a very simple example.
<%
Response.ContentType = "application/vnd.ms-excel"
%>
<TABLE>
<TR><TD>
<!-- Cell : A1 -->
2
</TD></TR>
<TR><TD>
<!-- Cell : A2 -->
3
</TD></TR>
<TR><TD>
<!-- Cell : A3 -->
=SUM(A1:A2)
</TD></TR>
</TABLE>
Thanks for the response. The problem is that the our spreadsheet generated has lots of additional functionality, e.g. setting up some "validation" for certain columns - the HTML solution would not be able to do this I think.
However - your answer forced me to take another look at the code, and it turns out simpler than I thought to do this using an "Excel Writer" like Aspose.Cells :)