我正在创建一个HTML页面,可以在用户单击“下载”按钮后以.docx的形式下载。我已经设法下载了.docx文件但是当我尝试打开它时,出现了一个弹出窗口,说“该文件已损坏,无法打开”。
Fyi,我尝试过使用这些:
Response.ContentType = "application/vnd.ms-word"
Response.ContentType = "application/octet-stream"
Response.ContentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
如何使用经典ASP将HTML页面中的每个数据(图像和表格)都扩展为带有扩展名docx的ms字词?
<%if Request("btn_download")<>"" Then
'Only set the headers if this is request for download
Response.Buffer = TRUE
Response.ContentType = "application/vnd.ms-word"
Response.AddHeader "content-disposition","attachment; filename=thefile.docx;"
end if
%>
<!DOCTYPE html>
<html>
<head>
<%response.write("<h1><center>Welcome!</h1>")%>
<center><img src="http://www.themelab.com/wp-content/uploads/smiley.jpg" alt="Smiley" style="width:auto;height:auto;"></center>
<style>
table {
font-family: arial, sans-serif;
border-collapse: collapse;
width: 100%;
}
td, th {
border: 1px solid #dddddd;
text-align: left;
padding: 8px;
}
tr:nth-child(even) {
background-color: #dddddd;
}
</style>
</head>
<body>
<h1> </h1>
<table>
<tr>
<th>Name</th>
<th>Contact</th>
<th>Country</th>
</tr>
<tr>
<td>Alfreds Futterkiste</td>
<td>Maria Anders</td>
<td>Germany</td>
</tr>
<tr>
<td>Centro comercial Moctezuma</td>
<td>Francisco Chang</td>
<td>Mexico</td>
</tr>
<tr>
<td>Ernst Handel</td>
<td>Roland Mendel</td>
<td>Austria</td>
</tr>
<tr>
<td>Island Trading</td>
<td>Helen Bennett</td>
<td>UK</td>
</tr>
<tr>
<td>Laughing Bacchus Winecellars</td>
<td>Yoshi Tannamuri</td>
<td>Canada</td>
</tr>
<tr>
<td>Magazzini Alimentari Riuniti</td>
<td>Giovanni Rovelli</td>
<td>Italy</td>
</tr>
</table>
<%if Request("btn_download")="" Then
'Show the download button only if the button has not been clicked.
%>
<p>Click the button below to copy the page as word document.</p>
<form>
<input type="submit" name="btn_download" value="Download">
</form>
<%End If%>
</body>
</html>
答案 0 :(得分:0)
其实我同意@Amos,它更容易理解。 不确定确切的语法,但这里有一些你可以参考的链接。
将ASP.NET导出到MSWORD - http://forums.asp.net/t/1204503.aspx
http://www.aspsnippets.com/Articles/Export-GridView-To-Word-Excel-PDF-CSV-Formats-in-ASP.Net.aspx
并尝试从这段代码中学习,它有效。
<%
Response.ContentType = "application/msword"
%>
<html xmlns:v="urn:schemas-microsoft-com:vml"
xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:w="urn:schemas-microsoft-com:office:word">
...
<v:shape id="_x0000_s1030" type="#_x0000_t75" style='position:absolute;
left:0;text-align:left;margin-left:0;margin-top:17.95pt;width:7in;height:116.85pt;
z-index:2;mso-position-horizontal:center;mso-position-horizontal-relative:page;
mso-position-vertical-relative:page'>
<v:imagedata src="http://xxx/image001.gif"/>
<w:wrap anchorx="page" anchory="page"/>
<w:anchorlock/>
</v:shape><![endif]--><![if !vml]><span style='mso-ignore:vglayout;position:
absolute;z-index:0;left:0px;margin-left:0px;margin-top:24px;width:672px;
height:156px'><img width=672 height=156
src="http://xxx/image001.gif" v:shapes="_x0000_s1030"></span><![endif]>