• 动态创建word后下载(二)


    首先:做一下基础工作:

    导入COM库:Microsoft word 11.0 Object Library.

    引入命名空间:
    using Microsoft.Office.Interop.Word;

    其次:分别阐述每个知识点的使用

    实例化一个word对象:

    实例化Code
     1 killWordProcess();//进程中会有word进程,杀掉上次的
     2 object missingValue = System.Reflection.Missing.Value;
     3                 object myTrue = false;                  //不显示Word窗口
     4                 Microsoft.Office.Interop.Word._Application oWord = new 
     5 
     6 Microsoft.Office.Interop.Word.ApplicationClass();
     7                 Microsoft.Office.Interop.Word._Document doc;
     8                 doc = oWord.Documents.Open(ref Obj_FileName, ref missingValue,
     9                    ref myTrue, ref missingValue, ref missingValue, ref missingValue,
    10                    ref missingValue, ref missingValue, ref missingValue,
    11                    ref missingValue, ref missingValue, ref missingValue,
    12                    ref missingValue, ref missingValue, ref missingValue,
    13                    ref missingValue);
     

     动态插入书签并赋值(图片、文字)
       前提:1.通过模板中一个来定位其他书签的位置 (aa:书签是模板中的)
               2.循环插入(循环产生:例如:bookmark1 bookmark11 bookmark32)

    动态书签 Code
     1 if (intTestType == 1)
     2                             {
     3                                 aa = "scoreBar";
     4                             }
     5                             
     6 //注意:书签是相连的
     7                             Microsoft.Office.Interop.Word.Range r = doc.Bookmarks.get_Item(ref aa).Range;
     8                             object rng = (object)r;
     9                             oWord.ActiveDocument.Bookmarks.Add("bookmark" + intTestType, ref rng); //bookmark1
    10                             oWord.ActiveDocument.Bookmarks.DefaultSorting = 0;
    11                             oWord.ActiveDocument.Bookmarks.ShowHidden = true;
    12 
    13                             object o = "bookmark" + intTestType;
    14 
    15                             Microsoft.Office.Interop.Word.Range r2 = doc.Bookmarks.get_Item(ref o).Range;
    16                             object rng2 = (object)r2;
    17                             oWord.ActiveDocument.Bookmarks.Add("bookmark1" + intTestType, ref rng2); //bookmark11
    18                             oWord.ActiveDocument.Bookmarks.DefaultSorting = 0;
    19                             oWord.ActiveDocument.Bookmarks.ShowHidden = true;
    20                             
    21 //以下对书签赋值                           
    22                             doc.Bookmarks.get_Item(ref aa).Range.InlineShapes.AddPicture(@"c:\scorebar.png", ref missingValue, ref missingValue); //插入图片
    23                             o2 = "bookmark1" + intTestType;
    24                             doc.Bookmarks.get_Item(ref o).Range.Text = NumberHelper.ConvertSum(intTestType.ToString()) + "" + dr["itemName"].ToString() + "\r\n"; //插入文字
    25 
    26 //循环变量
    27                     intTestType++;
    28                              intTestType1++;
    29 
    30                             Microsoft.Office.Interop.Word.Range r3 = doc.Bookmarks.get_Item(ref o2).Range;
    31                           
    32                             object rng3 = (object)r3;
    33                             oWord.ActiveDocument.Bookmarks.Add("bookmark3" + intTestType, ref rng3); //bookmark32
    34                             oWord.ActiveDocument.Bookmarks.DefaultSorting = 0;
    35                             oWord.ActiveDocument.Bookmarks.ShowHidden = true;
    36 
    37                             aa = "bookmark3" + intTestType;
     

     循环遍历模板中的书签,显示相应内容(图片、文字、表格)

    循环遍历书签 Code
     1 foreach (Microsoft.Office.Interop.Word.Bookmark bm in doc.Bookmarks)
     2                 {
     3                     if (bm.Name == "bookmark1")
     4                     {
     5                         object aa = "bookmark1";
     6                         doc.Bookmarks.get_Item(ref aa).Range.InlineShapes.AddPicture(@"c:\scorebar.png", ref missingValue, ref missingValue); //插入图片
     7                     }else if (bm.Name == "mainTitle")
     8                     {
     9 
    10                         bm.Select();
    11                         bm.Range.Text = strmaintitle;//插入文本
    12                     }else
    13     {
    14          object tmp = "copyScore";
    15                         Microsoft.Office.Interop.Word.Range startRange = oWord.ActiveDocument.Bookmarks.get_Item(ref tmp).Range;
    16 
    17                         int intTestNum = 10;//todo 从DB中获取该试卷的总题型
    18                         int intSumNum = intTestNum + 2;
    19                         //添加表格
    20                         doc.Tables.Add(startRange, 2, intSumNum, ref missingValue, ref missingValue);
    21 
    22 
    23                         doc.Tables[1].Cell(1, 1).Range.Text = "题号";//设置文本
    24                         doc.Tables[1].Cell(2, 1).Range.Text = "得分";
    25                         doc.Tables[1].Cell(1, intSumNum).Range.Text = "总分";//设置文本
    26 
    27                         for (int i = 1; i <= intTestNum; i++)
    28                         {
    29                             doc.Tables[1].Cell(1, i + 1).Range.Text = NumberHelper.ConvertSum(i.ToString());//设置文本
    30                         }
    31                         //为表格划线
    32                         startRange.Tables[1].Borders[WdBorderType.wdBorderTop].LineStyle = WdLineStyle.wdLineStyleSingle;
    33                         startRange.Tables[1].Borders[WdBorderType.wdBorderLeft].LineStyle = WdLineStyle.wdLineStyleSingle;
    34                         startRange.Tables[1].Borders[WdBorderType.wdBorderRight].LineStyle = WdLineStyle.wdLineStyleSingle;
    35                         startRange.Tables[1].Borders[WdBorderType.wdBorderBottom].LineStyle = WdLineStyle.wdLineStyleSingle;
    36                         startRange.Tables[1].Borders[WdBorderType.wdBorderHorizontal].LineStyle = WdLineStyle.wdLineStyleSingle;
    37                         startRange.Tables[1].Borders[WdBorderType.wdBorderVertical].LineStyle = WdLineStyle.wdLineStyleSingle;
    38 
    39                         doc.Tables[1].Select();//选中表格
    40                         oWord.Selection.Tables[1].Rows.Alignment = WdRowAlignment.wdAlignRowCenter;//表格居中
    41 
    42 }
    43 }
     

     
    // 每当产生新word 之前,先关闭以前的word.exe进程

    关闭 进程 Code
     1  protected void killWordProcess()
     2         {
     3             System.Diagnostics.Process[] myPs;
     4             myPs = System.Diagnostics.Process.GetProcesses();
     5             foreach (System.Diagnostics.Process p in myPs)
     6             {
     7                 if (p.ProcessName.ToUpper() == "WINWORD.EXE")
     8                 {
     9                     try
    10                     {
    11                         p.Kill();
    12                     }
    13                     catch (Exception)
    14                     {
    15 
    16                     }
    17 
    18                 }
    19             }
    20         }
     
  • 相关阅读:
    哈利波特买书事件
    闹钟类app构想
    梦断代码(7-尾)
    梦断代码(3-6)
    梦断代码(0-2)
    环形二维数组求最大子矩阵
    数组问题
    电梯考察
    四则运算的三张计划表
    团队开发用户需求调研
  • 原文地址:https://www.cnblogs.com/lei2007/p/2594065.html
Copyright © 2020-2023  润新知