整理的c#操作OFFICE的方法一

 导入COM库:Microsoft word 11.0 Object Library.
引用里面就增加了:

创建新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:\CCCXCXX\TestDoc.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:\XXXCCX\Test.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);

            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);
= 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();



 

复制模板文件并操作书签:

            foreach (System.Diagnostics.Process p in System.Diagnostics.Process.GetProcessesByName("WINWORD"))
            {
                p.Kill();
            }

            app = new Microsoft.Office.Interop.Word.Application();
            //模板文件  
            string TemplateFile = @"D:\土地转让合同.doc";

            //生成的具有模板样式的新文件  
            string Fname = "土地转让合同" + DateTime.Now.ToString("mmss") + ".doc";
            string FileName = @"D:\" + Fname;

            //FileName = Server.MapPath("~/UpLoad/TestWord/" + Fname );  
            //模板文件拷贝到新文件 
            File.Copy(TemplateFile, FileName);

            object Obj_FileName = FileName;
            object Visible = false;
            object ReadOnly = false;
            object missing = System.Reflection.Missing.Value;

            //打开文件  
            doc = app.Documents.Open(ref Obj_FileName,
            ref missing, ref ReadOnly, ref missing,
            ref missing, ref missing, ref missing, ref missing,
            ref missing, ref missing, ref missing, ref Visible,
            ref missing, ref missing, ref missing,ref missing);

            doc.Activate();
            //C#操作Word书签 
            foreach (Microsoft.Office.Interop.Word.Bookmark bm in doc.Bookmarks)
            {
                switch (bm.Name)
                {
                    case "a":
                        bm.Select();
                        bm.Range.Text = "a";
                        break;
                    case "b":
                        bm.Select();
                        bm.Range.Text = "b";
                        break;
                    default:
                        break;
                }
            }
            object IsSave = true;
            doc.Close(ref IsSave, ref missing, ref missing);

打印文件
            foreach (System.Diagnostics.Process p in System.Diagnostics.Process.GetProcessesByName("WINWORD"))
            {
                p.Kill();
            }

            object path = txtFile.Text;
            object missing = System.Reflection.Missing.Value;
            object readOnly = true;

            app = new Microsoft.Office.Interop.Word.Application();
            doc = app.Documents.Open(ref path, ref missing, ref readOnly, ref missing,
                ref missing, ref missing, ref missing, ref missing,
                ref missing, ref missing, ref missing, ref missing, ref missing,
                ref missing, ref missing, ref missing);

            //打印预览
            app.Visible = true;
            doc.PrintPreview();

            //后台打印
            //object copies = 1;
            //object background = true;

            //doc.Activate();
            //doc.PrintOut(ref background, ref missing, ref missing, ref missing, ref missing,
            //    ref missing, ref missing, ref copies, ref missing, ref missing, ref missing,
            //    ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
            //    ref missing);
            //doc.Close(ref missing, ref missing, ref missing);

 
隐藏WORD的某些控件:

            object oMissing = System.Reflection.Missing.Value;
            object readOnly = true;

            Microsoft.Office.Interop.Word._Application oWord;
            Microsoft.Office.Interop.Word._Document oDoc;
            oWord = new Microsoft.Office.Interop.Word.Application();
            oWord.Visible = true;
            object fileName = Server.MapPath(@"~/Doc/auditTable.doc");

            oDoc = oWord.Documents.Open(ref fileName,
            ref oMissing, ref readOnly, 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);

            oWord.Visible = true;
            for (int i = 1; i < oWord.ActiveDocument.ActiveWindow.Application.CommandBars.Count; i++)
            {
                oWord.ActiveDocument.ActiveWindow.Application.CommandBars[i].Enabled = false;
            }
            oDoc.Activate();
            //可以利用上面的循环输出所有的菜单名,ID,以及其control的名和ID再进行针对性的隐藏
            //(索引号是用其index,而不是用ID,要注意)

