c# - 生成word文档并用html设置方向景观

标签 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", "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();

它工作正常,但我想让它横向显示并在打印版式中显示,因为它出现在网页版式中 谁能帮帮我?

最佳答案

我找到了解决方案 in this post .像这样解决:

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>")

关于c# - 生成word文档并用html设置方向景观,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11396109/

相关文章:

c# - 从 Observable 中获取第 N 个元素

asp.net - 捕获对ASP.NET ASMX Web服务的SOAP请求

C# ASP.NET、WebForms 到 MVC : Does it make sense to change in our case?

php - 如何在 php/HTML 中显示数学方程?

c# - 如何打开文本框上的弹出窗口获得焦点?使用 uwp

c# - 从 XAML 开始 VisualState 转换

asp.net - 将旧版 ASP.NET 应用程序转换为 MVC3 和 HTML5

javascript - Canvas 获取填充区域的百分比

javascript - 在更正拼写错误并重新提交后验证 javascript 中的用户输入不会脱离 else 构造

c# - 从窗口内容截屏(无边框)