现在的位置: 首页 > 综合 > 正文

win form 操作word模板插入文字、图片及表格

2012年12月14日 ⁄ 综合 ⁄ 共 4274字 ⁄ 字号 评论关闭

 

源地址:http://apps.hi.baidu.com/share/detail/17026118

1.建立word模板文件 person.dot用书签 标示相关字段的填充位置

2.建立web应用程序 加入Microsoft.Office.Interop.Word

引用具体添加引用请参看http://www.microsoft.com/china/msdn/library/office/office/OfficePrIntopAssFAQ.mspx?mfr=true3.相关示例代码

 

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;
            }
        }

 

-----
以上代码在运行时 如遭遇80070005错误

解决方法一:
控制面板-》管理工具-》组件服务-》计算机-》我的电脑-》DCom配置-》找到Microsoft Word文档
之后
单击属性打开此应用程序的属性对话框。
2. 单击标识选项卡,然后选择交互式用户。
3.单击"安全"选项卡,分别在"启动和激活权限"和"访问权限"组中选中"自定义",然后
自定义->编辑->添加ASP.NET账户和IUSER_计算机名
4. 确保允许每个用户访问,然后单击确定。
5. 单击确定关闭 DCOMCNFG。

解决方法二:
如果上述方法不能解决问题,就应该是权限问题,请尝试用下面的方法:
在web.config中使用身份模拟,在<system.web>节中加入 <identity impersonate="true" userName="你的用户名

" password="密码"/>
</system.web>

附:图片的详细操作

object filename = @"C:\Inetpub\wwwroot\TestWebApp\test.doc";//文件名 
Word.Application a = new Word.ApplicationClass();//建立一个Word程序对像 
object Nothing = System.Reflection.Missing.Value;//空值 
Word.Document b = a.Documents.Open(ref filename,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing);//建立一个Word文档对像 

//其实这步就是执行了这个宏 
InlineShape shape = a.Selection.InlineShapes.AddPicture(@"C:\Documents and Settings\Administrator\桌面\2003121512223366481.jpg",ref Nothing,ref Nothing,ref Nothing); 

shape.Height = InchesToPoints(0.5) 
shape.Width = InchesToPoints(0.5) 

//Selection.InlineShapes.AddPicture FileName:= "C:\Documents and Settings\Administrator\桌面\2003121512223366481.bmp", LinkToFile:=False, SaveWithDocument:=True End Sub b.Save();//保存 
b.Close(ref Nothing,ref Nothing,ref Nothing);//关闭Word文档 
a.Quit(ref Nothing,ref Nothing,ref Nothing);//退出Word程序

 

抱歉!评论已关闭.