C#操作word模板插入文字、图片及表格详细步骤

2.建立web应用程序 加入Microsoft.Office.Interop.Word引用
具体添加引用请参看
?mfr=true

3.相关示例代码

protected void CreateReport_Click(object sender, EventArgs e) { Microsoft.Office.Interop.Word.Application appWord = null;//应用程序 Microsoft.Office.Interop.Word.DocumentClass doc = null;//文档 try { appWord = new Microsoft.Office.Interop.Word.Application(); appWord.Visible = false; object objTrue = true; object objFalse = false; object objTemplate = Server.MapPath("person.dot");//模板路径 object objDocType = WdDocumentType.wdTypeDocument; doc = (DocumentClass)appWord.Documents.Add(ref objTemplate, ref objFalse, ref objDocType,ref objTrue); //第一步生成word文档 //定义书签变量 object obDD_Name = "bm_Name";//姓 名 object obDD_Sex = "bm_Sex";//性 别 object obDD_Birthday = "bm_Birthday"; //出生年月 object obpic="pic"; object obtable = "obtable"; object Nothing = System.Reflection.Missing.Value; //InlineShape shape = appWord.Selection.InlineShapes.AddPicture(@"F:\Picture\_DSC1602.JPG", ref Nothing, ref Nothing, ref Nothing); //第二步 读取数据,填充数据集 System.Data.DataTable dt = new DataTable(); dt.Columns.Add("p_Name"); dt.Columns.Add("p_Sex"); dt.Columns.Add("p_Birthday"); DataRow dr = dt.NewRow(); dr["p_Name"] = "张三"; dr["p_Sex"] = ""; dr["p_Birthday"] = "1980-01-01"; dt.Rows.Add(dr); //第三步 给书签赋值 //给书签赋值 doc.Bookmarks.get_Item(ref obDD_Name).Range.Text = dt.Rows[0]["p_Name"].ToString(); //姓 名 doc.Bookmarks.get_Item(ref obDD_Sex).Range.Text = dt.Rows[0]["p_Sex"].ToString();//性 别 doc.Bookmarks.get_Item(ref obDD_Birthday).Range.Text = dt.Rows[0]["p_Birthday"].ToString();//年龄 doc.Bookmarks.get_Item(ref obpic).Range.InlineShapes.AddPicture(@"F:\Picture\_DSC1602.JPG", ref Nothing, ref Nothing, ref Nothing); //文档中插入表格 //doc.Bookmarks.get_Item(ref obtable).Range.Tables.Add(doc.Bookmarks.get_Item(ref obtable).Range, 12, 3, ref Nothing, ref Nothing); Microsoft.Office.Interop.Word.Table newTable = doc.Tables.Add(doc.Bookmarks.get_Item(ref obtable).Range, 12, 3, ref Nothing, ref Nothing); newTable.Borders.OutsideLineStyle = WdLineStyle.wdLineStyleSingle; newTable.Borders.InsideLineStyle = WdLineStyle.wdLineStyleSingle; //给文档的最后一行再添加内容 doc.Paragraphs.Last.Range.Text = ""; //第四步 生成word object filename = Server.MapPath("~") + "\\BG\\" + dt.Rows[0]["p_Name"].ToString() + ".doc"; object miss = System.Reflection.Missing.Value; 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 missingValue = Type.Missing; 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,同时释放资源 string aa = ex.ToString(); object miss = System.Reflection.Missing.Value; object missingValue = Type.Missing; 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; } }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zggsgs.html