Example #1
0
        /// <summary>
        /// 页面设置、页眉图片和文字设置,最后跳出页眉设置
        /// </summary>
        /// <param name="filePath"></param>
        public void SetPage()
        {
            //页面设置
            wordDoc.PageSetup.PaperSize      = MSWord.WdPaperSize.wdPaperA4;          //设置纸张样式为A4纸
            wordDoc.PageSetup.Orientation    = MSWord.WdOrientation.wdOrientPortrait; //排列方式为垂直方向
            wordDoc.PageSetup.TopMargin      = 57.0f;
            wordDoc.PageSetup.BottomMargin   = 57.0f;
            wordDoc.PageSetup.LeftMargin     = 57.0f;
            wordDoc.PageSetup.RightMargin    = 57.0f;
            wordDoc.PageSetup.HeaderDistance = 30.0f;//页眉位置

            #region 页码设置并添加页码

            //为当前页添加页码
            MSWord.PageNumbers pns = wordApp.Selection.Sections[1].Headers[MSWord.WdHeaderFooterIndex.wdHeaderFooterEvenPages].PageNumbers; //获取当前页的号码
            pns.NumberStyle               = MSWord.WdPageNumberStyle.wdPageNumberStyleNumberInDash;                                         //设置页码的风格,是Dash形还是圆形的
            pns.HeadingLevelForChapter    = 0;
            pns.IncludeChapterNumber      = false;
            pns.RestartNumberingAtSection = false;
            pns.StartingNumber            = 0;                                         //开始页页码?
            object pagenmbetal = MSWord.WdPageNumberAlignment.wdAlignPageNumberCenter; //将号码设置在中间
            object first       = true;
            wordApp.Selection.Sections[1].Footers[MSWord.WdHeaderFooterIndex.wdHeaderFooterEvenPages].PageNumbers.Add(ref pagenmbetal, ref first);

            #endregion
        }
