C# 生成word文档并使用html设置方向图
我尝试使用以下代码生成word文档:C# 生成word文档并使用html设置方向图,c#,asp.net,html,export-to-word,C#,Asp.net,Html,Export To Word,我尝试使用以下代码生成word文档: HttpContext.Current.Response.Clear(); HttpContext.Current.Response.Charset = ""; HttpContext.Current.Response.ContentType = "application/doc"; HttpContext.Current.Response.AddHeader("Content-Disposition", "attachme
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Charset = "";
HttpContext.Current.Response.ContentType = "application/doc";
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;
filename=" + DateTime.Now + ".doc");
var strHTMLContent = new StringBuilder();
strHTMLContent.Append(
"<h1 title='Heading' align='Center'style='font-family: verdana; font
-size: 80 % ;color: black'><u>Document Heading</u> </h1> ");
strHTMLContent.Append("<br>");
strHTMLContent.Append("<table align='Center'>");
// Row with Column headers
strHTMLContent.Append("<tr>");
strHTMLContent.Append("<td style='width:100px; background:# 99CC00'>
<b>Column1 </b> </td>");
strHTMLContent.Append("<td style='width:100px;background:# 99CC00'>
<b>Column2 </b> </td>");
strHTMLContent.Append("<td style='width:100px; background:# 99CC00'>
<b>Column 3</b></td>");
strHTMLContent.Append(" </tr> ");
// First Row Data
strHTMLContent.Append("<tr>");
strHTMLContent.Append(
"<td style='width:100px'></td>");
strHTMLContent.Append(
"<td style='width:100px'>b</td>");
strHTMLContent.Append(
"<td style='width:100px'>c</td>");
strHTMLContent.Append("</tr>");
// Second Row Data
strHTMLContent.Append("<tr>");
strHTMLContent.Append(
"<td style='width:100px'>d</td>");
strHTMLContent.Append(
"<td style='width:100px'>e</td>");
strHTMLContent.Append(
"<td style='width:100px'>f</td>");
strHTMLContent.Append("</tr>");
strHTMLContent.Append("</table>");
strHTMLContent.Append("<br><br>");
strHTMLContent.Append(
"<p align='Center'> Note : This is a dynamically
generated word document </p> ");
HttpContext.Current.Response.Write(strHTMLContent);
// HttpContext.Current.Response.BinaryWrite(strHTMLContent);
HttpContext.Current.Response.End();
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Charset=“”;
HttpContext.Current.Response.ContentType=“应用程序/doc”;
HttpContext.Current.Response.AddHeader(“内容处置”、“附件”;
filename=“+DateTime.Now+”.doc”);
var strHTMLContent=新的StringBuilder();
strHTMLContent.Append(
“文件标题”);
strHTMLContent.Append(“
”);
strHTMLContent.追加(“”);
//带列标题的行
strHTMLContent.追加(“”);
strHTMLContent.Append(“
第1栏“);
strHTMLContent.Append(“
第2栏“;
strHTMLContent.Append(“
第3栏“);
strHTMLContent.追加(“”);
//第一行数据
strHTMLContent.追加(“”);
strHTMLContent.Append(
"");
strHTMLContent.Append(
“b”);
strHTMLContent.Append(
“c”);
strHTMLContent.追加(“”);
//第二行数据
strHTMLContent.追加(“”);
strHTMLContent.Append(
“d”);
strHTMLContent.Append(
“e”);
strHTMLContent.Append(
“f”);
strHTMLContent.追加(“”);
strHTMLContent.追加(“”);
strHTMLContent.Append(“
”);
strHTMLContent.Append(
“注意:这是一个动态
生成的word文档“
”;
HttpContext.Current.Response.Write(strHTMLContent);
//HttpContext.Current.Response.BinaryWrite(strHTMLContent);
HttpContext.Current.Response.End();
HttpContext.Current.Response.Flush();
它的工作很好,但我想使其横向和印刷布局,因为它出现在网页布局
有人能帮我吗?我自己也没有试过,但不明白为什么它不起作用 如果您已经在编写html,然后将其显示为Word文档,为什么不使用css进行页面定向,如下所示:
@media screen and (orientation:landscape) {
body { opacity: 0; }
}
我找到了解决办法。这样解决:
Response.Write("<html>")
Response.Write("<head>")
Response.Write("<META HTTP-EQUIV=""Content-Type"" CONTENT=""text/html;
charset=UTF-8"">")
Response.Write("<meta name=ProgId content=Word.Document>")
Response.Write("<meta name=Generator content=""Microsoft Word 9"">")
Response.Write("<meta name=Originator content=""Microsoft Word 9"">")
Response.Write("<style>")
Response.Write("@page Section1 {size:595.45pt 841.7pt; margin:1.0in 1.25in 1.0in
1.25in;mso-header-margin:.5in;mso-footer-margin:.5in;mso-paper-source:0;}")
Response.Write("div.Section1 {page:Section1;}")
Response.Write("@page Section2 {size:841.7pt 595.45pt;mso-page-orientation:
landscape;margin:1.25in 1.0in 1.25in 1.0in;mso-header-margin:.5in;
mso-footer-margin:.5in;mso-paper-source:0;}")
Response.Write("div.Section2 {page:Section2;}")
Response.Write("</style>")
Response.Write("</head>")
Response.Write("<body>")
Response.Write("<div class=Section2>")
Response.Write(“”)
回答。写(“”)
回答。写(“”)
回答。写(“”)
回答。写(“”)
回答。写(“”)
回答。写(“”)
回答.写(“@page section 1{大小:595.45pt 841.7pt;边距:1.0in 1.25in 1.0in
1.25英寸;mso页眉边距:.5英寸;mso页脚边距:.5英寸;mso纸张来源:0;}”)
写(“div.Section1{page:Section1;}”)
响应.写入(“@page section 2{size:841.7pt 595.45pt;mso页面方向:
横向;边距:1.25英寸1.0英寸1.25英寸1.0英寸;mso页眉边距:5英寸;
mso页脚边距:.5英寸;mso纸张来源:0;}”)
写下(“div.Section2{page:Section2;}”)
回答。写(“”)
回答。写(“”)
回答。写(“”)
回答。写(“”)
我解决了这个问题,这对我来说很好
<style>
@page
{
mso-page-border-surround-header: no;
mso-page-border-surround-footer: no;
}
@page Section1
{
size:841.9pt 595.3pt;
mso-page-orientation:landscape;
margin: 0.7cm 0.7cm 0.7cm 0.7cm;
mso-header-margin: 42.55pt;
mso-footer-margin: 49.6pt;
mso-paper-source: 0;
layout-grid: 18.0pt;
}
div.Section1
{
page: Section1;
}
</style>
<div class="Section1"></div>
@页面
{
mso页面边框环绕页眉:否;
mso页面边框环绕页脚:否;
}
@第1页
{
尺寸:841.9磅595.3磅;
mso页面定位:横向;
边缘:0.7厘米0.7厘米0.7厘米0.7厘米0.7厘米;
mso页眉边距:42.55磅;
mso页脚边距:49.6磅;
mso论文来源:0;
布置网格:18.0pt;
}
第1节
{
第页:第1节;
}
对于打印布局,请使用此代码
string strBody = string.Empty;
strBody = @"<html xmlns:o='urn:schemas-microsoft-com:office:office' " +
"xmlns:w='urn:schemas-microsoft-com:office:word'" +
"xmlns='http://www.w3.org/TR/REC-html40'>";
strBody = strBody + "<!--[if gte mso 9]>" +
"<xml>" +
"<w:WordDocument>" +
"<w:View>Print</w:View>" +
"<w:Zoom>100</w:Zoom>" +
"</w:WordDocument>" +
"</xml>" +
"<![endif]-->";
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Charset = "";
HttpContext.Current.Response.ContentType = "application/vnd.ms-word";
HttpContext.Current.Response.AddHeader("Content-Disposition", "inline;filename=ContentDocument.doc");
StringBuilder htmlCode = new StringBuilder();
htmlCode.Append("<html>");
htmlCode.Append("<head>"+strBody+" <style type=\"text/css\">body {font-family:arial;font-size:14.5;}</style></head>");
htmlCode.Append("<body>");
... populate htmlCode ...
htmlCode.Append("</body></html>");
HttpContext.Current.Response.Write(htmlCode.ToString());
HttpContext.Current.Response.End();
HttpContext.Current.Response.Flush();
string strBody=string.Empty;
strBody=@;
strBody=strBody+“”;
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Charset=“”;
HttpContext.Current.Response.ContentType=“应用程序/vnd.ms word”;
HttpContext.Current.Response.AddHeader(“内容处置”,“内联;文件名=ContentDocument.doc”);
StringBuilder htmlCode=新的StringBuilder();
htmlCode.追加(“”);
追加(“+strBody+”正文{font-family:arial;font-size:14.5;}”);
htmlCode.追加(“”);
... 填充HTML代码。。。
htmlCode.追加(“”);
HttpContext.Current.Response.Write(htmlCode.ToString());
HttpContext.Current.Response.End();
HttpContext.Current.Response.Flush();
@r可能该文章已被删除。您可以将其放入Word html标题中: