Microsoft.Office.Interop.Word通过模板生成文档
来源:互联网 发布:js dom 大小改变事件 编辑:程序博客网 时间:2024/06/15 10:05
本博客实现通过模板动态生成word文档。
- C#
- Visual Studio 2013
- Microsoft.Office.Interop.Word.dll –>需要添加该引用,可以在网上下载
关键代码如下:
public void ExportWord() { //取得模板路径 string wordTemplatePath = @"C:\XXXXX\模板.doc"; //导出文件名称 fileName = "我的word" + System.DateTime.Now.ToString("yyyMMddHHmmss") + ".doc"; //导出文件路径 string outPutFilePath = @"C:\XXXXX\" + fileName; //创建word对象 object missing = System.Reflection.Missing.Value; object readOnly = false; Word.Application app = new Word.Application(); Word.Document doc = null; File.Copy(wordTemplatePath, outPutFilePath, true);//true 相同文件名时,覆盖 //打开doc object tempword = (object)wordTemplatePath; object output = (object)outPutFilePath; doc = app.Documents.Open(ref tempword, ref missing, ref readOnly, 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); doc.Activate(); //插入4行4列表格 WdLineStyle OutStyle = WdLineStyle.wdLineStyleThickThinLargeGap; WdLineStyle InStyle = WdLineStyle.wdLineStyleSingle; AddSimpleTable(app, doc, 4, 4, OutStyle, InStyle); //WdSaveFormat为Word 2003文档的保存格式 object format = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatDocument; //WdSaveFormat为Word 2007文档的保存格式 //object format =Microsoft.Office.Interop.WdSaveFormat.wdFormatDocumentDefault; doc.SaveAs(ref output, ref format, 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); object IsSave = true; doc.Close(ref IsSave, ref missing, ref missing);//关闭word app.Quit(ref IsSave, ref missing, ref missing); System.Runtime.InteropServices.Marshal.ReleaseComObject(app); //释放内存 } //插入表格 public static void AddSimpleTable(Application WordApp, Document WordDoc, int numrows, int numcolumns, WdLineStyle outStyle, WdLineStyle intStyle) { object tl = "tl";//tl为模板中需要插入表格位置处的书签名 Word.Range startRange = WordDoc.Bookmarks.get_Item(ref tl).Range; Object Nothing = System.Reflection.Missing.Value; //文档中创建表格 Microsoft.Office.Interop.Word.Table newTable = WordDoc.Tables.Add(startRange, numrows, numcolumns, ref Nothing, ref Nothing); //设置表格样式 newTable.Borders.OutsideLineStyle = Word.WdLineStyle.wdLineStyleSingle; newTable.Borders.OutsideLineWidth = WdLineWidth.wdLineWidth150pt;//外边框,此处样式可以根据需求更改 newTable.Borders.InsideLineStyle = intStyle; newTable.Columns[1].Width = 112f; newTable.Columns[2].Width = 112f; newTable.Columns[3].Width = 112f; newTable.Columns[4].Width = 112f; //填充表格内容 newTable.Cell(1, 1).Range.Text = "测试"; //newTable.Cell(1, 1).Range.Bold = 2;//设置单元格中字体为粗体 //newTable.Cell(1, 1).Range.Shading.BackgroundPatternColor = WdColor.wdColorGray10; newTable.Cell(1, 1).Shading.BackgroundPatternColor = WdColor.wdColorGray10;//背景底纹 //合并单元格 newTable.Cell(1, 1).Merge(newTable.Cell(1, 2)); //newTable.Cell(1, 1).Select();//选中一行 //WordApp.Selection.ParagraphFormat.SpaceBefore = 0f; //WordApp.Selection.ParagraphFormat.SpaceAfter = 0f; //WordApp.Selection.Cells.VerticalAlignment = WdCellVerticalAlignment.wdCellAlignVerticalCenter;//单元格垂直居中 ////WordApp.Selection.ParagraphFormat.LineSpacing = 16f;//固定行距16磅 //WordApp.Selection.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;//水平居中 newTable.Cell(1, 2).Range.Text = "李白"; newTable.Cell(1, 2).Shading.BackgroundPatternColor = WdColor.wdColorGray10; newTable.Cell(1, 3).Range.Text = "杜甫"; newTable.Cell(1, 3).Shading.BackgroundPatternColor = WdColor.wdColorGray10; newTable.Cell(2, 1).Range.Text = "唐朝"; //纵向合并单元格 newTable.Cell(2, 1).Select();//选中一行 object moveUnit = WdUnits.wdLine; object moveCount = 2; object moveExtend = WdMovementType.wdExtend; WordApp.Selection.MoveDown(ref moveUnit, ref moveCount, ref moveExtend); WordApp.Selection.Cells.Merge(); newTable.Cell(2, 2).Range.Text = "诗人"; newTable.Cell(2, 3).Range.Text = "12138"; newTable.Cell(2, 3).Select(); object moveUnit2 = WdUnits.wdLine; object moveCount2 = 2; object moveExtend2 = WdMovementType.wdExtend; WordApp.Selection.MoveDown(ref moveUnit2, ref moveCount2, ref moveExtend2); WordApp.Selection.Cells.Merge(); newTable.Cell(2, 4).Range.Text = "Friday"; newTable.Cell(3, 2).Range.Text = "Month"; newTable.Cell(3, 2).Select(); object moveUnit1 = WdUnits.wdLine; object moveCount1 = 1; object moveExtend1 = WdMovementType.wdExtend; WordApp.Selection.MoveDown(ref moveUnit1, ref moveCount1, ref moveExtend1); WordApp.Selection.Cells.Merge(); newTable.Cell(3, 4).Range.Text = "温柔"; newTable.Cell(3, 4).Select(); object moveUnit3 = WdUnits.wdLine; object moveCount3 = 1; object moveExtend3 = WdMovementType.wdExtend; WordApp.Selection.MoveDown(ref moveUnit3, ref moveCount3, ref moveExtend3); WordApp.Selection.Cells.Merge(); newTable.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;//水平居中 newTable.Range.Cells.VerticalAlignment = WdCellVerticalAlignment.wdCellAlignVerticalCenter;//垂直居中 newTable.Rows.Alignment = WdRowAlignment.wdAlignRowCenter;//表格整体居中 //在书签位置插入段落 object para = "paratag"; Word.Range parastartRange = WordApp.ActiveDocument.Bookmarks.get_Item(ref para).Range; object oparaRange = parastartRange; Microsoft.Office.Interop.Word.Paragraph oPara1; oPara1 = WordDoc.Content.Paragraphs.Add(ref oparaRange); oPara1.Range.Text = "1、要是能重来;" + '\n' +"2、我要当李白。"; //oPara1.Range.Font.Bold = 1; //oPara1.Format.SpaceAfter = 14; //24 pt spacing after paragraph. //oPara1.Range.InsertParagraphAfter(); ////插入图片 //string FileName = @"C:/1.jpg";//图片所在路径 //object Anchor = WordDoc.Application.Selection.Range; //float Width = 200f;//图片宽度 //float Height = 200f;//图片高度 ////将图片设置为四周环绕型 //WdWrapType wdWrapType = Microsoft.Office.Interop.Word.WdWrapType.wdWrapSquare; //AddPic.AddSimplePic(WordDoc, FileName, Width, Height, Anchor, wdWrapType); //newTable.Cell(12, 1).Range.Text = "产品特殊属性"; //newTable.Cell(12, 1).Merge(newTable.Cell(12, 3)); //在表格中增加行 //WordDoc.Content.Tables[1].Rows.Add(ref Nothing); }
阅读全文
0 0
- Microsoft.Office.Interop.Word通过模板生成文档
- Microsoft.Office.Interop.Word 创建word
- Microsoft.Office.Interop.Word 创建word
- Word转Html ( Microsoft.Office.Interop.Word )
- Microsoft.Office.Interop.Word 创建word
- 利用.NET提供的Microsoft.Office.Interop.Word插件来填充资料给WORD模板
- 关于Microsoft.Office.Interop.Word类库
- 系统找不到 Microsoft.Office.Interop.Word ---解决方案
- 解决引用Microsoft.Office.Core 和Microsoft.Office.Interop.Word
- WinForm利用Microsoft.Office.Interop.Word來合併多個Word檔
- [C#] 操作WORD檔(Microsoft.Office.Interop.Word)(doc檔)
- Microsoft.Office.Interop.Word 对word 的基本操作
- Microsoft.Office.Interop.Word 对word 的基本操作
- java poi通过模板生成word文档
- C#简繁体转换方法(Microsoft.Office.Interop.Word)
- C#简繁体转换方法(Microsoft.Office.Interop.Word)
- C#简繁体转换方法(Microsoft.Office.Interop.Word)
- 类型“Microsoft.Office.Interop.Word.ApplicationClass”未定义构造函数
- nodejs初步学习笔记
- sqlserver常用查询语句
- 自定义控件之水波纹效果
- [uboot] (番外篇)uboot 驱动模型
- 【常用函数封装】PHP没有pid的目录树
- Microsoft.Office.Interop.Word通过模板生成文档
- Echarts实现省份下点击跳转到指定链接
- 面试整理
- 项目中存在的问题
- JVM——Java类加载机制总结
- Linux的top命令
- 进程与线程
- mysql BigDecimal 加减乘除
- Java关键词final和static