Example #2
0
        public void CreateReport()
        {
            object       path;                                        //文件路径
            string       strContent;                                  //文件内容
            _Application wordApp;                                     //Word应用程序变量

            MSWord._Document wordDoc;                                 //Word文档变量
            Console.WriteLine(Environment.CurrentDirectory);
            path    = Environment.CurrentDirectory + "\\myWord.docx"; //保存为Word2003文档
            wordApp = new MSWord.ApplicationClass();                  //初始化
            if (File.Exists((string)path))
            {
                File.Delete((string)path);
            }
            Object Nothing = Missing.Value;

            wordDoc = wordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);

            #region 页面设置
            //
            wordDoc.PageSetup.PaperSize = Microsoft.Office.Interop.Word.WdPaperSize.wdPaperA4;            //设置纸张样式

            wordDoc.PageSetup.Orientation = Microsoft.Office.Interop.Word.WdOrientation.wdOrientPortrait; //排列方式为垂直方向
            //wordDoc.PageSetup.TopMargin = 57.0f;
            //wordDoc.PageSetup.BottomMargin = 57.0f;
            //wordDoc.PageSetup.LeftMargin = 57.0f;
            //wordDoc.PageSetup.RightMargin = 57.0f;
            //wordDoc.PageSetup.HeaderDistance = 30.0f;//页眉位置
            #endregion

            #region 设置页眉
            wordApp.ActiveWindow.View.Type              = Microsoft.Office.Interop.Word.WdViewType.wdOutlineView;       //视图样式
            wordApp.ActiveWindow.View.SeekView          = Microsoft.Office.Interop.Word.WdSeekView.wdSeekPrimaryHeader; //进入页眉设置,其中页眉边距在页面设置中已完成
            wordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphRight;
            wordApp.Selection.ParagraphFormat.Alignment = MSWord.WdParagraphAlignment.wdAlignParagraphRight;
            wordApp.ActiveWindow.ActivePane.Selection.InsertAfter("洛阳晶云信息科技有限公司");
            //去掉页眉的横线
            wordApp.ActiveWindow.ActivePane.Selection.ParagraphFormat.Borders[MSWord.WdBorderType.wdBorderBottom].LineStyle = MSWord.WdLineStyle.wdLineStyleNone;
            wordApp.ActiveWindow.ActivePane.Selection.Borders[MSWord.WdBorderType.wdBorderBottom].Visible = false;
            wordApp.ActiveWindow.ActivePane.View.SeekView = MSWord.WdSeekView.wdSeekMainDocument;//退出页眉设置
            #endregion

            #region 设置标头
            string filename = Environment.CurrentDirectory + "\\title_icon.png";
            //定义要向文档中插入图片的位置
            object range = wordApp.Selection.Range;
            //定义该图片是否为外部链接
            object linkToFile = false;//默认
            //定义插入的图片是否随word一起保存
            object saveWithDocument = true;
            //向word中写入图片
            object left     = 82;
            object top      = 65;
            Shape  TitlePic = wordDoc.Shapes.AddPicture(filename, ref linkToFile, ref saveWithDocument, ref left, ref top, ref Nothing, ref Nothing, ref Nothing);

            TitlePic.Height = wordApp.CentimetersToPoints(2.84f);
            TitlePic.Width  = wordApp.CentimetersToPoints(3.22f);

            //TitlePic.ConvertToShape().WrapFormat.Type = MSWord.WdWrapType.wdWrapFront;// 浮于文字上方
            TitlePic.Select(ref Nothing);
            wordDoc.Content.InsertAfter("\n");

            //object unite = WdUnits.wdStory;
            //wordApp.Selection.EndKey(ref unite, ref Nothing);
            //wordApp.Selection.ParagraphFormat.Alignment = MSWord.WdParagraphAlignment.wdAlignParagraphLeft;//左对齐显示图片


            //object unite2 = WdUnits.wdStory;
            //wordApp.Selection.EndKey(ref unite2, ref Nothing);
            //wordApp.Selection.MoveDown(ref Nothing, ref Nothing, ref Nothing); //移动焦点
            //wordDoc.Paragraphs.Last.Range.Select();
            object unitline = WdUnits.wdLine;
            wordApp.Selection.MoveUp(ref unitline, ref Nothing, ref Nothing);       //移动焦点
            wordApp.Selection.ParagraphFormat.CharacterUnitFirstLineIndent = 0.5f;  //首行缩进0.5字符
            wordApp.Selection.ParagraphFormat.CharacterUnitLeftIndent      = 9.07f; //左缩进9.07字符
            wordApp.Selection.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphJustify;
            strContent = "中国人民解放军总医院病理科";
            Range r = wordApp.Selection.Range;
            r.Text      = strContent;
            r.Font.Name = "黑体";
            r.Font.Size = 22;
            r.Font.Bold = 1;//Bold=0为不加粗


            //移动焦点并换行
            wordApp.Selection.MoveDown(ref Nothing, ref Nothing, ref Nothing); //移动焦点


            wordApp.Selection.ParagraphFormat.CharacterUnitFirstLineIndent = 2.98f; //首行缩进2.98字符
            wordApp.Selection.ParagraphFormat.CharacterUnitLeftIndent      = 9.07f; //左缩进9.07字符
            wordApp.Selection.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphJustify;
            strContent  = "分子病理检测报告\n";
            r           = wordDoc.Paragraphs.Last.Range;
            r.Text      = strContent;
            r.Font.Name = "黑体";
            r.Font.Size = 22;
            r.Font.Bold = 1;  //Bold=0为不加粗
            #endregion

            #region 设置基本信息
            wordApp.Selection.MoveDown(ref Nothing, ref Nothing, ref Nothing);//移动焦点
            wordApp.Selection.ParagraphFormat.Reset();
            wordApp.Selection.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;
            strContent  = "\n";
            r           = wordDoc.Paragraphs.Last.Range;
            r.Text      = strContent;
            r.Font.Name = "黑体";
            r.Font.Size = 12;
            r.Font.Bold = 0;


            wordApp.Selection.MoveDown(ref Nothing, ref Nothing, ref Nothing);//移动焦点
            wordApp.Selection.ParagraphFormat.Reset();
            wordApp.Selection.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphLeft;

            wordApp.Selection.ParagraphFormat.LineSpacingRule = WdLineSpacing.wdLineSpaceExactly;
            wordApp.Selection.ParagraphFormat.LineSpacing     = 17;
            strContent  = "姓 名:       性 别:         年 龄:       病理号:\n";
            r           = wordDoc.Paragraphs.Last.Range;
            r.Text      = strContent;
            r.Font.Name = "宋体";
            //r.Font.Size = 12;
            //r.Font.Bold = 0;

            wordApp.Selection.MoveDown(ref Nothing, ref Nothing, ref Nothing);//移动焦点
            //wordApp.Selection.ParagraphFormat.Reset();
            //wordApp.Selection.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphLeft;
            //wordApp.Selection.ParagraphFormat.LineSpacing = 17;
            strContent = "出生地:     送检部位:     标本类型:石蜡样本           门诊号:\n";
            r          = wordDoc.Paragraphs.Last.Range;
            r.Text     = strContent;
            //r.Font.Name = "宋体";
            //r.Font.Size = 12;
            //r.Font.Bold = 0;

            wordApp.Selection.MoveDown(ref Nothing, ref Nothing, ref Nothing);//移动焦点
            //wordApp.Selection.ParagraphFormat.Reset();
            //wordApp.Selection.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphLeft;
            //wordApp.Selection.ParagraphFormat.LineSpacing = 17;
            strContent = "检测方法:NGS   申请医生:      申请日期:       住院号:\n";
            r          = wordDoc.Paragraphs.Last.Range;
            r.Text     = strContent;
            //r.Font.Name = "宋体";
            //r.Font.Size = 12;
            //r.Font.Bold = 0;

            wordApp.Selection.MoveDown(ref Nothing, ref Nothing, ref Nothing);//移动焦点
            Microsoft.Office.Interop.Word.Shape ShapeLine = wordDoc.Shapes.AddLine(90, 210, 360, 210, ref Nothing);

            ShapeLine.Width = wordApp.CentimetersToPoints(14.92f);
            ShapeLine.Line.ForeColor.RGB = 0;    // 线条颜色 黑色
            ShapeLine.Line.Weight        = 1.5f; // 线条粗细1.5磅
            wordDoc.Content.InsertAfter("\n");

            wordApp.Selection.MoveDown(ref Nothing, ref Nothing, ref Nothing);//移动焦点
            //wordApp.Selection.ParagraphFormat.Reset();
            //wordApp.Selection.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphLeft;
            wordApp.Selection.ParagraphFormat.Space1();
            strContent  = "主要设备:ION TORRENT PGM 测序仪\n";
            r           = wordDoc.Paragraphs.Last.Range;
            r.Text      = strContent;
            r.Font.Size = 15;
            r.Start     = r.Start + 5;
            r.End       = r.End - 4;
            r.Font.Name = "Times New Roman";
            r.Font.Size = 14;

            wordApp.Selection.MoveDown(ref Nothing, ref Nothing, ref Nothing);//移动焦点
            //wordApp.Selection.ParagraphFormat.Reset();
            //wordApp.Selection.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphLeft;
            //wordApp.Selection.ParagraphFormat.Space1();
            strContent         = "检测试剂盒:Ion PGMTM Hi-QTM OT2 Kit-200、\n";
            r                  = wordDoc.Paragraphs.Last.Range;
            r.Text             = strContent;
            r.Font.Size        = 15;
            r.Start            = r.Start + 6;
            r.End              = r.End - 1;
            r.Font.Name        = "Times New Roman";
            r.Font.Size        = 14;
            r.Start            = r.Start + 7;// 选择第一个TM 设为上标
            r.End              = r.Start + 2;
            r.Font.Superscript = 1;
            r.Start            = r.Start + 7; // 选择第二个TM 设为上标
            r.End              = r.Start + 2;
            r.Font.Superscript = 1;

            wordApp.Selection.MoveDown(ref Nothing, ref Nothing, ref Nothing);//移动焦点
            //wordApp.Selection.ParagraphFormat.Reset();
            //wordApp.Selection.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphLeft;
            wordApp.Selection.ParagraphFormat.CharacterUnitFirstLineIndent = 6.5f; //首行缩进2.98字符
            wordApp.Selection.ParagraphFormat.Space15();
            strContent         = "Ion PGMTM Hi-QTM Sequencing Kit\n";
            r                  = wordDoc.Paragraphs.Last.Range;
            r.Font.Name        = "Times New Roman";
            r.Text             = strContent;
            r.Font.Size        = 15;
            r.Font.Size        = 14;
            r.Start            = r.Start + 7;// 选择第一个TM 设为上标
            r.End              = r.Start + 2;
            r.Font.Superscript = 1;
            r.Start            = r.Start + 7; // 选择第二个TM 设为上标
            r.End              = r.Start + 2;
            r.Font.Superscript = 1;
            #endregion

            #region 第一个表格 检测结果
            wordApp.Selection.MoveDown(ref Nothing, ref Nothing, ref Nothing);//移动焦点
            wordApp.Selection.ParagraphFormat.Reset();
            wordApp.Selection.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphJustify;
            wordApp.Selection.ParagraphFormat.Space15();
            strContent  = "检测结果:\n";
            r           = wordDoc.Paragraphs.Last.Range;
            r.Font.Name = "宋体";
            r.Text      = strContent;
            r.Font.Size = 11.5f;
            r.Font.Bold = 1;

            wordApp.Selection.MoveDown(ref Nothing, ref Nothing, ref Nothing);                      //移动焦点
            //wordApp.Selection.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;//水平居中
            Table t = wordDoc.Tables.Add(wordApp.Selection.Range, 16, 6, ref Nothing, ref Nothing); //16行6列的表
            t.Rows.Alignment          = MSWord.WdRowAlignment.wdAlignRowCenter;
            t.Borders.InsideLineStyle = WdLineStyle.wdLineStyleSingle;
            t.Select();
            wordApp.Selection.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;       //水平居中
            wordApp.Selection.Cells.VerticalAlignment   = WdCellVerticalAlignment.wdCellAlignVerticalCenter; // 垂直居中
            t.Columns[2].Select();
            wordApp.Selection.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphLeft;         //左对齐居中
            float [] ColWid = { 1.97f, 3.56f, 1.84f, 3.28f, 4.13f, 2.54f };
            string[] title  = { "基因名称", "外显子", "突变类型", "碱基变化", "氨基酸变化", "COSMIC ID" };
            for (int i = 1; i <= 6; i++)
            {
                t.Columns[i].Width = wordApp.CentimetersToPoints(ColWid[i - 1]);
                r           = t.Cell(1, i).Range;
                r.Text      = title[i - 1];
                r.Font.Size = 9;
                t.Cell(1, i).Select();
                wordApp.Selection.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;//水平居中
            }
            t.Rows[1].Borders[WdBorderType.wdBorderTop].LineStyle    = WdLineStyle.wdLineStyleSingle;
            t.Rows[1].Borders[WdBorderType.wdBorderTop].LineWidth    = WdLineWidth.wdLineWidth225pt;
            t.Rows[1].Borders[WdBorderType.wdBorderBottom].LineStyle = WdLineStyle.wdLineStyleSingle;
            t.Rows[1].Borders[WdBorderType.wdBorderBottom].LineWidth = WdLineWidth.wdLineWidth150pt;
            string[] GeneName = { "EGFR", "KRAS", "BRAF", "PIK3CA", "PDGFRA", "NRAS", "KIT", "DDR2", "EBRR2", "FLT3", "NPM1", "SMO", "DNMT3A", "ABL1", "TSC1" };
            string[] wai      = { "18、19、20、21", "2、3", "11、15", "9、20", "12、14、18", "2、3", "9、11、13、14、17", "18", "20", "14、15、20", "11", "8", "15、16、17、18、19、20、21、22、23", "4、5、6", "15" };
            for (int i = 0; i < GeneName.Length; i++)
            {
                r           = t.Cell(i + 2, 1).Range;
                r.Text      = GeneName[i];
                r.Font.Size = 10.5f;
                r.Font.Name = "Times New Roman";
                r.Bold      = 0;

                r           = t.Cell(i + 2, 2).Range;
                r.Text      = wai[i];
                r.Font.Size = 10.5f;
                r.Font.Name = "Times New Roman";
                r.Bold      = 0;

                r           = t.Cell(i + 2, 4).Range;
                r.Text      = "未见突变";
                r.Font.Size = 10.5f;
                r.Bold      = 0;
            }
            t.Rows[t.Rows.Count].Borders[WdBorderType.wdBorderBottom].LineStyle = WdLineStyle.wdLineStyleSingle;
            object movecount = t.Rows.Count;
            #endregion

            object bs = WdBreakType.wdSectionBreakNextPage;
            wordDoc.Paragraphs.Last.Range.InsertBreak(ref bs);
            Console.WriteLine(wordApp.Selection.MoveDown(ref Nothing, ref Nothing, ref Nothing));//移动焦点


            #region 第二个表格 ALK、ROS1、RET基因热点区域融合检测结果
            wordApp.Selection.ParagraphFormat.Reset();
            wordApp.Selection.ParagraphFormat.Alignment   = WdParagraphAlignment.wdAlignParagraphJustify;
            wordApp.Selection.ParagraphFormat.LineSpacing = 17;
            strContent = "ALK、ROS1、RET基因热点区域融合检测结果:\n";
            r          = wordDoc.Paragraphs.Last.Range;
            r.Select();
            r.Font.Name = "宋体";
            r.Text      = strContent;
            r.Font.Size = 10.5f;
            r.Font.Bold = 0;
            r.End       = r.Start + 3;
            r.Font.Name = "Times New Roman";
            r.Start     = r.End + 1;
            r.End       = r.Start + 4;
            r.Font.Name = "Times New Roman";
            r.Start     = r.End + 1;
            r.End       = r.Start + 3;
            r.Font.Name = "Times New Roman";

            Console.WriteLine(wordApp.Selection.MoveDown(ref Nothing, ref Nothing, ref Nothing));//移动焦点


            wordApp.Selection.ParagraphFormat.Reset();
            wordApp.Selection.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;    //水平居中
            Table t2 = wordDoc.Tables.Add(wordDoc.Paragraphs.Last.Range, 4, 5, ref Nothing, ref Nothing); //4行5列的表
            t2.PreferredWidthType = WdPreferredWidthType.wdPreferredWidthPercent;

            t2.Borders.InsideLineStyle = WdLineStyle.wdLineStyleSingle;
            t2.Select();
            wordApp.Selection.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;       //水平居中
            wordApp.Selection.Cells.VerticalAlignment   = WdCellVerticalAlignment.wdCellAlignVerticalCenter; // 垂直居中
            t2.Columns[2].Select();
            wordApp.Selection.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphLeft;         //左对齐居中
            float[]  ColPro   = { 13.6f, 44.6f, 12, 16, 13.5f };
            string[] HotTitle = { "基因名称", "检测范围", "结果类型", "具体位点", "COSMIC ID" };
            for (int i = 1; i <= 5; i++)
            {
                //t2.Columns[i].SetWidth(ColPro[i - 1], WdRulerStyle.wdAdjustProportional);
                t2.Columns[i].Width = ColPro[i - 1];
                r           = t2.Cell(1, i).Range;
                r.Text      = HotTitle[i - 1];
                r.Font.Size = 9;
                r.Font.Bold = 1;
                t2.Cell(1, i).Select();
                wordApp.Selection.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;//水平居中
            }
            t2.Rows[1].Borders[WdBorderType.wdBorderTop].LineStyle    = WdLineStyle.wdLineStyleSingle;
            t2.Rows[1].Borders[WdBorderType.wdBorderTop].LineWidth    = WdLineWidth.wdLineWidth225pt;
            t2.Rows[1].Borders[WdBorderType.wdBorderBottom].LineStyle = WdLineStyle.wdLineStyleSingle;
            t2.Rows[1].Borders[WdBorderType.wdBorderBottom].LineWidth = WdLineWidth.wdLineWidth150pt;
            string[] GeneName2 = { "EML4-ALK", "ROS1", "KIF5B-RET" };
            string[] TestR     = { "E13;A20、E20;A20、E6a;A20、E6b;A20、E14;ins11del49 A20、E2;A20、E2;ins117A20、E13;ins69A20、E14;del12A20", "SLC34A2 e4-ROS1 e32、SLC34A2 e4-ROS1 e34、SLC34A2 e13-ROS1 e32、SLC34A2 e13-ROS1 e34、CD74 e6-ROS1 e32、CD74 e6-ROS1 e34、SDC4 e2-ROS1 e32、SDC4 e2-ROS1 e34、SDC4 e4-ROS1 e32、SDC4 e4-ROS1 e34、EZR e10-ROS1 e34、TPM3 e8-ROS1 e35、LRIG3 e16-ROS1 e35、FIG e8-ROS1 e35、FIG e4-ROS1 e36", "K15;R12、K15;R11、K16;R12、K22;R12、K23;R12、K24;R8、K24;R11" };
            for (int i = 0; i < GeneName2.Length; i++)
            {
                r           = t2.Cell(i + 2, 1).Range;
                r.Text      = GeneName2[i];
                r.Font.Size = 10.5f;
                r.Font.Name = "Times New Roman";
                r.Bold      = 0;

                r           = t2.Cell(i + 2, 2).Range;
                r.Text      = TestR[i];
                r.Font.Size = 10.5f;
                r.Font.Name = "Times New Roman";
                r.Bold      = 0;
            }
            t2.AutoFitBehavior(WdAutoFitBehavior.wdAutoFitWindow);
            t2.Rows[t2.Rows.Count].Borders[WdBorderType.wdBorderBottom].LineStyle = WdLineStyle.wdLineStyleSingle;
            object t2rows = t2.Rows.Count;
            wordApp.Selection.MoveDown(ref unitline, ref t2rows, ref Nothing);//移动焦点
            r       = t2.Range;
            r.Start = r.End;
            r.InsertBreak(ref Nothing);
            #endregion

            Console.WriteLine(wordApp.Selection.MoveDown(ref Nothing, ref Nothing, ref Nothing));//移动焦点

            #region 第三个表格
            wordApp.Selection.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphLeft;
            wordApp.Selection.ParagraphFormat.Space1();
            strContent  = "阳性突变\n检测患者相关基因,对测序顺序进行深入的分析,确定突变基因、突变位点以及临床意义的详细信息如下:";
            r           = wordApp.Selection.Range;
            r.Font.Name = "宋体";
            r.Text      = strContent;
            r.Font.Size = 10.5f;
            r.Font.Bold = 1;
            r.Select();


            wordApp.Selection.MoveDown(ref Nothing, ref Nothing, ref Nothing);                             //移动焦点
            Table t3 = wordDoc.Tables.Add(wordDoc.Paragraphs.Last.Range, 3, 12, ref Nothing, ref Nothing); //3行12列的表
            t3.Borders.InsideLineStyle  = WdLineStyle.wdLineStyleSingle;
            t3.Borders.OutsideLineStyle = WdLineStyle.wdLineStyleSingle;
            t3.Select();
            wordApp.Selection.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;       //水平居中
            wordApp.Selection.Cells.VerticalAlignment   = WdCellVerticalAlignment.wdCellAlignVerticalCenter; // 垂直居中
            string[] DetailTitle = { "基因", "外显子", "碱基突变", "氨基酸突变", "突变比例", "Cosmic ID", "肿瘤", "标签", "等级", "说明来源", "CFDA批准的药物", "FDA批准的药物" };
            for (int i = 1; i <= DetailTitle.Length; i++)
            {
                r           = t3.Cell(1, i).Range;
                r.Text      = DetailTitle[i - 1];
                r.Font.Size = 10.5f;
                r.Font.Bold = 1;
            }

            t3.Rows[1].Shading.BackgroundPatternColor = (Microsoft.Office.Interop.Word.WdColor)(0x1 * 124 + 0x100 * 187 + 0x10000 * 225);
            #endregion

            wordApp.Selection.MoveDown(ref Nothing, ref Nothing, ref Nothing);//移动焦点

            wordDoc.Sections.Last.Headers[WdHeaderFooterIndex.wdHeaderFooterPrimary].LinkToPrevious = false;

            #region 设置页眉
            wordApp.ActiveWindow.View.Type     = WdViewType.wdOutlineView;       //视图样式
            wordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekPrimaryHeader; //进入页眉设置,其中页眉边距在页面设置中已完成

            wordApp.Selection.ParagraphFormat.Alignment  = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphRight;
            wordApp.Selection.ParagraphFormat.Alignment  = MSWord.WdParagraphAlignment.wdAlignParagraphRight;
            wordApp.Selection.Paragraphs.Last.Range.Text = "洛阳晶云信息科技有限公司2";

            //去掉页眉的横线
            wordApp.ActiveWindow.ActivePane.Selection.ParagraphFormat.Borders[MSWord.WdBorderType.wdBorderBottom].LineStyle = MSWord.WdLineStyle.wdLineStyleNone;
            wordApp.ActiveWindow.ActivePane.Selection.Borders[MSWord.WdBorderType.wdBorderBottom].Visible = false;
            wordApp.ActiveWindow.ActivePane.View.SeekView = MSWord.WdSeekView.wdSeekMainDocument;//退出页眉设置
            #endregion

            #region 设置页码
            MSWord.PageNumbers pns = wordApp.Selection.Sections[1].Headers[MSWord.WdHeaderFooterIndex.wdHeaderFooterEvenPages].PageNumbers;//获取当前页的号码
            pns.NumberStyle               = WdPageNumberStyle.wdPageNumberStyleNumberInDash;
            pns.HeadingLevelForChapter    = 0;
            pns.IncludeChapterNumber      = false;
            pns.RestartNumberingAtSection = false;
            pns.StartingNumber            = 0;
            object pagenmbetal = WdPageNumberAlignment.wdAlignPageNumberCenter;//将号码设置在中间
            object first       = true;
            wordApp.Selection.Sections[1].Footers[WdHeaderFooterIndex.wdHeaderFooterEvenPages].PageNumbers.Add(ref pagenmbetal, ref first);
            #endregion

            #region 保存为DOCX和PDF
            object format    = MSWord.WdSaveFormat.wdFormatDocumentDefault;
            object pdfformat = MSWord.WdSaveFormat.wdFormatPDF;
            //object htmlformat = WdSaveFormat.wdFormatHTML;
            object pdfpath = Environment.CurrentDirectory + "\\myPDF.pdf";
            //object htmlpath = Environment.CurrentDirectory + "\\myHTML.html";
            wordDoc.SaveAs(ref path, ref format, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing);
            wordDoc.SaveAs(ref pdfpath, ref pdfformat, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing);
            //wordDoc.SaveAs(ref htmlpath, ref htmlformat, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing);
            wordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
            wordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
            #endregion
        }
