首页 > 代码库 > C#生成word

C#生成word

  1 using Microsoft.Office.Interop.Word;  2 using System;  3 using System.Collections.Generic;  4 using System.ComponentModel;  5 using System.Data;  6 using System.Drawing;  7 using System.IO;  8 using System.Linq;  9 using System.Text; 10 using System.Threading.Tasks; 11 using System.Windows.Forms; 12  13 namespace WindowsFormsApplication2 14 { 15     public partial class Form1 : Form 16     { 17         public Form1() 18         { 19             InitializeComponent(); 20         } 21  22         public string CreateWordFile(string CheckedInfo) 23         { 24             string message = ""; 25             try 26             { 27                 Object Nothing = System.Reflection.Missing.Value; 28                 Directory.CreateDirectory("C:/CNSI");  //创建文件所在目录 29                 string name = "CNSI_" + DateTime.Now.ToLongDateString() + ".doc"; 30                 object filename = "C://CNSI//" + name;  //文件保存路径 31                 //创建Word文档 32                 Microsoft.Office.Interop.Word.Application WordApp = new Microsoft.Office.Interop.Word.ApplicationClass(); 33                 Microsoft.Office.Interop.Word.Document WordDoc = WordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing); 34  35                 //添加页眉 36                 WordApp.ActiveWindow.View.Type = WdViewType.wdOutlineView; 37                 WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekPrimaryHeader; 38                 WordApp.ActiveWindow.ActivePane.Selection.InsertAfter("[页眉内容]"); 39                 WordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphRight;//设置右对齐 40                 WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekMainDocument;//跳出页眉设置 41  42                 WordApp.Selection.ParagraphFormat.LineSpacing = 15f;//设置文档的行间距 43  44                 //移动焦点并换行 45                 object count = 14; 46                 object WdLine = Microsoft.Office.Interop.Word.WdUnits.wdLine;//换一行; 47                 WordApp.Selection.MoveDown(ref WdLine, ref count, ref Nothing);//移动焦点 48                 WordApp.Selection.TypeParagraph();//插入段落 49  50                 //文档中创建表格 51                 Microsoft.Office.Interop.Word.Table newTable = WordDoc.Tables.Add(WordApp.Selection.Range, 12, 3, ref Nothing, ref Nothing); 52                 //设置表格样式 53                 newTable.Borders.OutsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleThickThinLargeGap; 54                 newTable.Borders.InsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleSingle; 55                 newTable.Columns[1].Width = 100f; 56                 newTable.Columns[2].Width = 220f; 57                 newTable.Columns[3].Width = 105f; 58  59                 //填充表格内容 60                 newTable.Cell(1, 1).Range.Text = "产品详细信息表"; 61                 newTable.Cell(1, 1).Range.Bold = 2;//设置单元格中字体为粗体 62                 //合并单元格 63                 newTable.Cell(1, 1).Merge(newTable.Cell(1, 3)); 64                 WordApp.Selection.Cells.VerticalAlignment = Microsoft.Office.Interop.Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;//垂直居中 65                 WordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;//水平居中 66  67                 //填充表格内容 68                 newTable.Cell(2, 1).Range.Text = "产品基本信息"; 69                 newTable.Cell(2, 1).Range.Font.Color = Microsoft.Office.Interop.Word.WdColor.wdColorDarkBlue;//设置单元格内字体颜色 70                 //合并单元格 71                 newTable.Cell(2, 1).Merge(newTable.Cell(2, 3)); 72                 WordApp.Selection.Cells.VerticalAlignment = Microsoft.Office.Interop.Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter; 73  74                 //填充表格内容 75                 newTable.Cell(3, 1).Range.Text = "品牌名称:"; 76                 newTable.Cell(3, 2).Range.Text = "BrandName"; 77                 //纵向合并单元格 78                 newTable.Cell(3, 3).Select();//选中一行 79                 object moveUnit = Microsoft.Office.Interop.Word.WdUnits.wdLine; 80                 object moveCount = 5; 81                 object moveExtend = Microsoft.Office.Interop.Word.WdMovementType.wdExtend; 82                 WordApp.Selection.MoveDown(ref moveUnit, ref moveCount, ref moveExtend); 83                 WordApp.Selection.Cells.Merge(); 84                 //插入图片 85                 string FileName = @"c:\picture.jpg";//图片所在路径 86                 object LinkToFile = false; 87                 object SaveWithDocument = true; 88                 object Anchor = WordDoc.Application.Selection.Range; 89                 WordDoc.Application.ActiveDocument.InlineShapes.AddPicture(FileName, ref LinkToFile, ref SaveWithDocument, ref Anchor); 90                 WordDoc.Application.ActiveDocument.InlineShapes[1].Width = 100f;//图片宽度 91                 WordDoc.Application.ActiveDocument.InlineShapes[1].Height = 100f;//图片高度 92                 //将图片设置为四周环绕型 93                 Microsoft.Office.Interop.Word.Shape s = WordDoc.Application.ActiveDocument.InlineShapes[1].ConvertToShape(); 94                 s.WrapFormat.Type = Microsoft.Office.Interop.Word.WdWrapType.wdWrapSquare; 95  96                 newTable.Cell(12, 1).Range.Text = "产品特殊属性"; 97                 newTable.Cell(12, 1).Merge(newTable.Cell(12, 3)); 98                 //在表格中增加行 99                 WordDoc.Content.Tables[1].Rows.Add(ref Nothing);100 101                 WordDoc.Paragraphs.Last.Range.Text = "文档创建时间:" + DateTime.Now.ToString();//“落款”102                 WordDoc.Paragraphs.Last.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphRight;103 104                 //文件保存105                 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);106                 WordDoc.Close(ref Nothing, ref Nothing, ref Nothing);107                 WordApp.Quit(ref Nothing, ref Nothing, ref Nothing);108                 message = name + "文档生成成功,以保存到C:CNSI下";109             }110             catch(Exception e)111             {112                 throw e;113                 message = "文件导出异常!";114             }115             return message;116         }117 118         private void button1_Click_1(object sender, EventArgs e)119         {120             CreateWordFile("c:\\dd.doc");121         }       122     }123 }
View Code

注意 :需要引用 COM组件Microsoft.Office.Interop.Word

如果编译出现"无法嵌入互操作类型 请改用适用的接口"错误 右键上述组件 点击属性  嵌入互操作类型改为false