您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 管理学资料 > 1C#操作WORD标签
C#操作word标签(精)专题—Office操作2009-05-1510:06:06阅读147评论0字号:大中小usingSystem;usingSystem.Data;usingSystem.Configuration;usingSystem.Web;usingSystem.Web.Security;usingSystem.Web.UI;usingSystem.Web.UI.WebControls;usingSystem.Web.UI.WebControls.WebParts;usingSystem.Web.UI.HtmlControls;usingMicrosoft.Office;usingMicrosoft.Office.Core;usingMicrosoft.Office.Interop;usingMicrosoft.Office.Interop.Word;publicpartialclass_Default:System.Web.UI.Page{protectedvoidPage_Load(objectsender,EventArgse){}protectedvoidButton1_Click(objectsender,EventArgse){string[]demostr={测试数据1,afsdasd,fasdfdf,sdfasdfee33,asdfddeegadg,fee都受到};stringurl=http://+this.Request.Url.Authority+/demo/全国工业产品生产许可证申请登记表.doc;stringurlreadword=/demo/Officebak/+SetDocumentBookmarkData(url,demostr,Guid.NewGuid().ToString());Page.RegisterStartupScript(msg,scriptwindow.location='+urlreadword+';/script);}//设定标签的数据publicstringSetDocumentBookmarkData(stringFileName,string[]demostr,stringcaseid){//打开文档Microsoft.Office.Interop.Word.DocumentwDoc=null;Microsoft.Office.Interop.Word.ApplicationwApp=null;this.OpenWordDoc(FileName,refwDoc,refwApp);objectoEndOfDoc=\\endofdoc;objectmissing=System.Reflection.Missing.Value;//设定标签数据System.Collections.IEnumeratorenu=wApp.ActiveDocument.Bookmarks.GetEnumerator();string[]strbook=newstring[demostr.Length];inti=0;Microsoft.Office.Interop.Word.Bookmarkbk=null;while(enu.MoveNext()){bk=(Microsoft.Office.Interop.Word.Bookmark)enu.Current;if(bk.Name.ToString().Trim()!=Table){strbook[i]=bk.Name.ToString();i++;}}objecttempobject=null;intlength=0;for(i=0;istrbook.Length;i++){tempobject=strbook[i].ToString();if(wApp.ActiveDocument.Bookmarks.Exists(strbook[i].ToString())){wApp.ActiveDocument.Bookmarks.get_Item(reftempobject).Select();wApp.Selection.Text=demostr[i].ToString();}}Microsoft.Office.Interop.Word.TablewordTablexSoft=wDoc.Tables[1];InsertTabletoData(wordTablexSoft,refwDoc,refwApp);//收尾工作objecto=null;//stringguid=System.Guid.NewGuid().ToString();stringguid=caseid;objectsFileName=Server.MapPath(/demo/Officebak/+guid+.doc);if(wDoc.SaveFormat==(int)Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatDocument){wDoc.Application.ActiveDocument.SaveAs(refsFileName,refmissing,refmissing,refmissing,refmissing,refmissing,refmissing,refmissing,refmissing,refmissing,refmissing,refmissing,refmissing,refmissing,refmissing,refmissing);}wDoc.Close(refmissing,refmissing,refmissing);wApp.Quit(refmissing,refmissing,refmissing);if(wDoc!=null){System.Runtime.InteropServices.Marshal.ReleaseComObject(wDoc);wDoc=null;}if(wApp!=null){System.Runtime.InteropServices.Marshal.ReleaseComObject(wApp);wApp=null;}GC.Collect();returnguid+.doc;}#region打开word模板和word文件publicstaticvoidOpenWordDot(){//不建议用非标准doc此处方法略}privatevoidOpenWordDoc(stringFileName,refMicrosoft.Office.Interop.Word.DocumentwDoc,refMicrosoft.Office.Interop.Word.ApplicationWApp){if(FileName==)return;Microsoft.Office.Interop.Word.DocumentthisDocument=null;Microsoft.Office.Interop.Word.FormFieldsformFields=null;Microsoft.Office.Interop.Word.ApplicationthisApplication=newMicrosoft.Office.Interop.Word.ApplicationClass();thisApplication.Visible=true;thisApplication.Caption=;thisApplication.Options.CheckSpellingAsYouType=false;thisApplication.Options.CheckGrammarAsYouType=false;Objectfilename=FileName;ObjectConfirmConversions=false;ObjectReadOnly=true;ObjectAddToRecentFiles=false;ObjectPasswordDocument=System.Type.Missing;ObjectPasswordTemplate=System.Type.Missing;ObjectRevert=System.Type.Missing;ObjectWritePasswordDocument=System.Type.Missing;ObjectWritePasswordTemplate=System.Type.Missing;ObjectFormat=System.Type.Missing;ObjectEncoding=System.Type.Missing;ObjectVisible=System.Type.Missing;ObjectOpenAndRepair=System.Type.Missing;ObjectDocumentDirection=System.Type.Missing;ObjectNoEncodingDialog=System.Type.Missing;ObjectXMLTransform=System.Type.Missing;try{Microsoft.Office.Interop.Word.DocumentwordDoc=thisApplication.Documents.Open(reffilename,refConfirmConversions,refReadOnly,refAddToRecentFiles,refPasswordDocument,refPasswordTemplate,refRevert,refWritePasswordDocument,refWritePasswordTemplate,refFormat,refEncoding,refVisible,refOpenAndRepair,refDocumentDirection,refNoEncodingDialog,refXMLTransform);thisDocument=wordDoc;wDoc=wordDoc;WApp=thisApplication;formFields=wordDoc.FormFields;}catch(Exceptionex){}}#endregion///summary///在objtbale标签上新增表////summary///paramname=objTable/param///paramname=wDoc/param///paramname=WApp/param///returns/returnsprivateboolInsertTabletoData(Microsoft.Office.Interop.Word.TablewordTable,refMicrosoft.Office.Interop.Word.DocumentwDoc,refMicrosoft.Office.Interop.Word.ApplicationWApp){objectRownum=5;objectColumnnum=1;wordTable.Cell(18,2).Split(refRownum,refColumnnum);wordTable.Cell(18,3).Split(refRownum,refColumnnum);wordTable.Cell(18,4).Split(refRownum,refColumnnum);wordTable.Cell(18,5).Split(refRownum,refColumnnum);for(inti=0;i5;i++){wordTable.Cell(18+i,2).Range.Text=测试1列+i+行;wordTable.Cell(18+i,3).Range.Text=测试2列+i+行;wordTable.Cell(18+i,4).Range.Text=测试3列+i+行;w
本文标题:1C#操作WORD标签
链接地址:https://www.777doc.com/doc-4014580 .html