您的位置:首页 > 其它

导出word如何默认打开为页面视图

2017-11-07 13:07 253 查看
最近在做一个商务导出发货单功能,商务要求导出word版,实现之后又反馈说导出的word默认打开是web版式,而不是常用的页面视图,在网上找了很久,最终找到解决方案。现附上代码:

//导出word
protected void btnExportWord_Click(object sender, EventArgs e)
{
try
{
if (this.hidData.Value != "")
{
string html = "<html><head></head><body><table><tr><td>1</td><td>2</td></tr><tr><td>11</td><td>22</td></tr></table></body></html>";
//清除反冲区的内容
Response.Clear();
//设置输出流的http字符集
Response.Charset = "gb2312";
//将一个HTTP头添加到输出流
Response.AddHeader("content-disposition", "attachment;filename=" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".doc");
//设置输出的HTTP MIME类型
Response.ContentType = "application/vnd.doc";
System.Text.StringBuilder sb = new System.Text.StringBuilder();
sb.Append(@html);
//把字符数组写入HTTP响应输出流
Response.Write(sb.ToString());
//发送完,关闭
Response.End();
}
}
catch { }
}


1.修改html标签:

首先在html标签中要加入模板:

<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml"
xmlns="http://www.w3.org/TR/REC-html40">


2.在head标签之间加入以下文本:

<head>
<!--[if gte mso 9]><xml><w:WordDocument><w:View>Print</w:View><w:TrackMoves>false</w:TrackMoves><w:TrackFormatting/><w:ValidateAgainstSchemas/><w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid><w:IgnoreMixedContent>false</w:IgnoreMixedContent><w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText><w:DoNotPromoteQF/><w:LidThemeOther>EN-US</w:LidThemeOther><w:LidThemeAsian>ZH-CN</w:LidThemeAsian><w:LidThemeComplexScript>X-NONE</w:LidThemeComplexScript><w:Compatibility><w:BreakWrappedTables/><w:SnapToGridInCell/><w:WrapTextWithPunct/><w:UseAsianBreakRules/><w:DontGrowAutofit/><w:SplitPgBreakAndParaMark/><w:DontVertAlignCellWithSp/><w:DontBreakConstrainedForcedTables/><w:DontVertAlignInTxbx/><w:Word11KerningPairs/><w:CachedColBalance/><w:UseFELayout/></w:Compatibility><w:BrowserLevel>MicrosoftInternetExplorer4</w:BrowserLevel><m:mathPr><m:mathFont m:val="Cambria Math"/><m:brkBin m:val="before"/><m:brkBinSub m:val="--"/><m:smallFrac m:val="off"/><m:dispDef/><m:lMargin m:val="0"/> <m:rMargin m:val="0"/><m:defJc m:val="centerGroup"/><m:wrapIndent m:val="1440"/><m:intLim m:val="subSup"/><m:naryLim m:val="undOvr"/></m:mathPr></w:WordDocument></xml><![endif]-->
</head>


这样就可以实现把保存的word文档默认为“页面视图”打开。

注意点:这种方式是将整个页面编码作为字符串,在编码的html标签中和head标签之间添加相应内容是可以实现默认页面视图,即必须要保证将上述添加的这两串代码放到导出流中,如果只是导出页面中的一部分,是无法实现这种功能的。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  导出word 页面视图