Example #3
0
        public static void Print2Word(string strContent)
        {
            object path;                              //文件路径变量
            MSWord.Application wordApp;                   //Word应用程序变量 
            MSWord.Document wordDoc;                  //Word文档变量

            path = "d:\\result\\MyWord_Print.doc";
            wordApp = new MSWord.ApplicationClass(); //初始化

            wordApp.Visible = true;//使文档可见

            //如果已存在,则删除
            if (File.Exists((string)path))
            {
                File.Delete((string)path);
            }

            //由于使用的是COM库,因此有许多变量需要用Missing.Value代替
            Object Nothing = Missing.Value;
            wordDoc = wordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);
            // 页面设置
            wordDoc.PageSetup.PaperSize = MSWord.WdPaperSize.wdPaperA4;//设置纸张样式为A4纸
            wordDoc.PageSetup.Orientation = MSWord.WdOrientation.wdOrientPortrait;//排列方式为垂直方向
            wordDoc.PageSetup.TopMargin = 48.0f;
            wordDoc.PageSetup.BottomMargin = 48.0f;
            wordDoc.PageSetup.LeftMargin = 57.0f;
            wordDoc.PageSetup.RightMargin = 57.0f;
            wordDoc.PageSetup.HeaderDistance = 50.0f;//页眉位置
            wordDoc.PageSetup.FooterDistance = 80f;

            //设置页眉
            wordApp.ActiveWindow.View.Type = MSWord.WdViewType.wdPrintView; //普通视图(即页面视图)样式
            wordApp.ActiveWindow.View.SeekView = MSWord.WdSeekView.wdSeekPrimaryHeader;//进入页眉设置,其中页眉边距在页面设置中已完成
            MSWord.Range allRange = wordDoc.Range();

            foreach (MSWord.Section wordSection in wordApp.ActiveDocument.Sections)
            {
                MSWord.Range headerRange = wordSection.Headers[MSWord.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range;
                headerRange.Font.Size = 20;
                headerRange.Text = "文安县2017年公共住房公开配租摇号结果";

                MSWord.Range footerRange = wordSection.Footers[MSWord.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range;
                footerRange.Font.Size = 16;
                footerRange.Text = "\n监察局:\t\t公证处:\n\n\n群众代表:";
            }
            //wordApp.Selection.HeaderFooter.Range.Text = "这里是测试";

            //去掉页眉的横线
            //wordApp.ActiveWindow.ActivePane.Selection.ParagraphFormat.Borders[MSWord.WdBorderType.wdBorderBottom].LineStyle = MSWord.WdLineStyle.wdLineStyleNone;
            //wordApp.ActiveWindow.ActivePane.Selection.Borders[MSWord.WdBorderType.wdBorderBottom].Visible = false;            
            wordApp.ActiveWindow.ActivePane.View.SeekView = MSWord.WdSeekView.wdSeekMainDocument;//退出页眉设置

            //为当前页添加页码
            MSWord.PageNumbers pns = wordApp.Selection.Sections[1].Headers[MSWord.WdHeaderFooterIndex.wdHeaderFooterEvenPages].PageNumbers;//获取当前页的号码
            pns.NumberStyle = MSWord.WdPageNumberStyle.wdPageNumberStyleArabicFullWidth; //设置页码的风格,是Dash形还是圆形的            
            pns.HeadingLevelForChapter = 0;
            pns.IncludeChapterNumber = true;
            pns.RestartNumberingAtSection = false;
            pns.StartingNumber = 0; //开始页页码?
            object pagenmbetal = MSWord.WdPageNumberAlignment.wdAlignPageNumberCenter;//将号码设置在中间
            object first = true;
            wordApp.Selection.Sections[1].Footers[MSWord.WdHeaderFooterIndex.wdHeaderFooterEvenPages].PageNumbers.Add(ref pagenmbetal, ref first);



            // 行间距与缩进、文本字体、字号、加粗、斜体、颜色、下划线、下划线颜色设置
            object unite = MSWord.WdUnits.wdStory;
            wordApp.Selection.ParagraphFormat.Alignment = MSWord.WdParagraphAlignment.wdAlignParagraphLeft;
            wordApp.Selection.ParagraphFormat.LineSpacing = 20f;//设置文档的行间距
            wordApp.Selection.ParagraphFormat.FirstLineIndent = 30;//首行缩进的长度
            wordApp.Selection.EndKey(ref unite, ref Nothing);
            wordDoc.Paragraphs.Last.Range.Font.Size = 16;
            wordDoc.Paragraphs.Last.Range.Font.Name = "宋体";
            //写入普通文本
            wordDoc.Paragraphs.Last.Range.Text = strContent;

            object myNothing = System.Reflection.Missing.Value;
            object myFileName = path;
            object myWordFormatDocument = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatDocument;
            object myLockd = false;
            object myPassword = "";
            object myAddto = true;
            wordApp.ActiveDocument.SaveAs2(ref myFileName, ref myWordFormatDocument, ref myLockd, ref myPassword, ref myAddto, ref myPassword,
                     ref myLockd, ref myLockd, ref myLockd, ref myLockd, ref myNothing, ref myNothing, ref myNothing,
                     ref myNothing, ref myNothing, ref myNothing);



            //wordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
            //关闭wordApp组件对象
            //wordApp.Quit(ref Nothing, ref Nothing, ref Nothing);

        }
        private void button1_Click(object sender, EventArgs e)
        {
            object path;                              //文件路径变量
            string strContent;                        //文本内容变量

            MSWord.Application wordApp;               //Word应用程序变量
            MSWord.Document    wordDoc;               //Word文档变量

            path    = Path.GetFullPath("../../") + "\\MyWord_Print.doc";
            wordApp = new MSWord.ApplicationClass(); //初始化

            wordApp.Visible = true;                  //使文档可见

            //如果已存在,则删除
            if (File.Exists((string)path))
            {
                File.Delete((string)path);
            }

            //由于使用的是COM库,因此有许多变量需要用Missing.Value代替
            Object Nothing = Missing.Value;

            wordDoc = wordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);

            #region 页面设置、页眉图片和文字设置,最后跳出页眉设置

            //页面设置
            wordDoc.PageSetup.PaperSize    = MSWord.WdPaperSize.wdPaperA4;          //设置纸张样式为A4纸
            wordDoc.PageSetup.Orientation  = MSWord.WdOrientation.wdOrientPortrait; //排列方式为垂直方向
            wordDoc.PageSetup.TopMargin    = 57.0f;
            wordDoc.PageSetup.BottomMargin = 57.0f;
            wordDoc.PageSetup.LeftMargin   = 57.0f;
            wordDoc.PageSetup.RightMargin  = 57.0f;
            //wordDoc.PageSetup.HeaderDistance = 30.0f;//页眉位置
            #endregion

            #region 页码设置并添加页码

            //为当前页添加页码
            MSWord.PageNumbers pns = wordApp.Selection.Sections[1].Headers[MSWord.WdHeaderFooterIndex.wdHeaderFooterEvenPages].PageNumbers; //获取当前页的号码
            pns.NumberStyle               = MSWord.WdPageNumberStyle.wdPageNumberStyleNumberInDash;                                         //设置页码的风格,是Dash形还是圆形的
            pns.HeadingLevelForChapter    = 0;
            pns.IncludeChapterNumber      = false;
            pns.RestartNumberingAtSection = false;
            pns.StartingNumber            = 0;                                         //开始页页码?
            object pagenmbetal = MSWord.WdPageNumberAlignment.wdAlignPageNumberCenter; //将号码设置在中间
            object first       = true;
            wordApp.Selection.Sections[1].Footers[MSWord.WdHeaderFooterIndex.wdHeaderFooterEvenPages].PageNumbers.Add(ref pagenmbetal, ref first);

            #endregion

            #region 行间距与缩进、文本字体、字号、加粗、斜体、颜色、下划线、下划线颜色设置

            wordApp.Selection.ParagraphFormat.LineSpacing     = 16f; //设置文档的行间距
            wordApp.Selection.ParagraphFormat.FirstLineIndent = 30;  //首行缩进的长度
            //写入普通文本
            strContent = "我是普通文本\n";
            wordDoc.Paragraphs.Last.Range.Text = strContent;

            wordDoc.Paragraphs.Last.Range.Text = "我再加一行试试,这里不加'\\n'";
            //直接添加段,不是覆盖( += )
            wordDoc.Paragraphs.Last.Range.Text += "不会覆盖的,";

            //添加在此段的文字后面,不是新段落
            wordDoc.Paragraphs.Last.Range.InsertAfter("这是后面的内容\n");

            //将文档的前4个字替换成"哥是替换文字",并将其颜色设为红色
            object       start = 0;
            object       end   = 4;
            MSWord.Range rang  = wordDoc.Range(ref start, ref end);
            rang.Font.Color = MSWord.WdColor.wdColorRed;
            rang.Text       = "哥是替换文字";
            wordDoc.Range(ref start, ref end);

            //写入黑体文本
            object unite = MSWord.WdUnits.wdStory;
            wordApp.Selection.EndKey(ref unite, ref Nothing);      //将光标移到文本末尾
            wordApp.Selection.ParagraphFormat.FirstLineIndent = 0; //取消首行缩进的长度
            strContent = "这是黑体文本\n";
            wordDoc.Paragraphs.Last.Range.Font.Name = "黑体";
            wordDoc.Paragraphs.Last.Range.Text      = strContent;

            //写入加粗文本
            strContent = "这是粗体文本\n";                          //
            wordApp.Selection.EndKey(ref unite, ref Nothing); //这一句不加,有时候好像也不出问题,不过还是加了安全
            wordDoc.Paragraphs.Last.Range.Font.Bold = 1;
            wordDoc.Paragraphs.Last.Range.Text      = strContent;

            //写入15号字体文本
            strContent = "我这个文本的字号是15号,而且是宋体\n";
            wordApp.Selection.EndKey(ref unite, ref Nothing);
            wordDoc.Paragraphs.Last.Range.Font.Size = 15;
            wordDoc.Paragraphs.Last.Range.Font.Name = "宋体";
            wordDoc.Paragraphs.Last.Range.Text      = strContent;

            //写入斜体文本
            strContent = "我是斜体字文本\n";
            wordApp.Selection.EndKey(ref unite, ref Nothing);
            wordDoc.Paragraphs.Last.Range.Font.Italic = 1;
            wordDoc.Paragraphs.Last.Range.Text        = strContent;

            //写入蓝色文本
            strContent = "我是蓝色的文本\n";
            wordApp.Selection.EndKey(ref unite, ref Nothing);
            wordDoc.Paragraphs.Last.Range.Font.Color = MSWord.WdColor.wdColorBlue;
            wordDoc.Paragraphs.Last.Range.Text       = strContent;

            //写入下划线文本
            strContent = "我是下划线文本\n";
            wordApp.Selection.EndKey(ref unite, ref Nothing);
            wordDoc.Paragraphs.Last.Range.Font.Underline = MSWord.WdUnderline.wdUnderlineThick;
            wordDoc.Paragraphs.Last.Range.Text           = strContent;

            //写入红色下画线文本
            strContent = "我是点线下划线,并且下划线是红色的\n";
            wordApp.Selection.EndKey(ref unite, ref Nothing);
            wordDoc.Paragraphs.Last.Range.Font.Underline      = MSWord.WdUnderline.wdUnderlineDottedHeavy;
            wordDoc.Paragraphs.Last.Range.Font.UnderlineColor = MSWord.WdColor.wdColorRed;
            wordDoc.Paragraphs.Last.Range.Text = strContent;

            //取消下划线,并且将字号调整为12号
            strContent = "我他妈不要下划线了,并且设置字号为12号,黑色不要斜体\n";
            wordApp.Selection.EndKey(ref unite, ref Nothing);
            wordDoc.Paragraphs.Last.Range.Font.Size      = 12;
            wordDoc.Paragraphs.Last.Range.Font.Underline = MSWord.WdUnderline.wdUnderlineNone;
            wordDoc.Paragraphs.Last.Range.Font.Color     = MSWord.WdColor.wdColorBlack;
            wordDoc.Paragraphs.Last.Range.Font.Italic    = 0;
            wordDoc.Paragraphs.Last.Range.Text           = strContent;


            #endregion


            #region 插入图片、居中显示,设置图片的绝对尺寸和缩放尺寸,并给图片添加标题

            wordApp.Selection.EndKey(ref unite, ref Nothing); //将光标移动到文档末尾
            //图片文件的路径
            string filename = Path.GetFullPath("../../") + "\\6.jpg";
            //要向Word文档中插入图片的位置
            Object range = wordDoc.Paragraphs.Last.Range;
            //定义该插入的图片是否为外部链接
            Object linkToFile = false;               //默认,这里貌似设置为bool类型更清晰一些
            //定义要插入的图片是否随Word文档一起保存
            Object saveWithDocument = true;          //默认
            //使用InlineShapes.AddPicture方法(【即“嵌入型”】)插入图片
            wordDoc.InlineShapes.AddPicture(filename, ref linkToFile, ref saveWithDocument, ref range);
            wordApp.Selection.ParagraphFormat.Alignment = MSWord.WdParagraphAlignment.wdAlignParagraphCenter;//居中显示图片

            //设置图片宽高的绝对大小

            //wordDoc.InlineShapes[1].Width = 200;
            //wordDoc.InlineShapes[1].Height = 150;
            //按比例缩放大小

            wordDoc.InlineShapes[1].ScaleWidth  = 20;//缩小到20% ?
            wordDoc.InlineShapes[1].ScaleHeight = 20;

            //在图下方居中添加图片标题

            wordDoc.Content.InsertAfter("\n");//这一句与下一句的顺序不能颠倒,原因还没搞透
            wordApp.Selection.EndKey(ref unite, ref Nothing);
            wordApp.Selection.ParagraphFormat.Alignment = MSWord.WdParagraphAlignment.wdAlignParagraphCenter;
            wordApp.Selection.Font.Size = 10;//字体大小
            wordApp.Selection.TypeText("图1 测试图片\n");

            #endregion

            #region 添加表格、填充数据、设置表格行列宽高、合并单元格、添加表头斜线、给单元格添加图片
            wordDoc.Content.InsertAfter("\n");                //这一句与下一句的顺序不能颠倒,原因还没搞透
            wordApp.Selection.EndKey(ref unite, ref Nothing); //将光标移动到文档末尾
            wordApp.Selection.ParagraphFormat.Alignment = MSWord.WdParagraphAlignment.wdAlignParagraphLeft;
            //object WdLine2 = MSWord.WdUnits.wdLine;//换一行;
            //wordApp.Selection.MoveDown(ref WdLine2, 6, ref Nothing);//向下跨15行输入表格,这样表格就在文字下方了,不过这是非主流的方法

            //设置表格的行数和列数
            int tableRow    = 6;
            int tableColumn = 6;

            //定义一个Word中的表格对象
            MSWord.Table table = wordDoc.Tables.Add(wordApp.Selection.Range,
                                                    tableRow, tableColumn, ref Nothing, ref Nothing);

            //默认创建的表格没有边框,这里修改其属性,使得创建的表格带有边框
            table.Borders.Enable = 1;//这个值可以设置得很大,例如5、13等等

            //表格的索引是从1开始的。
            wordDoc.Tables[1].Cell(1, 1).Range.Text = "列\n行";
            for (int i = 1; i < tableRow; i++)
            {
                for (int j = 1; j < tableColumn; j++)
                {
                    if (i == 1)
                    {
                        table.Cell(i, j + 1).Range.Text = "Column " + j;//填充每列的标题
                    }
                    if (j == 1)
                    {
                        table.Cell(i + 1, j).Range.Text = "Row " + i;         //填充每行的标题
                    }
                    table.Cell(i + 1, j + 1).Range.Text = i + "行 " + j + "列"; //填充表格的各个小格子
                }
            }


            //添加行
            table.Rows.Add(ref Nothing);
            table.Rows[tableRow + 1].Height = 35;                             //设置新增加的这行表格的高度
            //向新添加的行的单元格中添加图片
            string FileName         = Path.GetFullPath("../../") + "\\6.jpg"; //图片所在路径
            object LinkToFile       = false;
            object SaveWithDocument = true;
            object Anchor           = table.Cell(tableRow + 1, tableColumn).Range;//选中要添加图片的单元格
            wordDoc.Application.ActiveDocument.InlineShapes.AddPicture(FileName, ref LinkToFile, ref SaveWithDocument, ref Anchor);

            //由于是本文档的第2张图,所以这里是InlineShapes[2]
            wordDoc.Application.ActiveDocument.InlineShapes[2].Width  = 50; //图片宽度
            wordDoc.Application.ActiveDocument.InlineShapes[2].Height = 35; //图片高度

            // 将图片设置为四周环绕型
            MSWord.Shape s = wordDoc.Application.ActiveDocument.InlineShapes[2].ConvertToShape();
            s.WrapFormat.Type = MSWord.WdWrapType.wdWrapSquare;


            //设置table样式
            table.Rows.HeightRule = MSWord.WdRowHeightRule.wdRowHeightAtLeast;       //高度规则是:行高有最低值下限?
            table.Rows.Height     = wordApp.CentimetersToPoints(float.Parse("0.8")); //

            table.Range.Font.Size = 10.5F;
            table.Range.Font.Bold = 0;

            table.Range.ParagraphFormat.Alignment = MSWord.WdParagraphAlignment.wdAlignParagraphCenter;       //表格文本居中
            table.Range.Cells.VerticalAlignment   = MSWord.WdCellVerticalAlignment.wdCellAlignVerticalBottom; //文本垂直贴到底部
            //设置table边框样式
            table.Borders.OutsideLineStyle = MSWord.WdLineStyle.wdLineStyleDouble;                            //表格外框是双线
            table.Borders.InsideLineStyle  = MSWord.WdLineStyle.wdLineStyleSingle;                            //表格内框是单线

            table.Rows[1].Range.Font.Bold    = 1;                                                             //加粗
            table.Rows[1].Range.Font.Size    = 12F;
            table.Cell(1, 1).Range.Font.Size = 10.5F;
            wordApp.Selection.Cells.Height   = 30;//所有单元格的高度

            //除第一行外,其他行的行高都设置为20
            for (int i = 2; i <= tableRow; i++)
            {
                table.Rows[i].Height = 20;
            }

            //将表格左上角的单元格里的文字(“行” 和 “列”)居右
            table.Cell(1, 1).Range.ParagraphFormat.Alignment = MSWord.WdParagraphAlignment.wdAlignParagraphRight;
            //将表格左上角的单元格里面下面的“列”字移到左边,相比上一行就是将ParagraphFormat改成了Paragraphs[2].Format
            table.Cell(1, 1).Range.Paragraphs[2].Format.Alignment = MSWord.WdParagraphAlignment.wdAlignParagraphLeft;

            table.Columns[1].Width = 50;//将第 1列宽度设置为50

            //将其他列的宽度都设置为75
            for (int i = 2; i <= tableColumn; i++)
            {
                table.Columns[i].Width = 75;
            }


            //添加表头斜线,并设置表头的样式
            table.Cell(1, 1).Borders[MSWord.WdBorderType.wdBorderDiagonalDown].Visible   = true;
            table.Cell(1, 1).Borders[MSWord.WdBorderType.wdBorderDiagonalDown].Color     = MSWord.WdColor.wdColorRed;
            table.Cell(1, 1).Borders[MSWord.WdBorderType.wdBorderDiagonalDown].LineWidth = MSWord.WdLineWidth.wdLineWidth150pt;

            //合并单元格
            table.Cell(4, 4).Merge(table.Cell(4, 5)); //横向合并

            table.Cell(2, 3).Merge(table.Cell(4, 3)); //纵向合并,合并(2,3),(3,3),(4,3)

            #endregion

            wordApp.Selection.EndKey(ref unite, ref Nothing); //将光标移动到文档末尾

            wordDoc.Content.InsertAfter("\n");
            wordDoc.Content.InsertAfter("就写这么多,算了吧!2016.09.27");



            //WdSaveFormat为Word 2003文档的保存格式
            object format = MSWord.WdSaveFormat.wdFormatDocument;// office 2007就是wdFormatDocumentDefault
            //将wordDoc文档对象的内容保存为doc文档
            wordDoc.SaveAs(ref path, ref format, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing);
            //关闭wordDoc文档对象

            //看是不是要打印
            //wordDoc.PrintOut();



            wordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
            //关闭wordApp组件对象
            wordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
            //Console.WriteLine(path + " 创建完毕!");
            //Console.ReadKey();


            //我还要打开这个文档玩玩
            MSWord.Application app = new MSWord.Application();
            MSWord.Document    doc = null;
            try
            {
                object unknow = Type.Missing;
                app.Visible = true;
                string str  = Path.GetFullPath("../../") + "\\MyWord_Print.doc";
                object file = str;
                doc = app.Documents.Open(ref file,
                                         ref unknow, ref unknow, ref unknow, ref unknow,
                                         ref unknow, ref unknow, ref unknow, ref unknow,
                                         ref unknow, ref unknow, ref unknow, ref unknow,
                                         ref unknow, ref unknow, ref unknow);
                string temp = doc.Paragraphs[1].Range.Text.Trim();
                //Console.WriteLine("你他妈输出temp干嘛?");
            }
            catch (Exception ex)
            {
                //Console.WriteLine(ex.Message);
            }
            wordDoc = doc;
            wordDoc.Paragraphs.Last.Range.Text += "我真的不打算再写了,就写这么多吧";
        }
