最近开发网上商城有发现我们会涉及到订单打印的问题,本来想到直接显示打印即可但是考虑还得下载到本地,最后只有直接选择控件按流方式输出。
导入到word中代码如下:
protected void Button1_Click(object sender, EventArgs e) { StringBuilder sb = new StringBuilder(); StringWriter sw = new StringWriter(sb); HtmlTextWriter htw = new HtmlTextWriter(sw); Page page = new Page(); HtmlForm form = new HtmlForm(); GridView1.EnableViewState = false; rptMlist.EnableViewState = false; myGridView.EnableViewState = false; page.EnableEventValidation = false; page.EnableViewState = false; // Realiza las inicializaciones de la instancia de la clase Page que requieran los diseñadores RAD. page.DesignerInitialize(); page.Controls.Add(form); form.Controls.Add(GridView1); form.Controls.Add(rptMlist); form.Controls.Add(myGridView); page.RenderControl(htw); Response.Clear(); Response.Buffer = true; Response.ContentType = "application/vnd.ms-word"; Response.AddHeader("Content-Disposition", "attachment;filename=" + PID + ".doc"); Response.Charset = "UTF-8"; Response.ContentEncoding = Encoding.Default; Response.Write(sb.ToString()); Response.End(); }
代码很简单,如果大家有更好的方法可以探讨一下。
时间: 2024-10-05 17:38:46