**/
        //创建报表
        public void MakeWord(object status)
        {
            IProgressCallback callback = status as IProgressCallback;

string strFileName; //=bc_SaveDialog.FileName;
            strFileName = saveFileDialog1.FileName;
            int Num_row = 12;
            int Num_column = 4;

object missing = System.Reflection.Missing.Value;
            object myTrue = false;
            object oLinkToFile = false;     //缺省   
            object oSaveWithDocument = true;//缺省

object modeFileName = AppDomain.CurrentDomain.BaseDirectory + "System\\Template.docx";
            Word._Application myWordApp = new Word.ApplicationClass();
            Word._Document myWordDoc = myWordApp.Documents.Open(ref modeFileName, 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);

string[] strS = new string[6];
            try
            {
                callback.Begin(0, 14);
                callback.StepTo(0);

object bk = "编号";
                if (myWordApp.ActiveDocument.Bookmarks.Exists(bk.ToString()))
                {
                    myWordDoc.Bookmarks.get_Item(ref bk).Range.Text = serialNum.Text;
                }
                bk = "标题";
                if (myWordApp.ActiveDocument.Bookmarks.Exists(bk.ToString()))
                {
                    myWordDoc.Bookmarks.get_Item(ref bk).Range.Text = Title.Text;
                }
                bk = "期号";
                if (myWordApp.ActiveDocument.Bookmarks.Exists(bk.ToString()))
                {
                    myWordDoc.Bookmarks.get_Item(ref bk).Range.Text = yearSel.Value.ToString() + " " +
                    "第 " + qiHao.Value.ToString() + " 期";
                }
                bk = "发送";
                if (myWordApp.ActiveDocument.Bookmarks.Exists(bk.ToString()))
                {
                    myWordDoc.Bookmarks.get_Item(ref bk).Range.Text = Sender.Text;
                }
                bk = "签发";
                if (myWordApp.ActiveDocument.Bookmarks.Exists(bk.ToString()))
                {
                    myWordDoc.Bookmarks.get_Item(ref bk).Range.Select();
                    string qianmingPath = System.Windows.Forms.Application.StartupPath + "\\System\\电子签名.png";
                    myWordApp.Selection.InlineShapes.AddPicture(qianmingPath, ref oLinkToFile, ref oSaveWithDocument, ref missing);
                    Word.Shape s = myWordApp.Application.ActiveDocument.InlineShapes[1].ConvertToShape();
                    s.Top = 0;
                }
                bk = "抄送";
                if (myWordApp.ActiveDocument.Bookmarks.Exists(bk.ToString()))
                {
                    myWordDoc.Bookmarks.get_Item(ref bk).Range.Text = Copy.Text;
                }
                bk = "时间";
                if (myWordApp.ActiveDocument.Bookmarks.Exists(bk.ToString()))
                {
                    string year1 = dateTimePicker2.Value.Year.ToString().Trim();
                    string month1 = dateTimePicker2.Value.Month.ToString().Trim();
                    if (month1.Length < 2) month1 = "0" + month1;
                    string day1 = dateTimePicker2.Value.Day.ToString().Trim();
                    if (day1.Length < 2) day1 = "0" + day1;
                    string hour1 = dateTimePicker2.Value.Hour.ToString().Trim();
                    if (hour1.Length < 2) hour1 = "0" + hour1;
                    myWordDoc.Bookmarks.get_Item(ref bk).Range.Text = year1 + "年" + month1 + "月" + day1 + "日" + hour1 + "时";
                }
                bk = "表格";
                if (myWordApp.ActiveDocument.Bookmarks.Exists(bk.ToString()))
                {
                    Word.Range rng2 = myWordDoc.Bookmarks.get_Item(ref bk).Range;
                    myWordDoc.Tables.Add(rng2, Num_row, Num_column, ref missing, ref missing);
                    myWordDoc.Tables[1].Rows.HeightRule = Word.WdRowHeightRule.wdRowHeightAtLeast;
                    myWordDoc.Tables[1].Rows.Height = myWordApp.CentimetersToPoints(float.Parse("0.8"));
                    myWordDoc.Tables[1].Range.Font.Size = (float)10.5;
                    myWordDoc.Tables[1].Range.Font.Color = Word.WdColor.wdColorBlack;
                    myWordDoc.Tables[1].Range.Font.Name = "宋体";
                    myWordDoc.Tables[1].Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
                    myWordDoc.Tables[1].Range.Cells.VerticalAlignment = Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;
                    //设置表格样式
                    myWordDoc.Tables[1].Borders[Word.WdBorderType.wdBorderLeft].LineStyle = Word.WdLineStyle.wdLineStyleSingle;
                    myWordDoc.Tables[1].Borders[Word.WdBorderType.wdBorderLeft].LineWidth = Word.WdLineWidth.wdLineWidth050pt;
                    myWordDoc.Tables[1].Borders[Word.WdBorderType.wdBorderLeft].Color = Word.WdColor.wdColorAutomatic;

myWordDoc.Tables[1].Borders[Word.WdBorderType.wdBorderTop].LineStyle = Word.WdLineStyle.wdLineStyleSingle;
                    myWordDoc.Tables[1].Borders[Word.WdBorderType.wdBorderTop].LineWidth = Word.WdLineWidth.wdLineWidth050pt;
                    myWordDoc.Tables[1].Borders[Word.WdBorderType.wdBorderTop].Color = Word.WdColor.wdColorAutomatic;

myWordDoc.Tables[1].Borders[Word.WdBorderType.wdBorderBottom].LineStyle = Word.WdLineStyle.wdLineStyleSingle;
                    myWordDoc.Tables[1].Borders[Word.WdBorderType.wdBorderBottom].LineWidth = Word.WdLineWidth.wdLineWidth050pt;
                    myWordDoc.Tables[1].Borders[Word.WdBorderType.wdBorderBottom].Color = Word.WdColor.wdColorAutomatic;

myWordDoc.Tables[1].Borders[Word.WdBorderType.wdBorderHorizontal].LineStyle = Word.WdLineStyle.wdLineStyleSingle;
                    myWordDoc.Tables[1].Borders[Word.WdBorderType.wdBorderHorizontal].LineWidth = Word.WdLineWidth.wdLineWidth050pt;
                    myWordDoc.Tables[1].Borders[Word.WdBorderType.wdBorderHorizontal].Color = Word.WdColor.wdColorAutomatic;

myWordDoc.Tables[1].Borders[Word.WdBorderType.wdBorderVertical].LineStyle = Word.WdLineStyle.wdLineStyleSingle;
                    myWordDoc.Tables[1].Borders[Word.WdBorderType.wdBorderVertical].LineWidth = Word.WdLineWidth.wdLineWidth050pt;
                    myWordDoc.Tables[1].Borders[Word.WdBorderType.wdBorderVertical].Color = Word.WdColor.wdColorAutomatic;

myWordDoc.Tables[1].Borders[Word.WdBorderType.wdBorderRight].LineStyle = Word.WdLineStyle.wdLineStyleSingle;
                    myWordDoc.Tables[1].Borders[Word.WdBorderType.wdBorderRight].LineWidth = Word.WdLineWidth.wdLineWidth050pt;
                    myWordDoc.Tables[1].Borders[Word.WdBorderType.wdBorderRight].Color = Word.WdColor.wdColorAutomatic;

//第1行显示
                    callback.StepTo(1);
                    myWordDoc.Tables[1].Cell(1, 1).Range.Text = "数据类型:";
                    myWordDoc.Tables[1].Cell(1, 2).Range.Text = dataType.Text;
                    myWordDoc.Tables[1].Cell(1, 3).Range.Text = "数据景数:";
                    myWordDoc.Tables[1].Cell(1, 4).Range.Text = dataNum.Value.ToString();

//第2行显示
                    callback.StepTo(2);
                    myWordDoc.Tables[1].Cell(2, 2).Merge(myWordDoc.Tables[1].Cell(2, 3));
                    myWordDoc.Tables[1].Cell(2, 2).Merge(myWordDoc.Tables[1].Cell(2, 3));
                    myWordDoc.Tables[1].Cell(2, 1).Range.Text = "成像时间:";
                    myWordDoc.Tables[1].Cell(2, 2).Range.Text = aquireTime.Text;

//第3行显示
                    callback.StepTo(3);
                    myWordDoc.Tables[1].Cell(3, 2).Merge(myWordDoc.Tables[1].Cell(3, 3));
                    myWordDoc.Tables[1].Cell(3, 2).Merge(myWordDoc.Tables[1].Cell(3, 3));
                    myWordDoc.Tables[1].Cell(3, 1).Range.Text = "坐 标 系:";
                    myWordDoc.Tables[1].Cell(3, 2).Range.Text = geoCoor.Text;

//第4行显示
                    callback.StepTo(4);
                    myWordDoc.Tables[1].Cell(4, 1).Range.Text = "西北角坐标:";
                    myWordDoc.Tables[1].Cell(4, 2).Range.Text = NorthWest.Text;
                    myWordDoc.Tables[1].Cell(4, 2).Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft;
                    myWordDoc.Tables[1].Cell(4, 3).Merge(myWordDoc.Tables[1].Cell(4, 4));

//第5行显示
                    callback.StepTo(5);
                    myWordDoc.Tables[1].Cell(5, 1).Range.Text = "东北角坐标:";
                    myWordDoc.Tables[1].Cell(5, 2).Range.Text = NorthEast.Text;
                    myWordDoc.Tables[1].Cell(5, 2).Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft;
                    myWordDoc.Tables[1].Cell(5, 3).Merge(myWordDoc.Tables[1].Cell(5, 4));

//第6行显示
                    callback.StepTo(6);
                    myWordDoc.Tables[1].Cell(6, 1).Range.Text = "东南角坐标:";
                    myWordDoc.Tables[1].Cell(6, 2).Range.Text = SourthEast.Text;
                    myWordDoc.Tables[1].Cell(6, 2).Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft;
                    myWordDoc.Tables[1].Cell(6, 3).Merge(myWordDoc.Tables[1].Cell(6, 4));

//第7行显示
                    callback.StepTo(7);
                    myWordDoc.Tables[1].Cell(7, 1).Range.Text = "西南角坐标:";
                    myWordDoc.Tables[1].Cell(7, 2).Range.Text = SourthWest.Text;
                    myWordDoc.Tables[1].Cell(7, 2).Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft;
                    myWordDoc.Tables[1].Cell(7, 3).Merge(myWordDoc.Tables[1].Cell(7, 4));

//合并4-7后两格,插入缩略图                
                    myWordDoc.Tables[1].Cell(4, 3).Merge(myWordDoc.Tables[1].Cell(7, 3));
                    myWordDoc.Tables[1].Cell(4, 3).LeftPadding = 0;
                    myWordDoc.Tables[1].Cell(4, 3).RightPadding = 0;
                    myNothing = System.Reflection.Missing.Value;
                    //para.Range = myWordDoc.Tables[1].Cell(4, 3).Range;
                    object kk = myWordDoc.Tables[1].Cell(4, 3).Range;
                    object oMissed = kk;
                    oLinkToFile = false;     //缺省   
                    oSaveWithDocument = true;//缺省   
                    //string picPath1 = textBox16.Text;
                    if (File.Exists(rasterExtent))
                    {
                        myWordApp.ActiveDocument.InlineShapes.AddPicture(rasterExtent,
                        ref oLinkToFile, ref oSaveWithDocument, ref oMissed);
                        myWordApp.ActiveDocument.InlineShapes[2].Width = 211.5f;//图片宽度
                        myWordApp.ActiveDocument.InlineShapes[2].Height = 123f;//图片高度
                    }
                    //第8行显示
                    callback.StepTo(8);
                    myWordDoc.Tables[1].Cell(8, 1).Merge(myWordDoc.Tables[1].Cell(8, 4));
                    myWordDoc.Tables[1].Cell(8, 1).Range.Text = "异常情况:";
                    myWordDoc.Tables[1].Cell(8, 1).Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft;

//第9行显示
                    callback.StepTo(9);
                    myWordDoc.Tables[1].Cell(9, 1).Range.Text = "是否发现异常";
                    myWordDoc.Tables[1].Cell(9, 2).Range.Text = "置信度";
                    myWordDoc.Tables[1].Cell(9, 3).Range.Text = "异常区数量";
                    myWordDoc.Tables[1].Cell(9, 4).Range.Text = "异常等级";

//第10行显示
                    callback.StepTo(10);
                    myWordDoc.Tables[1].Cell(10, 1).Range.Text = this.line6;

string beliv = "";
                    if (rbHigh.Checked == true)
                    {
                        beliv = "高";
                    }
                    if (rbMid.Checked == true)
                    {
                        beliv = "中";
                    }
                    if (rbLow.Checked == true)
                    {
                        beliv = "低";
                    }
                    myWordDoc.Tables[1].Cell(10, 2).Range.Text = beliv;

myWordDoc.Tables[1].Cell(10, 3).Range.Text = abnormalNum.Value.ToString();

string abLevel = "";
                    if (rbSeriousL.Checked == true)
                    {
                        abLevel = "严重";
                    }
                    if (rbSevereL.Checked == true)
                    {
                        abLevel = "较严重";
                    }
                    if (rbMidstL.Checked == true)
                    {
                        abLevel = "中等";
                    }
                    if (rbSlightL.Checked == true)
                    {
                        abLevel = "轻微";
                    }
                    if (rbNoneL.Checked == true)
                    {
                        abLevel = "无";
                    }
                    myWordDoc.Tables[1].Cell(10, 4).Range.Text = abLevel;

//第11行显示
                    callback.StepTo(11);
                    myWordDoc.Tables[1].Cell(11, 1).Merge(myWordDoc.Tables[1].Cell(11, 4));

if (this.abnormalNum.Value == 0)
                    {
                        this.line10 = this.line10 + "\n\n\n\n\n\n\n\n\n";
                    }
                    else if (this.abnormalNum.Value == 1)
                    {
                        this.line10 = this.line10 + "\n\n\n\n\n";
                    }
                    else if (this.abnormalNum.Value == 2)
                    {
                        this.line10 = this.line10 + "\n";
                    }
                    myWordDoc.Tables[1].Cell(11, 1).Range.Text = this.line10;
                    myWordDoc.Tables[1].Cell(11, 1).Range.Paragraphs.Space1();
                    myWordDoc.Tables[1].Cell(11, 1).Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft;

//第12行显示
                    callback.StepTo(12);
                    myWordDoc.Tables[1].Cell(12, 1).Merge(myWordDoc.Tables[1].Cell(12, 4));
                    Microsoft.Office.Interop.Word.Cell cell = (Microsoft.Office.Interop.Word.Cell)myWordDoc.Tables[1].Cell(12, 1);
                    object Rownum = 1;
                    object Columnnum = 2;
                    cell.Split(ref Rownum, ref  Columnnum);
                    myWordDoc.Tables[1].Cell(12, 1).Range.Text = "值班员:" + watch.Text;
                    myWordDoc.Tables[1].Cell(12, 2).Range.Text = "审  核:" + this.line12;

//插入图片
                    if (File.Exists(vectorFile.Text) == true)
                    {
                        //插入分页符
                        para = myWordDoc.Content.Paragraphs.Add(ref myNothing);
                        object pBreak = (int)Word.WdBreakType.wdSectionBreakNextPage;
                        para.Range.InsertBreak(ref pBreak);

//插入图片
                        myNothing = System.Reflection.Missing.Value;
                        object nn = para.Range;
                        oMissed = nn;
                        oLinkToFile = false;     //缺省   
                        oSaveWithDocument = true;//缺省   
                        string picPath = vectorFile.Text;
                        if (File.Exists(picPath))
                        {
                            myWordApp.Selection.InlineShapes.AddPicture(picPath, ref oLinkToFile, ref oSaveWithDocument, ref oMissed);
                        }
                    }
                    //插入图片
                    if (File.Exists(rasterFile.Text) == true)
                    {
                        //插入分页符
                        para = myWordDoc.Content.Paragraphs.Add(ref myNothing);
                        object pBreak = (int)Word.WdBreakType.wdSectionBreakNextPage;
                        para.Range.InsertBreak(ref pBreak);

//插入图片
                        myNothing = System.Reflection.Missing.Value;
                        object nn = para.Range;
                        oMissed = nn;
                        oLinkToFile = false;     //缺省   
                        oSaveWithDocument = true;//缺省   
                        string picPath = rasterFile.Text;
                        if (File.Exists(picPath))
                        {
                            myWordApp.Selection.InlineShapes.AddPicture(picPath, ref oLinkToFile, ref oSaveWithDocument, ref oMissed);
                        }
                    }
                }

myWordDoc.ActiveWindow.ActivePane.View.SeekView = Word.WdSeekView.wdSeekCurrentPageFooter;//激活页眉的编辑
                myWordApp.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;//设置对齐方式
                myWordApp.Selection.Font.Name = "宋体";
                myWordApp.Selection.Font.Size = 9f;

object wdFieldPage = Word.WdFieldType.wdFieldPage;
                object wdFieldPages = Word.WdFieldType.wdFieldNumPages;
                object preserveFormatting = true;
                myWordApp.Selection.TypeText("第 ");
                myWordApp.Selection.Fields.Add(myWordApp.Selection.Range,
                ref wdFieldPage, ref missing, ref preserveFormatting);
                myWordApp.Selection.TypeText(" 页 共 ");
                myWordApp.Selection.Fields.Add(myWordApp.Selection.Range,
                ref wdFieldPages, ref missing, ref preserveFormatting);
                myWordApp.Selection.TypeText(" 页");
                //------存储文档
                object filename = strFileName;
                //将myWordDoc文档对象的内容保存为DOC文档 
                myWordDoc.SaveAs(ref filename, 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);
                //关闭myWordDoc文档对象

myWordDoc.Close(ref missing, ref missing, ref missing);
                myWordApp.Quit(ref missing, ref missing, ref missing);

if (myWordDoc != null)
                {
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(myWordDoc);
                    myWordDoc = null;
                }
                if (myWordApp != null)
                {
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(myWordApp);
                    myWordApp = null;
                }

GC.Collect();

callback.StepTo(14);

MessageBox.Show("文档生成完毕!", "系统提示", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                MessageBox.Show("文档生成错误!错误原因:" + ex.Message.ToString(), "系统提示", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
                try
                {
                    myWordDoc.Close(ref missing, ref missing, ref missing);
                    myWordApp.Quit(ref missing, ref missing, ref missing);

if (myWordDoc != null)
                    {
                        System.Runtime.InteropServices.Marshal.ReleaseComObject(myWordDoc);
                        myWordDoc = null;
                    }
                    if (myWordApp != null)
                    {
                        System.Runtime.InteropServices.Marshal.ReleaseComObject(myWordApp);
                        myWordApp = null;
                    }

GC.Collect();
                    killAllProcess();
                }
                catch
                {
                    killAllProcess();
                }
            }
            finally
            {
                if (callback != null)
                    callback.End();
            }
        }

 

C#操作word标签相关推荐

  1. C#操作Word Aspose.Words组件介绍及使用 基本介绍与DOM概述

    1.基本介绍 Aspose.Words是一个商业.NET类库,可以使得应用程序处理大量的文件任务.Aspose.Words支持Doc,Docx,RTF,HTML,OpenDocument,PDF,XP ...

  2. POI和POI-TL操作Word

    POI操作Word 操作依赖包org.apache.poi,包括poi和poi-ooxml. 创建空白Word文件 void poiCreateNewWord() {String path = &qu ...

  3. poi操作word文档总结

    POI分段落生成纯Word动态模板并导入数据 导出数据,可以用word另存为xml格式的ftl文件,变量用${变量名}表示,然后在类中通过 freemarker去替换变量. 但是怎么导入word数据. ...

  4. java操作word(一)

    一. 需求背景 在做项目的过程中,经常会遇到要把数据库数据导出到Word文件中的需求,因为很多情况下,我们需要将数据导出到WORD中进行打印.此需求可以通过用程序填充数据到word模板中来实现.所谓模 ...

  5. 使用poi操作word文档实现套打功能

    使用poi操作word文档实现套打功能 本文目的是为了分享一个实现套打功能,但是不同于简单的word的文本替换而是采用poi对word的文本框就行操作实现的功能: poi中各种jar的说明 套打的实现 ...

  6. poi操作word替换模板向指定位置添加图表

    poi操作word替换模板向指定位置添加图表 首先是引入pom文件 <dependency><groupId>org.apache.poi</groupId>< ...

  7. 用Qt操作Word文档

    由于项目需要通过dot模板导出doc的文档,所以就查了资料写了一个操作word 的类,只实现了部分功能,其代码如下: #include "wordengine.h" #includ ...

  8. c#操作word图表(一)

    c#操作word图表 前阵子接到了一个任务,需要实现一个功能,利用代码在word中插入图表并且插入数据,生成类似于柱状图,饼图,线条之类乱七八糟的东西,关于这个方面,不得不说网上的资料相对较少,多方查 ...

  9. C#操作Word文档

    1.c#操作word 在指定书签插入文字或者图片 using Word = Microsoft.Office.Interop.Word; object Nothing = System.Reflect ...

最新文章

  1. 起底华为计算战略:十五年研发下苦功,三年连发10芯加速,目前2万工程师奋战...
  2. MySQL性能优化的最佳21条经验
  3. VC编译的除法的一段汇编代码解释
  4. 如何使WordPress博客添加多个sidebar侧边栏
  5. php文件上传并保存路径到数据库,thinkphp表单上传文件并将文件路径保存到数据库中?...
  6. 正面管教php_中联重科团委快乐父母协会第五期正面管教家长基础班招募!
  7. 乌班图配置mysql Java_Ubuntu准备+MySQL+Java
  8. 《深入理解计算机系统》 CSAPP 入坑推荐
  9. 计算机语言编程入门基础
  10. skiller v3 beta2 发布
  11. JAVA程序员笔试面试题汇总及答案 .
  12. 购买的腾讯云服务器一直被ddos恶意攻击怎么解决
  13. 深度学习中的数据增强(上)
  14. happy hacking keybord(hhkb pro)键盘 翻新真假判别(realforce键盘也一样)
  15. 思科网络设备安装与调试——帧中继云配置
  16. alertmanager钉钉报警
  17. Python3.0 如何抓取网页
  18. Hadoop HA (四) --------- YARN-HA 配置
  19. 脑波设备mindwave介绍
  20. 庄懂的技术美术入门课(美术向)——学习笔记01

热门文章

  1. 磁盘分区显示位置不可用咋修复?
  2. @人生最痛苦事是,在不懂爱的年龄,选择了婚姻,而在懂爱的年龄,遇到了不能在一起的人。
  3. 【OpenCV】KeyPoint数据结构
  4. Vue 实现拖拽模块(三)自定义拖拽组件的样式
  5. RFID系统工作原理
  6. 华为手机助手上架流程_华为对 Linux 贡献数一数二;倍思上架苹果 iPhone 12 Pro Max 手机壳...
  7. Task12:哈希表
  8. ebay账号防关联4要素(ebay防关联软件)
  9. gradle下载更新依赖库失败的解决办法 - 依赖库下载加速1000%
  10. 直播测试-网络数据设置