c#读取Word模板,利用书签替换内容包括表格

 //生成WORD程序对象和WORD文档对象
Microsoft.Office.Interop.Word.Application appWord = new Microsoft.Office.Interop.Word.Application();
Microsoft.Office.Interop.Word.Document doc = new Document();
object miss = System.Reflection.Missing.Value; try
{
//打开模板文档,并指定doc的文档类型
//object objTemplate = System.Windows.Forms.Application.StartupPath + @"\UploadFiles\tz103.doc"; //路径一定要正确 object objTemplate = @"c:\\测试.docx"; object objDocType = WdDocumentType.wdTypeDocument; object objfalse = false; object objtrue = true; doc = (Document)appWord.Documents.Add(ref objTemplate, ref objfalse, ref objDocType, ref objtrue); //获取模板中所有的书签 Bookmarks odf = doc.Bookmarks; string[] testTableremarks = { "FirstParty", "SecondParty", "FirstPartySign", "SecondPartySign" }; string[] testTablevalues = { "嘉实多(深圳)有限公司广州分公司", "广州嘉通", "嘉实多(深圳)有限公司广州分公司", "广州嘉通贸易有限公司" }; //循环所有的书签,并给书签赋值 for (int oIndex = ; oIndex < testTableremarks.Length; oIndex++)
{ object obDD_Name = ""; obDD_Name = testTableremarks[oIndex]; //doc.Bookmarks.get_Item(ref obDD_Name).Range.Text = p_TestReportTable.Rows[0][testTablevalues[oIndex]].ToString();//此处Range也是WORD中很重要的一个对象,就是当前操作参数所在的区域 odf.get_Item(ref obDD_Name).Range.Text = testTablevalues[oIndex]; } //附件,插入表格
//这里简单生成样例数据表,工作中要以实际的数据集为准
System.Data.DataTable dt = new System.Data.DataTable();
dt.Columns.Add("name", typeof(string));
dt.Columns.Add("age", typeof(string)); DataRow dr = dt.NewRow();
dr["name"] = "姓名"; dr["age"] = "年龄";
dt.Rows.Add(dr); dr = dt.NewRow();
dr["name"] = "张三"; dr["age"] = "";
dt.Rows.Add(dr); dr = dt.NewRow();
dr["name"] = "李四"; dr["age"] = "";
dt.Rows.Add(dr); //附件一
object obAttachMent = "Attachment1";
//创建Word表格,并指定标签
Microsoft.Office.Interop.Word.Table dtWord = doc.Tables.Add(odf.get_Item(ref obAttachMent).Range, dt.Rows.Count, dt.Columns.Count); dtWord.Borders.InsideLineStyle = WdLineStyle.wdLineStyleDot;
dtWord.Borders.OutsideLineStyle = WdLineStyle.wdLineStyleDot; //循环往表格里赋值
for (int i = ; i <= dt.Rows.Count; i++)
{
for (int j = ; j <= dt.Columns.Count; j++)
{
dtWord.Rows[i].Cells[j].Range.Text = dt.Rows[i - ][j - ].ToString();
}
} //第四步 生成word,将当前的文档对象另存为指定的路径,然后关闭doc对象。关闭应用程序 object filename = "c:\\" + DateTime.Now.ToString("yyyy-MM-dd") + ".docx";//HttpContext.Current.Server.MapPath("f:\\") + "Testing_" + DateTime.Now.ToShortDateString() + ".doc";
object Password = "P@55w0rd"; //对Word文档进行加密保护,不允许编辑
if (Password !=null)
{
doc.Protect(WdProtectionType.wdAllowOnlyReading, ref objfalse, ref Password, ref miss, ref miss);
} doc.SaveAs(ref filename, 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, ref miss); 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; MessageBox.Show("生成成功!"); System.Diagnostics.Process.Start(filename.ToString());//打开文档 }
catch (Exception)
{
doc.Close(ref miss, ref miss, ref miss);
appWord.Application.Quit(ref miss, ref miss, ref miss);
doc = null;
appWord = null;
}
上一篇:java设计模式之建造者模式


下一篇:Java设计模式之三 ----- 建造者模式和原型模式