• Word For Application By C#


      1 using System;
      2 using System.Data;
      3 using System.Configuration;
      4 using System.Web;
      5 using System.Web.Security;
      6 using System.Web.UI;
      7 using System.Web.UI.WebControls;
      8 using System.Web.UI.WebControls.WebParts;
      9 using System.Web.UI.HtmlControls;
     10 using Microsoft.Office;
     11 using Microsoft.Office.Core;
     12 using Microsoft.Office.Interop;
     13 using Microsoft.Office.Interop.Word;
     14 
     15 public partial class _Default : System.Web.UI.Page
     16 {
     17     protected void Page_Load(object sender, EventArgs e)
     18     {
     19 
     20     }
     21     protected void Button1_Click(object sender, EventArgs e)
     22     {
     23         string[] demostr = { "测试数据1""afsdasd""fasdfdf""sdfasdfee33""asdfddeegadg""fee都受到" };
     24         string url = "http://" + this.Request.Url.Authority + "/demo/全国工业产品生产许可证申请登记表.doc";
     25         string urlreadword = "/demo/Officebak/" + SetDocumentBookmarkData(url, demostr, Guid.NewGuid().ToString());
     26         Page.RegisterStartupScript("msg""<script>window.location='" + urlreadword + "';</script>");
     27     }
     28 
     29     //设定标签的数据   
     30     public string SetDocumentBookmarkData(string FileName, string[] demostr, string caseid)
     31     {
     32         //打开文档   
     33         Microsoft.Office.Interop.Word.Document wDoc = null;
     34         Microsoft.Office.Interop.Word.Application wApp = null;
     35         this.OpenWordDoc(FileName, ref wDoc, ref wApp);
     36         object oEndOfDoc = "\\endofdoc";
     37         object missing = System.Reflection.Missing.Value;
     38         //设定标签数据   
     39         System.Collections.IEnumerator enu = wApp.ActiveDocument.Bookmarks.GetEnumerator();
     40 
     41         string[] strbook = new string[demostr.Length];
     42         int i = 0;
     43         Microsoft.Office.Interop.Word.Bookmark bk = null;
     44         while (enu.MoveNext())
     45         {
     46             bk = (Microsoft.Office.Interop.Word.Bookmark)enu.Current;
     47 
     48             if (bk.Name.ToString().Trim() != "Table")
     49             {
     50                 strbook[i] = bk.Name.ToString();
     51                 i++;
     52             }
     53         }
     54 
     55         object tempobject = null;
     56         int length = 0;
     57         for (i = 0; i < strbook.Length; i++)
     58         {
     59             tempobject = strbook[i].ToString();
     60             if (wApp.ActiveDocument.Bookmarks.Exists(strbook[i].ToString()))
     61             {
     62                 wApp.ActiveDocument.Bookmarks.get_Item(ref tempobject).Select();
     63                 wApp.Selection.Text = demostr[i].ToString();
     64             }
     65 
     66         }
     67         Microsoft.Office.Interop.Word.Table wordTablexSoft = wDoc.Tables[1];
     68         InsertTabletoData(wordTablexSoft, ref wDoc, ref wApp);
     69 
     70         //收尾工作   
     71         object o = null;
     72 
     73         //string guid = System.Guid.NewGuid().ToString();
     74         string guid = caseid;
     75         object sFileName = Server.MapPath("/demo/Officebak/" + guid + ".doc");
     76         if (wDoc.SaveFormat == (int)Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatDocument)
     77         {
     78             wDoc.Application.ActiveDocument.SaveAs(ref sFileName, ref missing, ref missing,
     79             ref missing, ref missing, ref missing,
     80             ref missing, ref missing,
     81             ref missing, ref missing,
     82             ref missing, ref missing, ref missing,
     83             ref missing, ref missing, ref missing);
     84         }
     85 
     86         wDoc.Close(ref missing, ref missing, ref missing);
     87         wApp.Quit(ref missing, ref missing, ref missing);
     88 
     89         if (wDoc != null)
     90         {
     91             System.Runtime.InteropServices.Marshal.ReleaseComObject(wDoc);
     92             wDoc = null;
     93         }
     94 
     95         if (wApp != null)
     96         {
     97             System.Runtime.InteropServices.Marshal.ReleaseComObject(wApp);
     98             wApp = null;
     99         }
    100 
    101         GC.Collect();
    102 
    103         return guid + ".doc";
    104     }
    105 
    106 
    107     #region 打开word模板和word文件
    108 
    109     public static void OpenWordDot()
    110     {
    111         //不建议用非标准doc 此处方法 略
    112     }
    113 
    114     private void OpenWordDoc(string FileName, ref Microsoft.Office.Interop.Word.Document wDoc, ref  Microsoft.Office.Interop.Word.Application WApp)
    115     {
    116         if (FileName == ""return;
    117         Microsoft.Office.Interop.Word.Document thisDocument = null;
    118         Microsoft.Office.Interop.Word.FormFields formFields = null;
    119         Microsoft.Office.Interop.Word.Application thisApplication = new Microsoft.Office.Interop.Word.ApplicationClass();
    120         thisApplication.Visible = true;
    121         thisApplication.Caption = "";
    122         thisApplication.Options.CheckSpellingAsYouType = false;
    123         thisApplication.Options.CheckGrammarAsYouType = false;
    124 
    125         Object filename = FileName;
    126         Object ConfirmConversions = false;
    127         Object ReadOnly = true;
    128         Object AddToRecentFiles = false;
    129 
    130         Object PasswordDocument = System.Type.Missing;
    131         Object PasswordTemplate = System.Type.Missing;
    132         Object Revert = System.Type.Missing;
    133         Object WritePasswordDocument = System.Type.Missing;
    134         Object WritePasswordTemplate = System.Type.Missing;
    135         Object Format = System.Type.Missing;
    136         Object Encoding = System.Type.Missing;
    137         Object Visible = System.Type.Missing;
    138         Object OpenAndRepair = System.Type.Missing;
    139         Object DocumentDirection = System.Type.Missing;
    140         Object NoEncodingDialog = System.Type.Missing;
    141         Object XMLTransform = System.Type.Missing;
    142 
    143         try
    144         {
    145             Microsoft.Office.Interop.Word.Document wordDoc =
    146              thisApplication.Documents.Open(ref filename, ref ConfirmConversions,
    147              ref ReadOnly, ref AddToRecentFiles, ref PasswordDocument, ref PasswordTemplate,
    148              ref Revert, ref WritePasswordDocument, ref WritePasswordTemplate, ref Format,
    149              ref Encoding, ref Visible, ref OpenAndRepair, ref DocumentDirection,
    150              ref NoEncodingDialog, ref XMLTransform);
    151 
    152             thisDocument = wordDoc;
    153             wDoc = wordDoc;
    154             WApp = thisApplication;
    155             formFields = wordDoc.FormFields;
    156         }
    157         catch (Exception ex)
    158         {
    159 
    160         }
    161     }
    162 
    163     #endregion
    164     /// <summary>
    165     /// 在objtbale 标签上新增表
    166     /// </summary>
    167     /// <param name="objTable"></param>
    168     /// <param name="wDoc"></param>
    169     /// <param name="WApp"></param>
    170     /// <returns></returns>
    171     private bool InsertTabletoData(Microsoft.Office.Interop.Word.Table wordTable, ref Microsoft.Office.Interop.Word.Document wDoc, ref  Microsoft.Office.Interop.Word.Application WApp)
    172     {
    173 
    174 
    175         object Rownum = 5;
    176         object Columnnum = 1;
    177         wordTable.Cell(182).Split(ref Rownum, ref  Columnnum);
    178         wordTable.Cell(183).Split(ref Rownum, ref  Columnnum);
    179         wordTable.Cell(184).Split(ref Rownum, ref  Columnnum);
    180         wordTable.Cell(185).Split(ref Rownum, ref  Columnnum);
    181         for (int i = 0; i < 5; i++)
    182         {
    183             wordTable.Cell(18 + i, 2).Range.Text = "测试1列" + i + "";
    184             wordTable.Cell(18 + i, 3).Range.Text = "测试2列" + i + "";
    185             wordTable.Cell(18 + i, 4).Range.Text = "测试3列" + i + "";
    186             wordTable.Cell(18 + i, 5).Range.Text = "测试4列" + i + "";
    187         }
    188 
    189         return true;
    190     }
    191     /// <summary>
    192     /// 在objtbale 标签上新增表
    193     /// </summary>
    194     /// <param name="objTable"></param>
    195     /// <param name="wDoc"></param>
    196     /// <param name="WApp"></param>
    197     /// <returns></returns>
    198     private bool InsertTabletoBookmark(string objTable, ref Microsoft.Office.Interop.Word.Document wDoc, ref  Microsoft.Office.Interop.Word.Application WApp)
    199     {
    200         object oEndOfDoc = "\\endofdoc";
    201         object missing = System.Reflection.Missing.Value;
    202 
    203         object objBookmark = objTable;
    204 
    205         WApp.ActiveDocument.Bookmarks.get_Item(ref objBookmark).Select();
    206 
    207         Microsoft.Office.Interop.Word.Table table = wDoc.Tables.Add(WApp.Selection.Range, 34ref missing, ref missing);
    208         table.Cell(11).Range.Text = "表:" + WApp.ActiveDocument.Bookmarks.get_Item(ref objBookmark).Range.Tables[1].Rows.Count;
    209 
    210         return true;
    211     }
    212 }
  • 相关阅读:
    MySQL中interactive_timeout和wait_timeout的区别
    MySQL主从环境下存储过程,函数,触发器,事件的复制情况
    Hbase的伪分布式安装
    当master down掉后,pt-heartbeat不断重试会导致内存缓慢增长
    ERROR 1010 (HY000): Error dropping database (can't rmdir './test/', errno: 17)
    基于MySQL MEB的备份恢复
    pt-heartbeat
    B树和B+树的区别
    如何在Zabbix上安装MySQL监控插件PMP
    如何部署Zabbix服务端
  • 原文地址:https://www.cnblogs.com/weisteve/p/2218446.html
Copyright © 2020-2023  润新知