方法一:
页面:
<asp:Button ID="btnAdd" runat="server" Text="打 印" OnClick="btnAdd_Click" />
<div runat="server" id="divContent">内容。。。。</div>
后台:
protected void btnAdd_Click(object sender, EventArgs e)
{
outPutWord();
}
public void outPutWord()
{
Response.Clear();
Response.Buffer = true;
Response.Charset = "GB2312";
Response.AppendHeader("Content-Disposition", "attachment;filename=" + DateTime.Now.ToString("yyyyMMddhhss") + ".doc");
Response.ContentType = "application/ms-word";
this.EnableViewState = false;
System.IO.StringWriter swOut = new System.IO.StringWriter();
HtmlTextWriter hTw = new HtmlTextWriter(swOut);
divShow.RenderControl(hTw);
Response.Write(swOut.ToString());
Response.End();
}
方法二:
protected void btnAdd_Click(object sender, EventArgs e)
{
outPutWord();
}
public void outPutWord()
{
string tmppath = PermissionUtil.GetPrintPath("PrintDoc\\test.doc");
Document doc = new Document(tmppath); //载入模板
doc.Save(DateTime.Now.ToString("yyyyMMddhhmmss") + ".doc", SaveFormat.Doc, SaveType.OpenInWord, Response); //保存为doc,并打开
}
public static string GetPrintPath(string strPath)
{
return HttpContext.Current.Server.MapPath(HttpContext.Current.Request.ApplicationPath) + strPath;
}