您的位置:首页 > 其它

利用word模板生成word或pdf文件并输出

2010-12-03 14:55 381 查看
利用模板文件生成临时word文件,保存后,输出到客户端,再删除生成的临时word文件

/// <summary>
/// 首先创建文档,插入书签,保存为dot格式的word模版文件
/// </summary>
/// <param name="temppath">模版绝对路径2003 dot模版</param>
/// <param name="outputfilefullname">输出文件绝对路径名.doc</param>
/// <param name="markvalue">书签名及对应的书签值</param>
public static void CreateWordDoc(string temppath, string exportFilefullname, List<KeyValuePair<string, string>> markvalues)
{
Object Nothing = System.Reflection.Missing.Value;
object outputfullfilename = exportFilefullname; //文件保存绝对全路径

Microsoft.Office.Interop.Word.ApplicationClass appWord = null;      //应用程序
Microsoft.Office.Interop.Word.DocumentClass doc = null;             //文档

try
{
appWord = new ApplicationClass();
appWord.Visible = false;

object objTrue = true;
object objFalse = false;
object objTemplate = temppath;      //模板路径
object objDocType = WdDocumentType.wdTypeDocument;

doc = (DocumentClass)appWord.Documents.Add(ref objTemplate, ref objFalse, ref objDocType, ref objTrue);

//给书签赋值
object mark;
foreach (KeyValuePair<string, string> pair in markvalues)
{
mark = pair.Key;
if (doc.Bookmarks.Exists(pair.Key))
{
doc.Bookmarks.get_Item(ref mark).Range.Text = pair.Value;
}
}

//生成word并保存
object miss = System.Reflection.Missing.Value;
object fileformat = (int)Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatDocument;

//若希望输出pdf格式文件,则更改此处,同时修改相应的输出文件绝对路径名为pdf格式
//object fileformat = (int)Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatPDF;
doc.SaveAs(ref outputfullfilename, ref fileformat, ref miss, ref miss, ref miss, ref miss, ref miss,
ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss);

//退出模版不保存更改
object missingValue = System.Reflection.Missing.Value;
object doNotSaveChanges = WdSaveOptions.wdDoNotSaveChanges;

doc.Close(ref doNotSaveChanges, ref missingValue, ref missingValue);
appWord.Application.Quit(ref miss, ref miss, ref miss);
doc = null;
appWord = null;

}
catch (System.Exception ex)
{
//捕捉异常,如果出现异常则清空实例,退出word,同时释放资源
object miss = System.Reflection.Missing.Value;
object doNotSaveChanges = WdSaveOptions.wdDoNotSaveChanges;
doc.Close(ref doNotSaveChanges, ref miss, ref miss);
appWord.Application.Quit(ref miss, ref miss, ref miss);
doc = null;
appWord = null;
}
}


输出保存的word文档

/// <summary>
/// 说明:导出本地word文件类型
/// </summary>
/// <param name="page"></param>
/// <param name="filefullname">临时word文件地址</param>
public static  void ExportWordFile(System.Web .UI .Page page, string filefullname,bool deleteAfterExport,bool endResponse)
{
if (File.Exists(filefullname))
{
FileInfo fileinfo = new FileInfo(filefullname);
page.Response.Clear();
page.Response.Buffer = true;
page.Response.Charset = "utf-8";
page.Response.ContentEncoding = System.Text.Encoding.UTF8;
page.Response.ContentType = "application/ms-word";
page.Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileinfo.Name, Encoding.UTF8));

byte[] sw = File.ReadAllBytes(Convert.ToString(filefullname));
page.Response.BinaryWrite(sw);
page.Response.Flush();

if (deleteAfterExport)
{
File.Delete(filefullname);
}
if (endResponse)
{
page.Response.End();
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: