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

利用模板文件生成临时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();
}
}
}

 

 

 


 

 

转载于:https://www.cnblogs.com/niumeng/archive/2010/12/03/1895497.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值