Example #5
0
        public async static void CreateTestPaperWord(TestPaper testPaper)
        {
            object path;                              //文件路径变量
            string strContent;                        //文本内容变量

            MSWord.Application wordApp;               //Word应用程序变量
            MSWord.Document    wordDoc;               //Word文档变量
            string             pathStr = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"\QBM\";

            Directory.CreateDirectory(pathStr);
            wordApp         = new MSWord.Application(); //初始化
            pathStr         = pathStr + "TestPaper" + testPaper.Id + ".doc";
            wordApp.Visible = false;                    //使文档可见
            path            = pathStr;
            //如果已存在,则删除
            if (!File.Exists((string)path))
            {
                //由于使用的是COM库,因此有许多变量需要用Missing.Value代替
                Object Nothing = Missing.Value;
                wordDoc = wordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);
                #region 页面设置、页眉图片和文字设置,最后跳出页眉设置
                //页面设置
                wordDoc.PageSetup.PaperSize      = MSWord.WdPaperSize.wdPaperA4;          //设置纸张样式为A4纸
                wordDoc.PageSetup.Orientation    = MSWord.WdOrientation.wdOrientPortrait; //排列方式为垂直方向
                wordDoc.PageSetup.TopMargin      = 57.0f;
                wordDoc.PageSetup.BottomMargin   = 57.0f;
                wordDoc.PageSetup.LeftMargin     = 57.0f;
                wordDoc.PageSetup.RightMargin    = 57.0f;
                wordDoc.PageSetup.HeaderDistance = 30.0f;//页眉位置
                #endregion

                #region 页码设置并添加页码
                //为当前页添加页码
                MSWord.PageNumbers pns = wordApp.Selection.Sections[1].Headers[MSWord.WdHeaderFooterIndex.wdHeaderFooterEvenPages].PageNumbers; //获取当前页的号码
                pns.NumberStyle               = MSWord.WdPageNumberStyle.wdPageNumberStyleNumberInDash;                                         //设置页码的风格,是Dash形还是圆形的
                pns.HeadingLevelForChapter    = 0;
                pns.IncludeChapterNumber      = false;
                pns.RestartNumberingAtSection = false;
                pns.StartingNumber            = 1;                                         //开始页页码
                object pagenmbetal = MSWord.WdPageNumberAlignment.wdAlignPageNumberCenter; //将号码设置在中间
                object first       = true;
                wordApp.Selection.Sections[1].Footers[MSWord.WdHeaderFooterIndex.wdHeaderFooterEvenPages].PageNumbers.Add(ref pagenmbetal, ref first);
                #endregion
                wordApp.Selection.ParagraphFormat.LineSpacing     = 16f; //设置文档的行间距
                wordApp.Selection.ParagraphFormat.FirstLineIndent = 30;  //首行缩进的长度
                wordApp.Selection.ParagraphFormat.Alignment       = MSWord.WdParagraphAlignment.wdAlignParagraphCenter;
                object unite = MSWord.WdUnits.wdStory;
                wordApp.Selection.EndKey(ref unite, ref Nothing);
                strContent = testPaper.Title + "\n";
                wordDoc.Paragraphs.Last.Range.Font.Size = 20;
                wordDoc.Paragraphs.Last.Range.Font.Name = "黑体";
                wordDoc.Paragraphs.Last.Range.Text      = strContent;
                wordApp.Selection.EndKey(ref unite, ref Nothing);
                wordDoc.Paragraphs.Last.Range.Font.Size = 16;
                strContent = testPaper.Subtitle + "\n";
                wordDoc.Paragraphs.Last.Range.Font.Underline = MSWord.WdUnderline.wdUnderlineThick;
                wordDoc.Paragraphs.Last.Range.Text           = strContent;
                int paperQuestionTypeNumber = 1;
                wordApp.Selection.EndKey(ref unite, ref Nothing);
                wordDoc.Paragraphs.Last.Range.Font.Underline = MSWord.WdUnderline.wdUnderlineNone;
                foreach (var paperQuestionType in testPaper.PaperQuestionTypes)
                {
                    wordApp.Selection.ParagraphFormat.Alignment = MSWord.WdParagraphAlignment.wdAlignParagraphLeft;
                    strContent = paperQuestionTypeNumber + "、" + paperQuestionType.PaperQuestionTitle + "\n";
                    bool hasOption = paperQuestionType.QuestionType.HasOption;
                    wordApp.Selection.EndKey(ref unite, ref Nothing);
                    wordDoc.Paragraphs.Last.Range.Font.Size = 14;
                    wordDoc.Paragraphs.Last.Range.Font.Name = "黑体";
                    wordDoc.Paragraphs.Last.Range.Text      = strContent;
                    int examinationQuestionNumber = 1;
                    foreach (var examinationQuestion in paperQuestionType.ExaminationQuestionPaperTypes)
                    {
                        strContent = examinationQuestionNumber + "、" + examinationQuestion.ExaminationQuestion.Content + "\n";
                        wordApp.Selection.EndKey(ref unite, ref Nothing);
                        wordDoc.Paragraphs.Last.Range.Font.Size = 12;
                        wordDoc.Paragraphs.Last.Range.Font.Name = "宋体";
                        if (hasOption)
                        {
                            wordDoc.Paragraphs.Last.Range.Text = strContent;
                            wordApp.Selection.EndKey(ref unite, ref Nothing);
                            OptionItem[] optionItems = examinationQuestion.ExaminationQuestion.OptionItems.ToArray();
                            wordDoc.Paragraphs.Last.Range.Font.Size = 10;
                            if (optionItems[0].OptionContent.Length > 45 || optionItems[1].OptionContent.Length > 45 || optionItems[2].OptionContent.Length > 45 || optionItems[3].OptionContent.Length > 45)
                            {
                                wordDoc.Paragraphs.Last.Range.Text = String.Format(OptionTemplate3, optionItems[0].OptionContent, optionItems[1].OptionContent, optionItems[2].OptionContent, optionItems[3].OptionContent);
                            }
                            else if (optionItems[0].OptionContent.Length > 15 || optionItems[1].OptionContent.Length > 15 || optionItems[2].OptionContent.Length > 15 || optionItems[3].OptionContent.Length > 15)
                            {
                                wordDoc.Paragraphs.Last.Range.Text = String.Format(OptionTemplate2, optionItems[0].OptionContent, optionItems[1].OptionContent, optionItems[2].OptionContent, optionItems[3].OptionContent);
                            }
                            else
                            {
                                wordDoc.Paragraphs.Last.Range.Text = String.Format(OptionTemplate, optionItems[0].OptionContent, optionItems[1].OptionContent, optionItems[2].OptionContent, optionItems[3].OptionContent);
                            }
                        }
                        else
                        {
                            wordDoc.Paragraphs.Last.Range.Text = strContent;
                        }

                        examinationQuestionNumber++;
                    }
                    paperQuestionTypeNumber++;
                }
                object format = MSWord.WdSaveFormat.wdFormatDocument; // office 2007就是wdFormatDocumentDefault
                                                                      //将wordDoc文档对象的内容保存为DOCX文档
                wordDoc.SaveAs(ref path, ref format, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing);
                wordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
                //关闭wordApp组件对象
                wordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
            }
            OpenWord(path);
        }
