您好我尝试在托管的.net软件包上使用iTextSharp,但由于安全限制而遇到了困难。然后我决定输出到word文档而不是有效但是它似乎只允许我导出到.doc而不是.docx这有点问题。还有一种方法可以使文档格局化吗?
这里是我发现的代码适用于.doc的代码。
protected void ButtonWord_Click(object sender, EventArgs e)
{
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition",
"attachment;filename=GridViewExport.doc");
Response.Charset = "";
Response.ContentType = "application/vnd.ms-word ";
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
GridView1.AllowPaging = false;
GridView1.DataSource = Session["tblConversations"];
this.GridView1.Columns[1].Visible = false;
this.GridView1.Columns[2].Visible = false;
this.GridView1.Columns[3].Visible = false;
this.GridView1.Columns[0].Visible = false;
GridView1.DataBind();
GridView1.RenderControl(hw);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
}
答案 0 :(得分:0)
我在这里找到了解决方案 https://www.aspforums.net/Threads/889165/Export-Gridview-to-Word-in-Landscape-in-ASPNet/
它不会导出到docx,只会导出doc。
protected void ButtonWord_Click(object sender, EventArgs e)
{
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.doc");
Response.Charset = "";
Response.ContentType = "application/vnd.ms-word ";
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>");
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
GridView1.AllowPaging = false;
GridView1.DataSource = Session["tblConversations"];
GridView1.DataBind();
this.GridView1.Columns[1].Visible = false;
this.GridView1.Columns[2].Visible = false;
this.GridView1.Columns[3].Visible = false;
this.GridView1.Columns[0].Visible = false;
GridView1.RenderControl(hw);
Response.Write(sw.ToString());
Response.Write("</div>");
Response.Write("</body>");
Response.Write("</html>");
Response.Flush();
Response.End();
}