2012-07-09 67 views
4

我嘗試使用此代碼生成word文檔:生成的Word文檔,並設置定向景觀HTML

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(); 

它工作得很好,但我想讓它的風景和打印佈局導致它出現在Web版式 有誰能夠幫助我?

+0

你可以把在Word生成的HTML頭:http://stackoverflow.com/questions/37019517/c-sharp-httpcontext-不工作時試圖保存html-to-word-in-c-sharp – ib11 2016-05-06 16:58:28

回答

3

我找到了解決方案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>") 
+0

鏈接不起作用... – rwalter 2014-03-18 16:07:31

+0

@rwalter可能文章被刪除 – Alex 2014-03-19 12:12:58

0

我還沒有嘗試過自己,但不明白爲什麼它不應該工作

如果你已經寫那麼你的HTML它顯示爲Word文檔的原因,爲什麼不使用CSS頁面方向是這樣的:

@media screen and (orientation:landscape) { 
    body { opacity: 0; } 
} 
+0

它對我不起作用,.. – Alex 2012-07-09 13:46:44

0

我解決了這個問題,對我來說工作很好。

<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> 
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();