C#操作word标签

**/
        //创建报表
        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();
            }
        }

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值