• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C#操作word

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
  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;
  object oStart = 14;
                object oEnd = oPara1.Range.Start + 22;
                Microsoft.Office.Interop.Word.Range rBold = WordDoc.Range(ref oStart, ref oEnd);
                rBold.Font.Underline = Microsoft.Office.Interop.Word.WdUnderline.wdUnderlineSingle;

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; }

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C#多线程锁定全局变量发布时间:2022-07-13
下一篇:
C#如何在程序里定时启动任务发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap