`
javatoyou
  • 浏览: 1008504 次
  • 性别: Icon_minigender_2
  • 来自: 北京
文章分类
社区版块
存档分类
最新评论

C#也能动态生成Word文档并填充数据

 
阅读更多

  要使用C#操作word,首先要添加引用:

1、添加引用->COM->Microsoft Word 11.0 Object Library

2、在.cs文件中添加

using Word;
下面的例子中包括C#对Word文档的创建、插入表格、设置样式等操作:

(例子中代码有些涉及数据信息部分被省略,重要是介绍一些C#操作word文档的方法)

public string CreateWordFile( string CheckedInfo)
... {
string message = "" ;
try
... {
ObjectNothing
= System.Reflection.Missing.Value;
Directory.CreateDirectory(
" C:/CNSI " ); // 创建文件所在目录
string name = " CNSI_ " + DateTime.Now.ToShortString() + " .doc " ;
object filename = " C://CNSI// " + name; // 文件保存路径
// 创建Word文档
Word.ApplicationWordApp = new Word.ApplicationClass();
Word.DocumentWordDoc
= 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
= Word.WdParagraphAlignment.wdAlignParagraphRight; // 设置右对齐
WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekMainDocument; // 跳出页眉设置

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

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

// 文档中创建表格
Word.TablenewTable = WordDoc.Tables.Add(WordApp.Selection.Range, 12 , 3 , ref Nothing, ref Nothing);
// 设置表格样式
newTable.Borders.OutsideLineStyle = Word.WdLineStyle.wdLineStyleThickThinLargeGap;
newTable.Borders.InsideLineStyle
= Word.WdLineStyle.wdLineStyleSingle;
newTable.Columns[
1 ].Width = 100f;
newTable.Columns[
2 ].Width = 220f;
newTable.Columns[
3 ].Width = 105f;

// 填充表格内容
newTable.Cell( 1 , 1 ).Range.Text = " 产品详细信息表 " ;
newTable.Cell(
1 , 1 ).Range.Bold = 2 ; // 设置单元格中字体为粗体
// 合并单元格
newTable.Cell( 1 , 1 ).Merge(newTable.Cell( 1 , 3 ));
WordApp.Selection.Cells.VerticalAlignment
= Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter; // 垂直居中
WordApp.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter; // 水平居中

// 填充表格内容
newTable.Cell( 2 , 1 ).Range.Text = " 产品基本信息 " ;
newTable.Cell(
2 , 1 ).Range.Font.Color = Word.WdColor.wdColorDarkBlue; // 设置单元格内字体颜色
// 合并单元格
newTable.Cell( 2 , 1 ).Merge(newTable.Cell( 2 , 3 ));
WordApp.Selection.Cells.VerticalAlignment
= Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;

// 填充表格内容
newTable.Cell( 3 , 1 ).Range.Text = " 品牌名称: " ;
newTable.Cell(
3 , 2 ).Range.Text = BrandName;
// 纵向合并单元格
newTable.Cell( 3 , 3 ).Select(); // 选中一行
object moveUnit = Word.WdUnits.wdLine;
object moveCount = 5 ;
object moveExtend = Word.WdMovementType.wdExtend;
WordApp.Selection.MoveDown(
ref moveUnit, ref moveCount, ref moveExtend);
WordApp.Selection.Cells.Merge();
// 插入图片
string FileName = Picture; // 图片所在路径
object LinkToFile = false ;
object SaveWithDocument = true ;
object Anchor = WordDoc.Application.Selection.Range;
WordDoc.Application.ActiveDocument.InlineShapes.AddPicture(FileName,
ref LinkToFile, ref SaveWithDocument, ref Anchor);
WordDoc.Application.ActiveDocument.InlineShapes[
1 ].Width = 100f; // 图片宽度
WordDoc.Application.ActiveDocument.InlineShapes[ 1 ].Height = 100f; // 图片高度
// 将图片设置为四周环绕型
Word.Shapes = WordDoc.Application.ActiveDocument.InlineShapes[ 1 ].ConvertToShape();
s.WrapFormat.Type
= Word.WdWrapType.wdWrapSquare;

newTable.Cell(
12 , 1 ).Range.Text = " 产品特殊属性 " ;
newTable.Cell(
12 , 1 ).Merge(newTable.Cell( 12 , 3 ));
// 在表格中增加行
WordDoc.Content.Tables[ 1 ].Rows.Add( ref Nothing);

WordDoc.Paragraphs.Last.Range.Text
= " 文档创建时间: " + DateTime.Now.ToString(); // “落款”
WordDoc.Paragraphs.Last.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight;

// 文件保存
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 + " 文档生成成功,以保存到C:CNSI下 " ;
}

catch
... {
message
= " 文件导出异常! " ;
}

return message;
}

分享到:
评论

相关推荐

    用C#动态生成Word文档并将数据填入Word表格中

    用C#动态生成Word文档并将数据填入Word表格中

    Asp.net动态生成Word文档并填充数据

    Asp.net动态生成Word文档并填充数据

    C#编程实现动态生成Word文档

    如何用C#编程实现动态生成Word文档并填充数据的效果呢?要使用C#操作word,首先要添加引用: 1、添加引用->COM->Microsoft Word 11.0 Object Library 2、在.cs文件中添加 using Word; 下面的例子中包括C#对Word...

    利用C#语言动态生成Word文档

    该资源属自己整理,利用c#语言动态生成word文档并填充数据

    C#向word文档动态插入数据并打印预览

    Microsoft.Office.Interop.Word.Application myWordApp = null; Microsoft.Office.Interop.Word.Document doc = null; object Filename = path + "\\" + TaskID + ".docx";//目的文件 object templateFile = ...

    ASP.NET(c#) 从零动态生成Word文件(用程序设置文本段落格式、填充数据)

    使用的是PageOffice,除了提供Word/Excel动态数据填充,格式控制,Word/Excel用户输入提交,Word/Excel/PowerPoint/WPS等Office文档的在线打开、只读浏览、编辑、保存等功能外,还给在线协同办公内置了强大的支持...

    C#动态生成Word

    此程序更加XML(wordtest.xml)配置文件动态生成WORD文档,支持固定表格、列表表格、根据DataSet中Table个数生成多个同一表格,表格可以合并行或列,字体和颜色都可以设置,页眉页脚也可以设置。

    WordTemplate_npoi_c#+excel模板_word生成_word_excel_

    由于项目需求,需要根据现有的word模板,进行批量生成word报表,为了方便,选用的是 NPOI (NPOI是一个开源的C#读写Excel、WORD等微软OLE2组件文档的项目)来实现。本次用的版本是NPOI 2.3.0.0,版本不同写法会有...

    ASP.NET(c#) 动态生成Excel表格

    用C#编程实现绘制Excel表格并填充数据和公式,生成表格或报表。 不是服务器端自动化。 使用的是PageOffice,除了提供Word/Excel动态数据填充,格式控制,Word/Excel用户输入提交,Word/Excel/PowerPoint/WPS等...

    openxml.wordprocessing-word

    动态生成Word文档并填充数据, 读取Word生成的一些信息

    C#里操作Word文档

    该实例程序下载后即可运行(VS2008、SQL2005),可以看到,我们在Word文档里定义了一个报表模版,然后程序自动从数据库取出数据并在指定的位置填充,包括表格的动态生成并填充,和标识字段的超找替换,最后生成一张...

    c# 写入Word各种特殊符号: 复选框 手指 剪刀等等

    教你怎样往Word里写入各种特殊符号,附带源码,绝版珍藏。留着以后肯定能用到。导出文件中加入特殊符号,所需积分5积分不知道怎么回变成那么多。

    读取excel输出word文档使用npoi.rar

     用模板新建word文档  每一行excel数据,用word模板生成一个报告文档。  向报告中插入图片。图片存放在固定文件夹下,采用jpg格式,按建筑编号命名即可。  向表格中指定的单元格填充数据,数据从excel表中...

    shp2word 完成shp到word文档信息的转化

    shp word 转化 生成 arcengine c# 从shp里面读取数据填充word文档,这样的数据间的转化操作很常见

    C#实现通过模板自动创建Word文档的方法

    引言:前段时间有项目要用c#生成Word格式的计算报告,通过网络查找到很多内容,但是都很凌乱,于是自己决定将具体的步骤总结整理出来,以便于更好的交流和以后相似问题可以迅速的解决! 现通过具体的示例演示具体的...

    C#开发实例大全(基础卷).软件开发技术联盟(带详细书签) PDF 下载

    主要内容有C#开发环境的使用、C#语言基础应用、字符串处理技术、数组和...数据控件、自定义用户控件、文件基本操作、文件夹基本操作、文件流操作、加密、解密及解压缩文件、C#与Word互操作、高效应用Excel、基本图形...

    Aspose_Word模板使用总结.doc

    Aspose_Word模板使用总结,各种生成word的情况,亲测可用

Global site tag (gtag.js) - Google Analytics