简体   繁体   English

如何从经典ASP输出Excel * .xls文件

[英]How to output an Excel *.xls file from classic ASP

I have a number of generated html tables that I need to output as an Excel file. 我有许多生成的html表,需要将它们输出为Excel文件。 The site is codded in classic ASP. 该网站采用经典ASP编写。 Is this possible? 这可能吗? Could it be done by somehow using the Open Office libraries? 可以通过某种方式使用Open Office库来完成吗?


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. 理想情况下,我希望用户能够单击一个链接,该链接将开始下载.xls文件。 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. 使用IE7来获取页面时似乎失败。 IE says that it "cannot download excelTest.asp" and that "The requested site is either unavailable or cannot be found." IE表示它“无法下载excelTest.asp”,并且“所请求的站点不可用或找不到。”

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 . 更新:我写了一篇关于如何在ASP Classic中生成Excel文件的博客文章。 It contains a rather useful piece of code to generate both xls and csv files. 它包含一个相当有用的代码,可以同时生成xls和csv文件。

MS made a COM library called Office Web Components to do this. MS为此创建了一个名为Office Web Components的COM库。 MSOWC.dll needs to be registered on the server. MSOWC.dll需要在服务器上注册。 It can create and manipulate office document files. 它可以创建和处理Office文档文件。

How To Use the Spreadsheet Web Component with Visual Basic 如何在Visual Basic中使用电子表格Web组件

Working with the Office Web Components 使用Office Web组件

I had the same issue until I added Response.Buffer = False. 在添加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" Response.Buffer = False Response.ContentType =“ application / vnd.ms-excel” Response.AddHeader“ Content-Disposition”,“附件; filename = 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. 您尝试打开的文件“ FileName [1] .xls”的格式与文件扩展名指定的格式不同。 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. 希望能有所帮助。

You can always just export the HTML table to an XLS document. 您始终可以只将HTML表导出到XLS文档。 Excel does a pretty good job understanding HTML tables. 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. 另一个可能的方法是将HTML表导出为CSV或TSV文件,但是您需要在代码中设置格式。 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. Microsoft.Office.Interop中有一些类允许您以编程方式创建Excel文件,但我一直发现它们有些笨拙。 You can find a .NET version of creating a spreadsheet here , which should be pretty easy to modify for classic ASP. 您可以在此处找到创建电子表格的.NET版本,对于传统的ASP来说,修改它应该非常容易。

As for .NET, I've always liked CarlosAG's Excel XML Writer Library . 至于.NET,我一直很喜欢CarlosAG的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. 它具有一个不错的生成器,因此您可以设置Excel文件,将其另存为XML电子表格,并生成执行所有格式设置和所有操作的代码。 I know it's not classic ASP, but I thought that I would throw it out there. 我知道它不是经典的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"

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. 如果输出制表符分隔的文本并使文件名为* .xls,则Excel会在没有异议,问题或警告的情况下打开它。 So just crank the data out into a text file with tab delimitation and you can open it with Excel or Open Office. 因此,只需将数据转换为带有制表符分隔的文本文件,即可使用Excel或Open Office打开它。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM