C#如何打印word模板

C#如何打印word模板
C#如何打印word模板

C#如何打印word模板

using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using Microsoft.Office; using Microsoft.Office.Core; using Microsoft.Office.Interop; using Microsoft.Office.Interop.Word; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void Button1_Click(object sender, EventArgs e) { string[] demostr ={ "测试数据1", "afsdasd", "fasdfdf", "sdfasdfee33", "asdfddeegadg", "fee都受到" }; string url = "http://" + this.Request.Url.Authority + "/demo/全国工业产品生产许可证申请登记表.doc"; string urlreadword = "/demo/Officebak/" + SetDocumentBookmarkData(url, demostr, Guid.NewGuid().ToString()); Page.RegisterStartupScript("msg", ""); } //设定标签的数据public string SetDocumentBookmarkData(string FileName, string [] demostr, string caseid) { //打开文档Microsoft.Office.Interop.Word.Document wDoc = null; Microsoft.Office.Interop.Word.Application wApp = null; this.OpenWordDoc(FileName, ref wDoc, ref wApp); object oEndOfDoc = "\\endofdoc"; object missing = System.Reflection.Missing.Value; //设定标签数据System.Collections.IEnumerator enu = wApp.ActiveDocument.Bookmarks.GetEnumerator(); string[] strbook = new string[demostr.Length ]; int i = 0; Microsoft.Office.Interop.Word.Bookmark bk = null; while (enu.MoveNext()) { bk = (Microsoft.Office.Interop.Word.Bookmark)enu.Current; if (https://www.360docs.net/doc/1415574359.html,.ToString().Trim() != "Table") { strbook[i] = https://www.360docs.net/doc/1415574359.html,.ToString(); i++; } } object tempobject = null; int length = 0; for (i = 0; i < strbook.Length; i++) { tempobject = strbook[i].ToString(); if (wApp.ActiveDocument.Bookmarks.Exists(strbook[i].ToString())) { wA pp.ActiveDocument.Bookmarks.get_Item(ref

tempobject).Select(); wApp.Selection.Text = demostr[i].ToString (); } } Microsoft.Office.Interop.Word.Table wordTablexSoft = wDoc.Tables[1]; InsertTabletoData(wordTablexSoft, ref wDoc, ref wApp); //收尾工作object o = null; //string guid = System.Guid.NewGuid().T oString(); string guid = caseid; object sFileName = Server.MapPath("/demo/Officebak/" + guid + ".doc"); if (wDoc.SaveFormat == (int)Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatDocument) { wDoc.Application.ActiveDocument.SaveAs(ref sFileName, 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, ref missing, ref missing); } wDoc.Close(ref

missing, ref missing, ref missing); wApp.Quit(ref missing, ref missing, ref missing); if (wDoc != null) { System.Runtime.InteropServices.Marshal.ReleaseComObject(wDoc ); wDoc = null; } if (wApp != null) { System.Runtime.InteropServices.Marshal.ReleaseComObject(wApp ); wApp = null; } GC.Collect(); return guid + ".doc"; } #region 打开word模板和word文件public static void OpenWordDot() { //不建议用非标准doc 此处方法略} private void OpenWordDoc(string FileName, ref Microsoft.Office.Interop.Word.Document wDoc, ref Microsoft.Office.Interop.Word.Application WApp) { if (FileName == "") return; Microsoft.Office.Interop.Word.Document thisDocument = null; Microsoft.Office.Interop.Word.FormFields formFields = null; Microsoft.Office.Interop.Word.Application thisApplication = new Microsoft.Office.Interop.Word.ApplicationClass(); thisApplication.Visible = true; thisApplication.Caption = ""; thisApplication.Options.CheckSpellingAsYouType = false; thisApplication.Options.CheckGrammarAsYouType = false; Object filename = FileName; Object ConfirmConversions = false; Object ReadOnly = true; Object AddT oRecentFiles = false; Object PasswordDocument = System.Type.Missing; Object PasswordTemplate = System.Type.Missing; Object Revert = System.Type.Missing; Object WritePasswordDocument = System.Type.Missing; Object WritePasswordTemplate = System.Type.Missing; Object Format = System.Type.Missing; Object Encoding = System.Type.Missing; Object Visible = System.Type.Missing; Object OpenAndRepair = System.Type.Missing; Object DocumentDirection = System.Type.Missing; Object NoEncodingDialog = System.Type.Missing; Object XMLTransform = System.Type.Missing; try { Microsoft.Office.Interop.Word.Document wordDoc = thisApplication.Documents.Open(ref filename, ref ConfirmConversions, ref ReadOnly, ref AddToRecentFiles, ref PasswordDocument, ref PasswordTemplate, ref Revert, ref WritePasswordDocument, ref WritePasswordTemplate, ref Format, ref Encoding, ref Visible, ref OpenAndRepair, ref DocumentDirection, ref NoEncodingDialog, ref XMLTransform); thisDocument = wordDoc; wDoc = wordDoc; WApp = thisApplication; formFields = wordDoc.FormFields; } catch (Exception ex) { } } #endregion ///

/// 在objtbale 标签上新增表/// /// /// /// /// private bool InsertTabletoData(Microsoft.Office.Interop.Word.Table wordTable, ref Microsoft.Office.Interop.Word.Document wDoc, ref Microsoft.Office.Interop.Word.Application WApp) { object Rownum =

5; object Columnnum = 1; wordTable.Cell(18, 2).Split(ref Rownum, ref Columnnum); wordTable.Cell(18, 3).Split(ref Rownum, ref Columnnum); wordTable.Cell(18, 4).Split(ref Rownum, ref Columnnum); wordTable.Cell(18, 5).Split(ref Rownum, ref Columnnum); for (int i = 0; i < 5; 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 + "行"; wordTable.Cell(18 + i, 5).Range.Text = "测试4列" + i + "行"; } return true; } ///

/// 在objtbale 标签上新增表/// /// /// /// /// private bool InsertT abletoBookmark(string objTable, ref Microsoft.Office.Interop.Word.Document wDoc, ref Microsoft.Office.Interop.Word.Application WApp) { object oEndOfDoc = "\\endofdoc"; object missing = System.Reflection.Missing.Value; object objBookmark = objT able; WApp.ActiveDocument.Bookmarks.get_Item(ref objBookmark).Select(); Microsoft.Office.Interop.Word.Table table = wDoc.Tables.Add(WApp.Selection.Range, 3, 4, ref missing, ref missing); table.Cell(1, 1).Range.T ext = "表:" + WApp.ActiveDocument.Bookmarks.get_Item(ref

objBookmark).Range.Tables[1].Rows.Count; return true; } }

相关主题
相关文档
最新文档