注意:调用OFFICE,COM控件进行操作时,是调用服务端的资源进行的,就是说其打开,打印等其实是调用了服务端的资料进行操作,所以对于BS的系统来说,要特别注意,一般用其生成文件、替换文字等操作效率是不错的,因为直接在服务端操作了。对于CS来说就全部功能都可以操作。
然后BS操作OFFICE,用WEBOFFICE插件还是不错的,经过亲身使用,使用方法很简便,控制起来很很简单,功能也很齐全,对于文件的新增,在线修改,打印,功能功制等都很方便实现。具体的可搜索相关资料。

实现C#Word文档替换操作的方式:

使用文档(Document )对象的 Content 属性选择整个文档。

 
 
  1.  ///﹤summary﹥  
  2. /// 浅析C#Word文档替换操作,在word 中查找一个字符串直接替换所需要的文本  
  3. /// ﹤/summary﹥  
  4. /// ﹤param name="strOldText"﹥原文本﹤/param﹥  
  5. /// ﹤param name="strNewText"﹥新文本﹤/param﹥  
  6. /// ﹤returns﹥﹤/returns﹥  
  7. public bool Replace(string strOldText,string strNewText)  
  8. {  
  9. this.oDoc.Content.Find.Text = strOldText ;  
  10. object FindText,  ReplaceWith, Replace ;//   
  11. object MissingValue = Type.Missing;   
  12. FindText = strOldText ;//要查找的文本  
  13. ReplaceWith = strNewText ;//替换文本  
  14.    Replace = Word.WdReplace.wdReplaceAll ;  
  15. /**//*wdReplaceAll - 替换找到的所有项。  
  16.   * wdReplaceNone - 不替换找到的任何项。  
  17. * wdReplaceOne - 替换找到的第一项。  
  18. * */ 
  19. this.oDoc.Content.Find.ClearFormatting();  
  20. //移除Find的搜索文本和段落格式设置  
  21. if (this.oDoc.Content.Find.Execute(  
  22. ref FindText,ref MissingValue,  
  23. ref MissingValue,ref MissingValue,  
  24. ref MissingValue,ref MissingValue,  
  25. ref MissingValue,ref MissingValue,ref MissingValue,  
  26. ref ReplaceWith,ref Replace,  
  27. ref MissingValue,ref MissingValue,  
  28. ref MissingValue,ref MissingValue))  
  29. {  
  30. return true ;  
  31. }  
  32. return false ;  
  33.  
  34. }  

说明:其中oDoc是一个word文档的Document对象.

此外还可以运用Word Application 对象Selection的Find实现浅析C#Word文档替换操作.

 
 
  1. public bool SearchReplace(string strOldText,string strNewText)  
  2. {   
  3. object replaceAll = Word.WdReplace.wdReplaceAll;   
  4. object missing = Type.Missing;   
  5.  
  6. //首先清除任何现有的格式设置选项,然后设置搜索字符串 strOldText。  
  7. this.oWordApplic.Selection.Find.ClearFormatting();   
  8. oWordApplic.Selection.Find.Text = strOldText;   
  9.  
  10. oWordApplic.Selection.Find.Replacement.ClearFormatting();   
  11. oWordApplic.Selection.Find.Replacement.Text = strNewText;   
  12.  
  13. if (oWordApplic.Selection.Find.Execute(  
  14. ref missing, ref missing,   
  15. ref missing, ref missing, ref missing,   
  16. ref missing, ref missing,   
  17. ref missing, ref missing, ref missing,  
  18. ref replaceAll, ref missing,   
  19. ref missing, ref missing, ref missing))  
  20. {  
  21. return true ;  
  22. }  
  23. return false ;  
  24. }  

注:oWordApplic是一个Word Application 对象

当然也可以使用word文档的书签BookMark.使用 Bookmark 的 Range 属性可将文本插入占位符书签,以便能够在以后检索文本,或替换已包含文本的书签中的文本。


 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值