private void button1_Click(object sender, System.EventArgs e)
{
object oMissing = System.Reflection.Missing.Value;
object oEndOfDoc = "\\endofdoc"; /* \endofdoc是预定义的bookmark */ //创建一个document.
Word._Application oWord;
Word._Document oDoc;
oWord = new Word.Application();
oWord.Visible = true;
oDoc = oWord.Documents.Add(ref oMissing, ref oMissing,
ref oMissing, ref oMissing); //在document的开始部分添加一个paragraph.
Word.Paragraph oPara1;
oPara1 = oDoc.Content.Paragraphs.Add(ref oMissing);
oPara1.Range.Text = "Heading 1";
oPara1.Range.Font.Bold = ;
oPara1.Format.SpaceAfter = ; //24 pt spacing after paragraph.
oPara1.Range.InsertParagraphAfter(); //在当前document的最后添加一个paragraph Word.Paragraph oPara2;
object oRng = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;
oPara2 = oDoc.Content.Paragraphs.Add(ref oRng);
oPara2.Range.Text = "Heading 2";
oPara2.Format.SpaceAfter = ;
oPara2.Range.InsertParagraphAfter(); //接着添加一个paragraph
Word.Paragraph oPara3;
oRng = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;
oPara3 = oDoc.Content.Paragraphs.Add(ref oRng);
oPara3.Range.Text = "This is a sentence of normal text. Now here is a table:";
oPara3.Range.Font.Bold = ;
oPara3.Format.SpaceAfter = ;
oPara3.Range.InsertParagraphAfter(); //添加一个3行5列的表格,填充数据,并且设定第一行的样式 Word.Table oTable;
Word.Range wrdRng = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;
oTable = oDoc.Tables.Add(wrdRng, , , ref oMissing, ref oMissing);
oTable.Range.ParagraphFormat.SpaceAfter = ;
int r, c;
string strText;
for(r = ; r <= ; r++)
for(c = ; c <= ; c++)
{
strText = "r" + r + "c" + c;
oTable.Cell(r, c).Range.Text = strText;
}
oTable.Rows[].Range.Font.Bold = ;
oTable.Rows[].Range.Font.Italic = ; //接着添加一些文字 Word.Paragraph oPara4;
oRng = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;
oPara4 = oDoc.Content.Paragraphs.Add(ref oRng);
oPara4.Range.InsertParagraphBefore();
oPara4.Range.Text = "And here's another table:";
oPara4.Format.SpaceAfter = ;
oPara4.Range.InsertParagraphAfter(); //添加一个5行2列的表,填充数据并且改变列宽
wrdRng = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;
oTable = oDoc.Tables.Add(wrdRng, , , ref oMissing, ref oMissing);
oTable.Range.ParagraphFormat.SpaceAfter = ;
for(r = ; r <= ; r++)
for(c = ; c <= ; c++)
{
strText = "r" + r + "c" + c;
oTable.Cell(r, c).Range.Text = strText;
}
oTable.Columns[].Width = oWord.InchesToPoints(); //Change width of columns 1 & 2
oTable.Columns[].Width = oWord.InchesToPoints(); //Keep inserting text. When you get to 7 inches from top of the
//document, insert a hard page break.
object oPos;
double dPos = oWord.InchesToPoints();
oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range.InsertParagraphAfter();
do
{
wrdRng = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;
wrdRng.ParagraphFormat.SpaceAfter = ;
wrdRng.InsertAfter("A line of text");
wrdRng.InsertParagraphAfter();
oPos = wrdRng.get_Information
(Word.WdInformation.wdVerticalPositionRelativeToPage);
}
while(dPos >= Convert.ToDouble(oPos));
object oCollapseEnd = Word.WdCollapseDirection.wdCollapseEnd;
object oPageBreak = Word.WdBreakType.wdPageBreak;
wrdRng.Collapse(ref oCollapseEnd);
wrdRng.InsertBreak(ref oPageBreak);
wrdRng.Collapse(ref oCollapseEnd);
wrdRng.InsertAfter("We're now on page 2. Here's my chart:");
wrdRng.InsertParagraphAfter(); //添加一个chart Word.InlineShape oShape;
object oClassType = "MSGraph.Chart.8";
wrdRng = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;
oShape = wrdRng.InlineShapes.AddOLEObject(ref oClassType, ref oMissing,
ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing); //Demonstrate use of late bound oChart and oChartApp objects to
//manipulate the chart object with MSGraph.
object oChart;
object oChartApp;
oChart = oShape.OLEFormat.Object;
oChartApp = oChart.GetType().InvokeMember("Application",
BindingFlags.GetProperty, null, oChart, null); //Change the chart type to Line.
object[] Parameters = new Object[];
Parameters[] = ; //xlLine = 4
oChart.GetType().InvokeMember("ChartType", BindingFlags.SetProperty,
null, oChart, Parameters); //Update the chart image and quit MSGraph.
oChartApp.GetType().InvokeMember("Update",
BindingFlags.InvokeMethod, null, oChartApp, null);
oChartApp.GetType().InvokeMember("Quit",
BindingFlags.InvokeMethod, null, oChartApp, null);
//... If desired, you can proceed from here using the Microsoft Graph
//Object model on the oChart and oChartApp objects to make additional
//changes to the chart. //Set the width of the chart.
oShape.Width = oWord.InchesToPoints(6.25f);
oShape.Height = oWord.InchesToPoints(3.57f); //Add text after the chart.
wrdRng = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;
wrdRng.InsertParagraphAfter();
wrdRng.InsertAfter("THE END."); //Close this form.
this.Close();
} 使用模板 如果您要使用自动化功能创建的文档都是通用格式,则利用基于预设格式的模板的新文档来开始创建过程会更加容易。与从头创建文档相比,将某个模板与 Word 自动化客户端配合使用有两大优点:
• 您可以对整个文档中的对象的格式设置和布局施加更多控制。
• 可以使用较少的代码创建文档。
通过使用模板,可以精确地调整表格、段落和其他对象在文档中的布局,并可为这些对象添加格式设置。通过使用自动化功能,可以基于包含下面这样的代码的模板创建新文档: 在模板中,可以定义书签,这样,自动化客户端就可以在文档的特定位置加入可变文本,如下所示: 使用模板的另一个优点在于,您可以创建和存储希望在运行时应用的格式样式,如下所示: - 或者 - object oTemplate = "c:\\MyTemplate.dot";
oDoc = oWord.Documents.Add(ref oTemplate, ref oMissing,
ref oMissing, ref oMissing); object oBookMark = "MyBookmark";
oDoc.Bookmarks.Item(ref oBookMark).Range.Text = "Some Text Here"; object oStyleName = "MyStyle";
oDoc.Bookmarks.Item(ref oBookMark).Range.set_Style(ref oStyleName); object oStyleName = "MyStyle";
oWord.Selection.set_Style(ref oStyleName); 最主要的就是理解word application 的框架层次,其它的就像面向过程编程一样,一步步写代码,其中比较麻烦的是嵌套的表格...