Example #6
0
        private void button1_Click(object sender, EventArgs e)
        {
            //新建文档
            // Word.Application newapp = new Word.Application();//用这句也能初始化
            Word.Application newapp = new Word.ApplicationClass();
            Word.Document    newdoc;
            object           nothing = System.Reflection.Missing.Value;                                //用于作为函数的默认参数

            newdoc         = newapp.Documents.Add(ref nothing, ref nothing, ref nothing, ref nothing); //生成一个word文档
            newapp.Visible = true;                                                                     //是否显示word程序界面

            //页面设置
            //newdoc.PageSetup.Orientation = Word.WdOrientation.wdOrientLandscape ;
            //newdoc.PageSetup.PageWidth = newapp.CentimetersToPoints(21.0f);
            //newdoc.PageSetup.PageHeight = newapp.CentimetersToPoints(29.7f);
            newdoc.PageSetup.PaperSize      = Word.WdPaperSize.wdPaperA4;
            newdoc.PageSetup.Orientation    = Word.WdOrientation.wdOrientPortrait;
            newdoc.PageSetup.TopMargin      = 57.0f;
            newdoc.PageSetup.BottomMargin   = 57.0f;
            newdoc.PageSetup.LeftMargin     = 57.0f;
            newdoc.PageSetup.RightMargin    = 57.0f;
            newdoc.PageSetup.HeaderDistance = 30.0f;//页眉位置

            //设置页眉
            newapp.ActiveWindow.View.Type              = Word.WdViewType.wdOutlineView;       //视图样式。
            newapp.ActiveWindow.View.SeekView          = Word.WdSeekView.wdSeekPrimaryHeader; //进入页眉设置,其中页眉边距在页面设置中已完成
            newapp.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight;
            //插入页眉图片
            string headerfile = "d:\\header.jpg";

            // this.outpicture(headerfile, Properties.Resources.header);
            Word.InlineShape shape1 = newapp.ActiveWindow.ActivePane.Selection.InlineShapes.AddPicture(headerfile, ref nothing, ref nothing, ref nothing);
            shape1.Height = 30;
            shape1.Width  = 80;
            newapp.ActiveWindow.ActivePane.Selection.InsertAfter("中建东北院");
            //去掉页眉的那条横线
            newapp.ActiveWindow.ActivePane.Selection.ParagraphFormat.Borders[Microsoft.Office.Interop.Word.WdBorderType.wdBorderBottom].LineStyle = Word.WdLineStyle.wdLineStyleNone;
            newapp.ActiveWindow.ActivePane.Selection.Borders[Microsoft.Office.Interop.Word.WdBorderType.wdBorderBottom].Visible = false;
            newapp.ActiveWindow.ActivePane.View.SeekView = Word.WdSeekView.wdSeekMainDocument;//退出页眉设置

            //添加页码
            Word.PageNumbers pns = newapp.Selection.Sections[1].Headers[Microsoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterEvenPages].PageNumbers;
            pns.NumberStyle               = Word.WdPageNumberStyle.wdPageNumberStyleNumberInDash;
            pns.HeadingLevelForChapter    = 0;
            pns.IncludeChapterNumber      = false;
            pns.ChapterPageSeparator      = Word.WdSeparatorType.wdSeparatorHyphen;
            pns.RestartNumberingAtSection = false;
            pns.StartingNumber            = 0;
            object pagenmbetal = Word.WdPageNumberAlignment.wdAlignPageNumberCenter;
            object first       = true;

            newapp.Selection.Sections[1].Footers[Microsoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterEvenPages].PageNumbers.Add(ref pagenmbetal, ref first);

            //文字设置(Selection表示当前选择集,如果当前没有选择对像,则指对光标所在处进行设置)
            newapp.Selection.Font.Size  = 14;
            newapp.Selection.Font.Bold  = 0;
            newapp.Selection.Font.Color = Word.WdColor.wdColorBlack;
            newapp.Selection.Font.Name  = "宋体";

            //段落设置
            newapp.Selection.ParagraphFormat.LineSpacingRule = Word.WdLineSpacing.wdLineSpaceExactly;
            newapp.Selection.ParagraphFormat.LineSpacing     = 20;
            newapp.Selection.ParagraphFormat.Alignment       = Word.WdParagraphAlignment.wdAlignParagraphLeft;
            newapp.Selection.ParagraphFormat.FirstLineIndent = 30;
            //  newdoc.Content.InsertAfter(WindowsFormsApplication1.Properties.Resources.PreViewWords);



            //插入公式
            object oEndOfDoc = "\\endofdoc";

            Word.Range rang1             = newdoc.Bookmarks.get_Item(ref oEndOfDoc).Range;
            object     fieldType         = Word.WdFieldType.wdFieldEmpty;
            object     formula           = @"eq \i(a,b,ξxdx)";
            object     presrveFormatting = false;

            rang1.Text           = formula.ToString();
            rang1.Font.Size      = 14;
            rang1.Font.Bold      = 0;
            rang1.Font.Subscript = 0;
            rang1.Font.Color     = Word.WdColor.wdColorBlue;
            rang1.Font.Name      = "宋体";
            rang1.ParagraphFormat.LineSpacingRule = Word.WdLineSpacing.wdLineSpaceSingle;
            rang1.ParagraphFormat.Alignment       = Word.WdParagraphAlignment.wdAlignParagraphRight;
            newdoc.Fields.Add(rang1, ref fieldType, ref formula, ref presrveFormatting);

            //将文档的前三个字替换成"asdfasdf",并将其颜色设为蓝色
            object start = 0;
            object end   = 3;

            Word.Range rang2 = newdoc.Range(ref start, ref end);
            rang2.Font.Color = Word.WdColor.wdColorBlue;
            rang2.Text       = "as签";

            //将文档开头的"as"替换成"袁波"
            rang1.Start = 0;
            rang1.End   = 2;
            rang1.Text  = "这是一个";
            rang1.InsertAfter("书");
            //rang1.Select();
            object codirection = Word.WdCollapseDirection.wdCollapseStart;

            rang1.Collapse(ref codirection);//将rang1的起点和终点都定于起点或终点

            //对前三个字符进行加粗
            newdoc.Range(ref start, ref end).Bold = 1;
            object rang = rang2;

            newdoc.Bookmarks.Add("yb", ref rang);


            object unite = Word.WdUnits.wdStory;

            newapp.Selection.EndKey(ref unite, ref nothing);//将光标移至文末
            newapp.Selection.Font.Size = 10;
            newapp.Selection.TypeText("...............................(式1)\n");


            //插入图片
            newapp.Selection.EndKey(ref unite, ref nothing);//将光标移至文末
            //newapp.Selection.HomeKey(ref unite, ref nothing);//将光标移至文开头
            newapp.Selection.ParagraphFormat.LineSpacingRule = Word.WdLineSpacing.wdLineSpaceSingle;
            newapp.Selection.ParagraphFormat.Alignment       = Word.WdParagraphAlignment.wdAlignParagraphCenter;
            object LinkToFile       = false;
            object SaveWithDocument = true;
            object Anchor           = newapp.Selection.Range;
            string picname          = "d:\\kk.jpg";

            //     this.outpicture(picname, Properties.Resources.IMG_2169);
            newdoc.InlineShapes.AddPicture(picname, ref LinkToFile, ref SaveWithDocument, ref Anchor);
            newdoc.InlineShapes[1].Height = 200;
            newdoc.InlineShapes[1].Width  = 200;
            newdoc.Content.InsertAfter("\n");
            newapp.Selection.EndKey(ref unite, ref nothing);//将光标移至文末
            newapp.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
            newapp.Selection.Font.Size = 10;
            newapp.Selection.TypeText("图1 袁冶\n");
            newapp.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight;
            newdoc.Content.InsertAfter("\n");
            newdoc.Content.InsertAfter("\n");

            //用这种方式也可以插入公式,并且这种方法更简单
            newapp.Selection.Font.Size = 14;
            newapp.Selection.InsertFormula(ref formula, ref nothing);
            newapp.Selection.Font.Size = 10;
            newapp.Selection.TypeText("..............................(式2)\n");
            newapp.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
            newapp.Selection.TypeText("表1 电子产品\n");

            //插入表格
            Word.Table table1 = newdoc.Tables.Add(newapp.Selection.Range, 4, 3, ref nothing, ref nothing);
            newdoc.Tables[1].Cell(1, 1).Range.Text = "产品\n项目";
            newdoc.Tables[1].Cell(1, 2).Range.Text = "电脑";
            newdoc.Tables[1].Cell(1, 3).Range.Text = "手机";
            newdoc.Tables[1].Cell(2, 1).Range.Text = "重量(kg)";
            newdoc.Tables[1].Cell(3, 1).Range.Text = "价格(元)";
            newdoc.Tables[1].Cell(4, 1).Range.Text = "共同信息";
            newdoc.Tables[1].Cell(4, 2).Range.Text = "信息A";
            newdoc.Tables[1].Cell(4, 3).Range.Text = "信息B";


            table1.Select();
            table1.Rows.Alignment = Word.WdRowAlignment.wdAlignRowCenter;//整个表格居中
            newapp.Selection.Cells.VerticalAlignment   = Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;
            newapp.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
            newapp.Selection.Cells.HeightRule          = Word.WdRowHeightRule.wdRowHeightExactly;
            newapp.Selection.Cells.Height                          = 40;
            table1.Rows[2].Height                                  = 20;
            table1.Rows[3].Height                                  = 20;
            table1.Rows[4].Height                                  = 20;
            table1.Range.ParagraphFormat.Alignment                 = Word.WdParagraphAlignment.wdAlignParagraphCenter;
            newapp.Selection.Cells.Width                           = 150;
            table1.Columns[1].Width                                = 75;
            table1.Cell(1, 1).Range.ParagraphFormat.Alignment      = Word.WdParagraphAlignment.wdAlignParagraphRight;
            table1.Cell(1, 1).Range.Paragraphs[2].Format.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft;



            //表头斜线
            table1.Cell(1, 1).Borders[Microsoft.Office.Interop.Word.WdBorderType.wdBorderDiagonalDown].Visible   = true;
            table1.Cell(1, 1).Borders[Microsoft.Office.Interop.Word.WdBorderType.wdBorderDiagonalDown].Color     = Word.WdColor.wdColorGreen;
            table1.Cell(1, 1).Borders[Microsoft.Office.Interop.Word.WdBorderType.wdBorderDiagonalDown].LineWidth = Word.WdLineWidth.wdLineWidth050pt;

            //表格边框
            table1.Borders[Microsoft.Office.Interop.Word.WdBorderType.wdBorderHorizontal].Visible   = true;
            table1.Borders[Microsoft.Office.Interop.Word.WdBorderType.wdBorderHorizontal].Color     = Word.WdColor.wdColorGreen;
            table1.Borders[Microsoft.Office.Interop.Word.WdBorderType.wdBorderHorizontal].LineWidth = Word.WdLineWidth.wdLineWidth050pt;

            table1.Borders[Microsoft.Office.Interop.Word.WdBorderType.wdBorderVertical].Visible   = true;
            table1.Borders[Microsoft.Office.Interop.Word.WdBorderType.wdBorderVertical].Color     = Word.WdColor.wdColorGreen;
            table1.Borders[Microsoft.Office.Interop.Word.WdBorderType.wdBorderVertical].LineWidth = Word.WdLineWidth.wdLineWidth050pt;

            table1.Borders[Microsoft.Office.Interop.Word.WdBorderType.wdBorderLeft].Visible   = true;
            table1.Borders[Microsoft.Office.Interop.Word.WdBorderType.wdBorderLeft].Color     = Word.WdColor.wdColorGreen;
            table1.Borders[Microsoft.Office.Interop.Word.WdBorderType.wdBorderLeft].LineWidth = Word.WdLineWidth.wdLineWidth050pt;
            table1.Borders[Microsoft.Office.Interop.Word.WdBorderType.wdBorderLeft].LineStyle = Word.WdLineStyle.wdLineStyleDoubleWavy;

            table1.Borders[Microsoft.Office.Interop.Word.WdBorderType.wdBorderRight].Visible   = true;
            table1.Borders[Microsoft.Office.Interop.Word.WdBorderType.wdBorderRight].Color     = Word.WdColor.wdColorGreen;
            table1.Borders[Microsoft.Office.Interop.Word.WdBorderType.wdBorderRight].LineWidth = Word.WdLineWidth.wdLineWidth050pt;
            table1.Borders[Microsoft.Office.Interop.Word.WdBorderType.wdBorderRight].LineStyle = Word.WdLineStyle.wdLineStyleDoubleWavy;

            table1.Borders[Microsoft.Office.Interop.Word.WdBorderType.wdBorderBottom].Visible   = true;
            table1.Borders[Microsoft.Office.Interop.Word.WdBorderType.wdBorderBottom].Color     = Word.WdColor.wdColorGreen;
            table1.Borders[Microsoft.Office.Interop.Word.WdBorderType.wdBorderBottom].LineWidth = Word.WdLineWidth.wdLineWidth050pt;
            table1.Borders[Microsoft.Office.Interop.Word.WdBorderType.wdBorderBottom].LineStyle = Word.WdLineStyle.wdLineStyleDouble;

            table1.Borders[Microsoft.Office.Interop.Word.WdBorderType.wdBorderTop].Visible   = true;
            table1.Borders[Microsoft.Office.Interop.Word.WdBorderType.wdBorderTop].Color     = Word.WdColor.wdColorGreen;
            table1.Borders[Microsoft.Office.Interop.Word.WdBorderType.wdBorderTop].LineWidth = Word.WdLineWidth.wdLineWidth050pt;
            table1.Borders[Microsoft.Office.Interop.Word.WdBorderType.wdBorderTop].LineStyle = Word.WdLineStyle.wdLineStyleDouble;

            //合并单元格
            newdoc.Tables[1].Cell(4, 2).Merge(table1.Cell(4, 3));

            //删除图片
            this.delpictfile(headerfile);
            this.delpictfile(picname);


            //保存文档
            object name = "c:\\yb3.doc";

            newdoc.SaveAs(ref name, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing,
                          ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing,
                          ref nothing, ref nothing);

            //关闭文档
            object saveOption = Word.WdSaveOptions.wdDoNotSaveChanges;

            newdoc.Close(ref nothing, ref nothing, ref nothing);
            newapp.Application.Quit(ref saveOption, ref nothing, ref nothing);
            newdoc = null;
            newapp = null;
            ShellExecute(IntPtr.Zero, "open", "c:\\yb3.doc", "", "", 3);
        }