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?
我有许多生成的html表需要作为Excel文件输出。该网站是在经典ASP。这是可能的吗?是否可以通过使用开放式办公室图书馆来实现?
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:
编辑:到目前为止,我已经尝试了一些建议,但似乎失败了。理想情况下,我希望用户能够单击将要开始下载.xls文件的链接。这段代码:
<%@ 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."
当IE7被用来获取页面时,似乎失败了。IE说它“无法下载excelTest”。并且“请求的站点要么不可用,要么找不到。”
6 个解决方案
#1
18
It's AddHeader, not AppendHeader.
这是AddHeader,不是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.
更新:我写了一篇关于如何在ASP Classic中生成Excel文件的博文。它包含了一段非常有用的代码来生成xls和csv文件。
#2
4
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.
MS制作了一个名为Office Web Components的COM库来实现这一点。MSOWC。需要在服务器上注册dll。它可以创建和操作office文档文件。
How To Use the Spreadsheet Web Component with Visual Basic
如何使用电子表格Web组件与Visual Basic
Working with the Office Web Components
与Office Web组件一起工作。
#3
0
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.
有一个我用过的“又便宜又脏”的把戏……嘘,不要告诉任何人。如果输出选项卡分隔文本并使文件名*。然后,xls将Excel打开,没有异议、问题或警告。因此,只需将数据转换成文本文件并使用tab分隔符,就可以使用Excel或open Office打开它。
#4
0
You must specify the file to be downloaded (attachment) by the client in the http header:
您必须在http头文件中指定要下载的文件(附件):
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
http://classicasp.aspfaq.com/general/how-do-i-prompt-a-save-as-dialog-for-an-accepted-mime-type.html
#5
0
You can always just export the HTML table to an XLS document. Excel does a pretty good job understanding HTML tables.
您总是可以将HTML表导出到XLS文档。Excel很好地理解了HTML表。
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.
另一种可能是将HTML表导出为CSV或TSV文件,但您需要在代码中设置格式。这不是很难做到的。
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.
办公室里有一些课程。Interop允许您以编程方式创建Excel文件,但我总是发现它们有点笨拙。您可以在这里找到创建电子表格的。net版本,这对于经典的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.
对于。net,我一直喜欢CarlosAG的Excel XML Writer库。它有一个很好的生成器,所以您可以设置您的Excel文件,将它保存为一个XML电子表格,它生成代码来完成所有的格式化和所有操作。我知道这不是经典的ASP,但我想我会把它扔到那里。
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"
#6
0
I had the same issue until I added Response.Buffer = False. Try changing the code to the following.
我也有同样的问题,直到我增加了回应。缓冲= False。尝试将代码更改为以下代码。
Response.Buffer = False Response.ContentType = "application/vnd.ms-excel" Response.AddHeader "Content-Disposition", "attachment; filename=excelTest.xls"
响应。缓冲= False响应。盾ContentType = "应用程序/。ms excel”反应。AddHeader“附加”,“附件;文件名= excelTest.xls”
The only problem I have now is that when Excel opens the file I get the following message.
我现在遇到的唯一问题是,当Excel打开文件时,我得到以下消息。
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?
你要打开的文件,文件名[1]。xls'的格式与文件扩展名指定的格式不同。在打开文件之前,验证文件没有被损坏,并且来自受信任的源文件。你想现在打开文件吗?
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.
希望有帮助。
#1
18
It's AddHeader, not AppendHeader.
这是AddHeader,不是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.
更新:我写了一篇关于如何在ASP Classic中生成Excel文件的博文。它包含了一段非常有用的代码来生成xls和csv文件。
#2
4
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.
MS制作了一个名为Office Web Components的COM库来实现这一点。MSOWC。需要在服务器上注册dll。它可以创建和操作office文档文件。
How To Use the Spreadsheet Web Component with Visual Basic
如何使用电子表格Web组件与Visual Basic
Working with the Office Web Components
与Office Web组件一起工作。
#3
0
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.
有一个我用过的“又便宜又脏”的把戏……嘘,不要告诉任何人。如果输出选项卡分隔文本并使文件名*。然后,xls将Excel打开,没有异议、问题或警告。因此,只需将数据转换成文本文件并使用tab分隔符,就可以使用Excel或open Office打开它。
#4
0
You must specify the file to be downloaded (attachment) by the client in the http header:
您必须在http头文件中指定要下载的文件(附件):
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
http://classicasp.aspfaq.com/general/how-do-i-prompt-a-save-as-dialog-for-an-accepted-mime-type.html
#5
0
You can always just export the HTML table to an XLS document. Excel does a pretty good job understanding HTML tables.
您总是可以将HTML表导出到XLS文档。Excel很好地理解了HTML表。
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.
另一种可能是将HTML表导出为CSV或TSV文件,但您需要在代码中设置格式。这不是很难做到的。
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.
办公室里有一些课程。Interop允许您以编程方式创建Excel文件,但我总是发现它们有点笨拙。您可以在这里找到创建电子表格的。net版本,这对于经典的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.
对于。net,我一直喜欢CarlosAG的Excel XML Writer库。它有一个很好的生成器,所以您可以设置您的Excel文件,将它保存为一个XML电子表格,它生成代码来完成所有的格式化和所有操作。我知道这不是经典的ASP,但我想我会把它扔到那里。
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"
#6
0
I had the same issue until I added Response.Buffer = False. Try changing the code to the following.
我也有同样的问题,直到我增加了回应。缓冲= False。尝试将代码更改为以下代码。
Response.Buffer = False Response.ContentType = "application/vnd.ms-excel" Response.AddHeader "Content-Disposition", "attachment; filename=excelTest.xls"
响应。缓冲= False响应。盾ContentType = "应用程序/。ms excel”反应。AddHeader“附加”,“附件;文件名= excelTest.xls”
The only problem I have now is that when Excel opens the file I get the following message.
我现在遇到的唯一问题是,当Excel打开文件时,我得到以下消息。
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?
你要打开的文件,文件名[1]。xls'的格式与文件扩展名指定的格式不同。在打开文件之前,验证文件没有被损坏,并且来自受信任的源文件。你想现在打开文件吗?
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.
希望有帮助。