使用C# 生成word记录的更多相关文章

  1. C# 利用WORD模板和标签(bookmark) 批量生成WORD

    前言: 由于对C#操作WORD不熟悉,也就留下这么一篇水文,别吐糟...=_=||| 利用Microsoft.Office.Interop.Word (2003版也就11版)——因为部分客户端还是用O ...

  2. FreemarkerJavaDemo【Android将表单数据生成Word文档的方案之一(基于freemarker2.3.28,只能java生成)】

    版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 这个方案只能在java中运行,无法在Android项目中运行.所以此方案是:APP将表单数据发送给后台,后台通过freemarker ...

  3. springboot中使用freemarker生成word文档并打包成zip下载(简历)

    一.设计出的简历模板图以及给的简历小图标切图         二.按照简历模板图新建简历word文件 :${字段名},同时将图片插入到word中,并将建好的word文件另存为xml文件:    三.直 ...

  4. C# 动态生成word文档 [C#学习笔记3]关于Main(string[ ] args)中args命令行参数 实现DataTables搜索框查询结果高亮显示 二维码神器QRCoder Asp.net MVC 中 CodeFirst 开发模式实例

    C# 动态生成word文档 本文以一个简单的小例子,简述利用C#语言开发word表格相关的知识,仅供学习分享使用,如有不足之处,还请指正. 在工程中引用word的动态库 在项目中,点击项目名称右键-- ...

  5. php 备份数据库代码(生成word,excel,json,xml,sql)

    单表备份代码: 复制代码代码如下: <?php class Db { var $conn; function Db($host="localhost",$user=" ...

  6. Java Web项目中使用Freemarker生成Word文档遇到的问题

    这段时间项目中使用了freemarker生成word文档.在项目中遇到了几个问题,在这里记录一下.首先就是关于遍历遇到的坑.整行整行的遍历是很简单的,只需要在整行的<w:tr></w ...

  7. python生成word中文字体

    python生成word中文字体 我们今天用python生成word文件,主要是用到了PyRTF包生成rtf文件,由于PyRTF的包中文教程比较少,所以特此记录几篇文章,也希望给大家有一些帮助. 开始 ...

  8. Aspose.Words简单生成word文档

    Aspose.Words简单生成word文档 Aspose.Words.Document doc = new Aspose.Words.Document(); Aspose.Words.Documen ...

  9. php 生成word的三种方式

    原文地址 http://www.jb51.net/article/97253.htm 最近工作遇到关于生成word的问题 现在总结一下生成word的三种方法. btw:好像只要是标题带PHP的貌似点击 ...

随机推荐

  1. 【转】定时器、sigevent结构体详解

    原文网址:http://blog.163.com/zheng_he_xiang/blog/static/18650532620116311020390/ 最强大的定时器接口来自POSIX时钟系列,其创 ...

  2. MySQL基础之第10章 查询数据

    10.1.基本查询语句 SELECT 属性列表 FROM 表名和视图列表[WHERE条件表达式1][GROUPBY 属性名1 [HAVING条件表达式2]][ORDERBY 属性名2[ASC|DESC ...

  3. HDU 4035Maze(树状+概率dp,绝对经典)

    题意: 给你n个节点的树,从1节点开始走,到每个节点都有三种情况,被杀死回到1节点,找到隐藏的出口出去,沿着当前节点相邻的边走到下一个节点,给出每个节点三种情况发生的概率分别为ki,ei,1-ki-e ...

  4. windows各种程序中文显示乱码又找不到原因时

    我电脑上的各种程序,如xshell,Navicat for MySQL都不正常显示中文,该软件的编码,utf-8,gbk,gb2312来回切换好几回,没一次正常,最好解决办法如下       进入控制 ...

  5. EF6 Database First (DbContext) - Change Schema at runtime

    Problem:There are two SQL databases (dev and live) with on Azure which has identical table structure ...

  6. 【转载】【内存对齐(二)】__declspec( align(#) )的用法和大小计算

    转自:http://www.cppblog.com/deercoder/archive/2011/03/13/141747.html 感谢作者! 在上面讲到了关于pack的内存对齐和计算方法,这里继续 ...

  7. 我喜欢的乐队-Euphoria

    来自日本的后摇乐团,001年冬天由森川裕之.佐藤昭太.木下阳辅三人于东京组建,2003年签约日本独立厂牌123Record,并发行首张EP细碟<Floral Dew>.包括EP.Singl ...

  8. ORA-12162: TNS:net service name is incorrectly specified

    今天在进行修改oracle_sid环境变量的时候,将相关的环境变量值去掉,从而不能进入sqlplus,报错如下: [oracle@kel ~]$ sqlplus / as sysdba SQL*Plu ...

  9. 《Genesis-3D开源游戏引擎完整实例教程-跑酷游戏篇:简介及目录》(附上完整工程文件)

    跑酷游戏制作 游戏类型: 此游戏Demo,为跑酷类游戏. 框架简介: 游戏通常由程序代码和资源组成.如果说模型.贴图.声音之类的可以给游戏环境提供一个物理描述和设置,那么脚本和代码块会给游戏赋予生命, ...

  10. Spring学习笔记(一) Spring基础IOC、AOP

    1.       注入类型 a)       Spring_0300_IOC_Injection_Type b)       setter(重要) c)       构造方法(可以忘记) d)     ...