在最近由于项目需要生成Word报告,并且报告样式必须符合客户要求,为了便于系统的升级和维护,编写了一个使用Word模板生成报告的工具类,其使用相对比较麻烦,用到了外部的.net开发包,并需要使用_Application类和_Document类提供的各种方法,在此总结一下,以备以后使用。在和同事沟通的过程中,找到了两个解决方案
1.先通过程序生成报表样式的HTML页面,然后修改HTML页面的后缀名为DOC。
2.定制WORD文档的模板文件,在C中操作WORD模板,生成新的WORD文档。
第一方案简单,只需要改动文件的扩展名就行了,但是也存在了一些问题,譬如生成的WORD文档样式的丢失。这样对于客户来说可能是一个无法通过的方案。
第二方案比较复杂,需要调用OFFICE的WORD组件通过C来操作WORD,进而生成WORD。此方法类似于我们在c中的后台拼接数据。虽然麻烦,但是能够灵活定制,只不过是操作WORD对象而已。 经过再三考虑:决定用第二种方法来生成WORD报告文档。
通过自己的实践,这个需求总算是搞定了,在实际开发的过程中,遇到了这样那样的问题,还好,通过不断的查找网络资源,结合实际开发中的情况,问题都得到了解决。
现将本人在开发过程中的一些理解与经验总结一下:
在VS2008平台下,引用.net-Microsoft.Office.Interop.Word.12,这样就可以在程序用操作WORD对象了。 通过简单执行,报了80070005错误这个错误是因为权限不够,需要在DCOM配置中更改.net和IIS用户的操作权限,具体修改过程如下:
解决方法一:
1.控制面板-》管理工具-》组件服务-》计算机-》我的电脑-》DCom配置-》找到Microsoft Word文档之后,单击属性打开此应 用程序的属性对话框。
2.单击标识选项卡,然后选择交互式用户。
3.单击安全选项卡分别在启动和激活权限和访问权限组中选中自定义然后自定义-编辑-添加ASP.NET账户和IUSER_计算机 名。
4. 确保允许每个用户访问,然后单击确定。
5. 单击确定关闭 DCOMCNFG。
如果上述方法不能解决问题就应该是权限问题请尝试用下面的方法:
在web.config中使用身份模拟在节中加入
解决了上述问题,开始考虑如何创建WORD模板文件,WORD的模板文件其实就是通过书签来添加内容的。也就是通过在WORD文档中创建书签,然后在程序中获取模板文件的所有书签,通过给书签赋值来进行文档生成的。 在程序中的操作流程如下: 声明WORD程序的对象 → 声明一个WORD文档对象 → 获取当前的操作文档对象 → 获取文档所有的书签 → 将数据库数据赋值到对应的书签 → 将文档另存为指定的文件夹下.
创建新Word
object oMissing = System.Reflection.Missing.Value;
Word._Application oWord;
Word._Document oDoc;
oWord = new Word.Application();
oWord.Visible = true;
oDoc = oWord.Documents.Add(ref oMissing, ref oMissing,
ref oMissing, ref oMissing);
打开文档:
object oMissing = System.Reflection.Missing.Value;
Word._Application oWord;
Word._Document oDoc;
oWord = new Word.Application();
oWord.Visible = true;
object fileName = @"E:CCCXCXXTestDoc.doc";
oDoc = oWord.Documents.Open(ref fileName,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
导入模板
object oMissing = System.Reflection.Missing.Value;
Word._Application oWord;
Word._Document oDoc;
oWord = new Word.Application();
oWord.Visible = true;
object fileName = @"E:XXXCCXTest.doc";
oDoc = oWord.Documents.Add(ref fileName, ref oMissing,
ref oMissing, ref oMissing);
.添加新表
object oMissing = System.Reflection.Missing.Value;
Word._Application oWord;
Word._Document oDoc;
oWord = new Word.Application();
oWord.Visible = true;
oDoc = oWord.Documents.Add(ref oMissing, ref oMissing,
ref oMissing, ref oMissing);
object start = 0;
object end = 0;
Word.Range tableLocation = oDoc.Range(ref start, ref end);
oDoc.Tables.Add(tableLocation, 3, 4, ref oMissing, ref oMissing);
.表插入行
object oMissing = System.Reflection.Missing.Value;
Word._Application oWord;
Word._Document oDoc;
oWord = new Word.Application();
oWord.Visible = true;
oDoc = oWord.Documents.Add(ref oMissing, ref oMissing,
ref oMissing, ref oMissing);
object start = 0;
object end = 0;
Word.Range tableLocation = oDoc.Range(ref start, ref end);
oDoc.Tables.Add(tableLocation, 3, 4, ref oMissing, ref oMissing);
Word.Table newTable = oDoc.Tables[1];
object beforeRow = newTable.Rows[1];
newTable.Rows.Add(ref beforeRow);
.单元格合并
object oMissing = System.Reflection.Missing.Value;
Word._Application oWord;
Word._Document oDoc;
oWord = new Word.Application();
oWord.Visible = true;
oDoc = oWord.Documents.Add(ref oMissing, ref oMissing,
ref oMissing, ref oMissing);
object start = 0;
object end = 0;
Word.Range tableLocation = oDoc.Range(ref start, ref end);
oDoc.Tables.Add(tableLocation, 3, 4, ref oMissing, ref oMissing);
Word.Table newTable = oDoc.Tables[1];
object beforeRow = newTable.Rows[1];
newTable.Rows.Add(ref beforeRow);
Word.Cell cell = newTable.Cell(1, 1);
cell.Merge(newTable.Cell(1, 2));
.单元格分离
object oMissing = System.Reflection.Missing.Value;
Word._Application oWord;
Word._Document oDoc;
oWord = new Word.Application();
oWord.Visible = true;
oDoc = oWord.Documents.Add(ref oMissing,
ref oMissing, ref oMissing);
object start = 0;
object end = 0;
Word.Range tableLocation = oDoc.Range(ref start, ref end);
oDoc.Tables.Add(tableLocation, 3, 4, ref oMissing, ref oMissing);
Word.Table newTable = oDoc.Tables[1];
object beforeRow = newTable.Rows[1];
newTable.Rows.Add(ref beforeRow);
Word.Cell cell = newTable.Cell(1, 1);
cell.Merge(newTable.Cell(1, 2));
object Rownum = 2;
object Columnnum = 2;
cell.Split(ref Rownum, ref Columnnum);
通过段落控制插入
object oMissing = System.Reflection.Missing.Value;
object oEndOfDoc = "\endofdoc"; /**//* endofdoc is a predefined bookmark */
//Start Word and create a new document.
Word._Application oWord;
Word._Document oDoc;
oWord = new Word.Application();
oWord.Visible = true;
oDoc = oWord.Documents.Add(ref oMissing, ref oMissing,
ref oMissing, ref oMissing);
//Insert a paragraph at the beginning of the document.
Word.Paragraph oPara1;
oPara1 = oDoc.Content.Paragraphs.Add(ref oMissing);
oPara1.Range.Text = "Heading 1";
oPara1.Range.Font.Bold = 1;
oPara1.Format.SpaceAfter = 24; //24 pt spacing after paragraph.
oPara1.Range.InsertParagraphAfter();