C# 操作word

C#  操作word
  public string CreateWordFile()
        {
            string message = "";
            try
            {
                Object Nothing = System.Reflection.Missing.Value;
                Directory.CreateDirectory("D:/CNSI");  //创建文件所在目录

                string name = "CNSI_" + DateTime.Now.ToLongDateString() + ".doc";//文件名
                object filename = "D://CNSI//" + name;  //文件保存路径

                //创建Word文档
                Microsoft.Office.Interop.Word.Application WordApp = new Microsoft.Office.Interop.Word.ApplicationClass();
                Microsoft.Office.Interop.Word.Document WordDoc = WordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);

                //添加页眉
                WordApp.ActiveWindow.View.Type = WdViewType.wdOutlineView;
                WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekPrimaryHeader;
                WordApp.ActiveWindow.ActivePane.Selection.InsertAfter("[页眉内容]");
                WordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphRight;//设置右对齐
                WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekMainDocument;//跳出页眉设置

                WordApp.Selection.ParagraphFormat.LineSpacing = 15f;//设置文档的行间距

                //移动焦点并换行
                object count = 14;
                object WdLine = Microsoft.Office.Interop.Word.WdUnits.wdLine;//换一行;
                WordApp.Selection.MoveDown(ref WdLine, ref count, ref Nothing);//移动焦点
                WordApp.Selection.TypeParagraph();//插入段落


                Microsoft.Office.Interop.Word.Paragraph oPara1;
                oPara1 = WordDoc.Content.Paragraphs.Add(ref Nothing);
                oPara1.Range.Text = "2013年9月(用户单位)服务情况小结";
                oPara1.Range.Font.Bold = 1;//加粗
                oPara1.Range.Font.Name = "黑体";
                oPara1.Range.Font.Size = 16;
                oPara1.Range.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;
                oPara1.Range.InsertParagraphAfter();


                Microsoft.Office.Interop.Word.Paragraph oPara2;
                oPara2 = WordDoc.Content.Paragraphs.Add(ref Nothing);
                oPara2.Range.Text = "2013年10月8日";
                oPara2.Range.Font.Bold = 1;
                oPara2.Range.Font.Name = "黑体";
                oPara2.Range.Font.Size = (float)10.5;
                oPara2.Range.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphRight;
                oPara2.Range.InsertParagraphAfter();


                Microsoft.Office.Interop.Word.Paragraph oPara3;
                oPara3 = WordDoc.Content.Paragraphs.Add(ref Nothing);
                oPara3.Range.Text = "1. 用户基本信息";
                oPara3.Range.Font.Bold = 1;
                oPara3.Range.Font.Name = "黑体";
                oPara3.Range.Font.Size = 14;
                oPara3.Range.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphLeft;
                oPara3.Range.InsertParagraphAfter();


                Microsoft.Office.Interop.Word.Paragraph oPara4;
                oPara4 = WordDoc.Content.Paragraphs.Add(ref Nothing);
                oPara4.Range.Text = "用户:***";
                oPara4.Range.Font.Bold = 0;
                oPara4.Range.Font.Name = "宋体";
                oPara4.Range.Font.Size = (float)10.5;
                oPara4.Range.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphLeft;
                oPara4.Range.InsertParagraphAfter();

                Microsoft.Office.Interop.Word.Paragraph oPara5;
                oPara5 = WordDoc.Content.Paragraphs.Add(ref Nothing);
                oPara5.Range.Text = "联系人:***";
                oPara5.Range.Font.Bold = 0;
                oPara5.Range.Font.Name = "宋体";
                oPara5.Range.Font.Size = (float)10.5;
                oPara5.Range.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphLeft;
                oPara5.Range.InsertParagraphAfter();

                Microsoft.Office.Interop.Word.Paragraph oPara6;
                oPara6 = WordDoc.Content.Paragraphs.Add(ref Nothing);
                oPara6.Range.Text = "账号有效时间:2013.09.05 - ";
                oPara6.Range.Font.Bold = 0;
                oPara6.Range.Font.Name = "宋体";
                oPara6.Range.Font.Size = (float)10.5;
                oPara6.Range.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphLeft;
                oPara6.Range.InsertParagraphAfter();


                Microsoft.Office.Interop.Word.Paragraph oPara7;
                oPara7 = WordDoc.Content.Paragraphs.Add(ref Nothing);
                oPara7.Range.Text = "账号:***";
                oPara7.Range.Font.Name = "宋体";
                oPara7.Range.Font.Bold = 0;
                oPara7.Range.Font.Size = (float)10.5;
                oPara7.Range.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphLeft;
                oPara7.Range.InsertParagraphAfter();


                Microsoft.Office.Interop.Word.Paragraph oPara8;
                oPara8 = WordDoc.Content.Paragraphs.Add(ref Nothing);
                oPara8.Range.Text = "可用资源:";
                oPara8.Range.Font.Name = "宋体";
                oPara8.Range.Font.Size = (float)10.5;
                oPara8.Range.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphLeft;
                oPara8.Range.InsertParagraphAfter();



                WordApp.Selection.MoveDown(ref WdLine, ref count, ref Nothing);//移动焦点
                WordApp.Selection.TypeParagraph();//插入段落


                CreateTabel(WordDoc, WordApp, 2, 3, "");


                WordApp.Selection.MoveDown(ref WdLine, ref count, ref Nothing);//移动焦点
                WordApp.Selection.TypeParagraph();//插入段落


                //文件保存
                WordDoc.SaveAs(ref filename, 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);
                WordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
                WordApp.Quit(ref Nothing, ref Nothing, ref Nothing);

                message = name + "文档生成成功,以保存到D:CNSI下";
                Response.Write(message);
            }
            catch
            {
                message = "文件导出异常!";
            }
            return message;
        }



        Microsoft.Office.Interop.Word.WdColor GetColor(Color c)
        {
            UInt32 R = 0x1, G = 0x100, B = 0x10000;
            return (Microsoft.Office.Interop.Word.WdColor)(R * c.R + G * c.G + B * c.B);
        }



        public void CreateTabel(Microsoft.Office.Interop.Word.Document WordDoc, Microsoft.Office.Interop.Word.Application WordApp,int rows,int cells,string value)
        {
            Object Nothing = System.Reflection.Missing.Value;
            Microsoft.Office.Interop.Word.Table newTable = WordDoc.Tables.Add(WordApp.Selection.Range, rows, cells, ref Nothing, ref Nothing);
            //设置表格
            Color u = Color.FromArgb(79, 129, 189);

            newTable.Cell(1, 1).Range.Shading.ForegroundPatternColor = GetColor(u);
            newTable.Cell(1, 2).Range.Shading.ForegroundPatternColor = GetColor(u);
            newTable.Cell(1, 3).Range.Shading.ForegroundPatternColor = GetColor(u);// Microsoft.Office.Interop.Word.WdColor.wdColorTurquoise;//背景颜色
            newTable.Cell(1, 1).Range.Font.Color = Microsoft.Office.Interop.Word.WdColor.wdColorWhite;//字体颜色
            newTable.Cell(1, 2).Range.Font.Color = Microsoft.Office.Interop.Word.WdColor.wdColorWhite;//字体颜色
            newTable.Cell(1, 3).Range.Font.Color = Microsoft.Office.Interop.Word.WdColor.wdColorWhite;//字体颜色


            newTable.Borders.OutsideColor = GetColor(u);
            newTable.Borders.InsideColor = GetColor(u);
            newTable.Borders.OutsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleSingle;
            newTable.Borders.InsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleSingle;
            newTable.Columns[1].Width = 100f;
            newTable.Columns[2].Width = 100f;
            newTable.Columns[3].Width = 220f;

            newTable.Cell(1, 1).Range.Text = "CPU";
            newTable.Cell(1, 2).Range.Text = "磁盘空间";
            newTable.Cell(1, 3).Range.Text = "队列";
            //在表格中增加行
            //WordDoc.Content.Tables[1].Rows.Add(ref Nothing);
            newTable.Cell(2, 1).Range.Text = "48";
            newTable.Cell(2, 2).Range.Text = "1 TB";
            newTable.Cell(2, 3).Range.Text = "Ansys/CFX/Fluent/Nastran/Abaqus";

            newTable.Borders[WdBorderType.wdBorderVertical].LineStyle = WdLineStyle.wdLineStyleNone;
            newTable.Borders[WdBorderType.wdBorderLeft].Color = GetColor(u);
            newTable.Borders[WdBorderType.wdBorderRight].Color = GetColor(u);
            newTable.Borders[WdBorderType.wdBorderBottom].Color = GetColor(u);
            newTable.Borders[WdBorderType.wdBorderTop].Color = GetColor(u);
            newTable.Borders[WdBorderType.wdBorderHorizontal].Color = GetColor(u);
            //oDoc.Tables[1].Cell(1, 1).Range.Font.Color = Microsoft.Office.Interop.Word.WdColor.wdColorBlue;//字体颜色
            //newTable.Range.Borders[WdBorderType.wdBorderVertical].LineStyle = WdLineStyle.wdLineStyleNone;
        }
C#  操作word

C# 操作word

上一篇:Activity 通知


下一篇:MVC4研发中遇到问题【持续总结....】