C#导出Word各种参数

上传人:无*** 文档编号:162596002 上传时间:2022-10-18 格式:DOC 页数:24 大小:95KB
收藏 版权申诉 举报 下载
C#导出Word各种参数_第1页
第1页 / 共24页
C#导出Word各种参数_第2页
第2页 / 共24页
C#导出Word各种参数_第3页
第3页 / 共24页
资源描述:

《C#导出Word各种参数》由会员分享,可在线阅读,更多相关《C#导出Word各种参数(24页珍藏版)》请在装配图网上搜索。

1、实例: WebToWord toWord = new WebToWord(); StringBuilder sb = new StringBuilder(); sb.AppendLine(Word文档); sb.AppendLine( . 待办任务 任务数量); sb.AppendLine( 1 设备采购申请 6 deviced_apply ); sb.AppendLine( 2 生产厂商信息登记 14 provider ); sb.AppendLine( 3 设备质量索赔记录 142 ); toWord.CreateDocFile(C:+DateTime.Now.ToFileTime().T

2、oString()+.DOC, sb.ToString(); using MsWord = Microsoft.Office.Interop.Word;using System.Reflection; / / 生成doc文件函数 / / 文件的生成路径,包括文件名;例如C:11.doc,注意参数为object类 /型,只接受绝对路径 / 要保存在word文档中的内容 public void CreateDocFile(object fileName, string conCon) MsWord.Application wordApp; /word应用程序变量 MsWord.Document w

3、ordDoc; /word文档变量 wordApp = new MsWord.ApplicationClass(); /初始化 Object Nothing = Missing.Value; wordDoc = wordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing); /向文档中写入数据 wordDoc.Paragraphs.Last.Range.Text = conCon; /将wordDoc文档对象的内容保存为DOC文档 object format = MsWord.WdSaveFormat.wdF

4、ormatDocument; /将wordDoc文档对象的内同保存为DOC文档 wordDoc.SaveAs(ref fileName, ref format, 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文档对象 wordDoc.Close(ref Not

5、hing, ref Nothing, ref Nothing); /会产生多义性警告,暂时未解决 /关闭wordApp组件对象 wordApp.Quit(ref Nothing, ref Nothing, ref Nothing);/会产生多义性警告,暂时未解决 / MessageBox.Show(文档创建成功); /本文来自CSDN博客,转载请标明出处: 我们都知道,Office是基于COM组件技术开发的,属于非托管程序,然而C#使用的都是托管程序,那么如何使用非托管的COM组件就是我们操作WORD的第一个问题。所幸的是,.NET FRAMEWORK提供了一种不同种类类库的转换工具tlbim

6、p,exe,通过这个转换工具,我们可以把COM组件转化为.NET FRAMEWORK可以直接调用的DLL文件。接下来就是转化工作了,Office组件都可以在C:Program FilesMicrosoft Office目录下找到,当然安装的Office版本不同,子目录是不一样的。笔者使用的是Office 2007,可以在C:Program FilesMicrosoft OfficeOffice12目录下找到MSWORD.OLB,这个是WORD组件的类库文件,还可以找到MSACC.OLB操作ACCESS,MSPPT.OLB操作PPT,XL5CHS32.OLB操作EXCEL。载入不同的组件就可以完

7、成对不同Office组件的操作。使用tlbimp,exe工具转化MSWORD.OLB文件后可以得到三个DLL文件,Office,dll,Visual BasicIDE.dll,Word.dll。最后在编译文件的时候,记得将这三个DLL文件载入,命令如下:csc /r:system.dll /r:system.windows.forms.dll /r:system.drawing.dll /r:office.dll /r:vbide.dll /r:word.dll word.cs笔者使用Visual Studio.NET 2005编译环境,通过IDE提供的功能可以大大简化我们对组件转化的工作,并

8、且在编译时也不需要输入那么繁杂的语句,非常方便了。下面介绍一下IDE载入Office组件的方式。在菜单栏选择“项目”“添加引用”,弹出的窗口中我们可以选择“COM”选项卡,找到Microsoft Office 12.0 Object Library(Office 2003/2007需要使用12.0版的,如果你使用的是Office 2000或者更低的版本,只要载入10.0版的就可以了),确定后引入.也可以在“浏览”选项卡下找到我们上面提到的MSWORD.OLB文件,引入即可。引入后我们可以发现在解决方案中,引用目录下多了三个文件Microsoft.Office.Core,Microsoft.Of

9、fice.Interop.Word,VSIDE。这说明引用文件成功,之后在编译程序的时候,在Debug目录下会生成两个DLL文件,Interop.Microsoft.Office.Core.dll,Interop.Microsoft.Office.Interop.Word.dll。完成组件的引入,下面就可以开始程序设计了。先看一下Word对像模型Application :用来表现WORD应用程序,包含其它所有对象。他的成员经常应用于整个WORD,你可以用它的属性和方法控制WORD环境。Document :Document对象是WORD编程的核心。当你打开一个已有的文档或创建一个新的文档时,就创

10、建了一个新的Document对象, 新创建的Document将会被添加到Word Documents Collection。Selection :Selection对象是描述当前选中的区域。若选择区域为空,则认为是当前光标处。Rang :是Document的连续部分,根据起始字符和结束字符定义位置。Bookmark:类似于Rang,但Bookmark可以有名字并在保存Document时Bookmark也被保存。 在编程中使用到的代码如下,注释比较详细,这里就不再具体的说明。 /Word程序对象 private Microsoft.Office.Interop.Word.Application

11、WordApp = new Microsoft.Office.Interop.Word.Application(); /Word文档对象 private Microsoft.Office.Interop.Word._Document aDoc; private void openfile_Click(object sender, EventArgs e) /打开Word文件 if (openFileDialog.ShowDialog() = DialogResult.OK) /定义打开文件的16个参数 object fileName = openFileDialog.FileName; /文件

12、名称 object ConfirmConversions = false; /允许转换 object ReadOnly = false; /只读方式打开 object AddToRecentFiles = false; /添加到最近打开的文档 object PasswordDocument = System.Type.Missing; object PasswordTemplate = System.Type.Missing; object Revert = System.Type.Missing; object WritePasswordDocument = System.Type.Miss

13、ing; object WritePasswordTemplate = System.Type.Missing; object Format = System.Type.Missing; /格式 object Encoding = System.Type.Missing; /编码 object Visible = System.Type.Missing; object OpenAndRepair = System.Type.Missing; object DocumentDirection = System.Type.Missing; object NoEncodingDialog = Sys

14、tem.Type.Missing; object XMLTransform = System.Type.Missing; WordApp.Visible = true; try /打开文档 aDoc = WordApp.Documents.Open(ref fileName, ref ConfirmConversions, ref ReadOnly, ref AddToRecentFiles, ref PasswordDocument, ref PasswordTemplate, ref Revert, ref WritePasswordDocument, ref WritePasswordT

15、emplate, ref Format, ref Encoding, ref Visible, ref OpenAndRepair, ref DocumentDirection, ref NoEncodingDialog, ref XMLTransform); /激活文档,使文档为当前处理 aDoc.Activate(); catch MessageBox.Show(出现错误!); private void closefile_Click(object sender, EventArgs e) /关闭Word文件 object SaveChanges = false; /保存更改 object

16、 OriginalFormat = System.Type.Missing; object RouteDocument = System.Type.Missing; /关闭文档 aDoc.Close(ref SaveChanges, ref OriginalFormat, ref RouteDocument); /退出程序 WordApp.Quit(ref SaveChanges, ref OriginalFormat, ref RouteDocument); 通过文档类对象aDoc还可以完成文件的保存,另存为等等操作,详细的可以参阅MSDN。用C#动态生成Word文档并将数据填入Word表格

17、中电脑知识 2009-07-09 16:37:03 阅读91 评论0 字号:大中小 刚刚实现了个功能:用C#实现动态生成Word文档,在Word文档中插入表格,并将读出的数据填入到表格中。 要使用C#操作word,首先要添加引用: 1、添加引用-COM-Microsoft Word 11.0 Object Library 2、在.cs文件中添加using Word;下面的例子中包括C#对Word文档的创建、插入表格、设置样式等操作:(例子中代码有些涉及数据信息部分被省略,重要是介绍一些C#操作word文档的方法) public string CreateWordFile(string Chec

18、kedInfo) . string message = ; try . Object Nothing = System.Reflection.Missing.Value; Directory.CreateDirectory(C:/CNSI); /创建文件所在目录 string + DateTime.Now.ToShortString()+.doc; object filename = C:/CNSI/ + name; /文件保存路径 /创建Word文档 Word.Application WordApp = new Word.ApplicationClass(); Word.Document W

19、ordDoc = 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.Par

20、agraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight;/设置右对齐 WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekMainDocument;/跳出页眉设置 WordApp.Selection.ParagraphFormat.LineSpacing = 15f;/设置文档的行间距 /移动焦点并换行 object count = 14; object WdLine = Word.WdUnits.wdLine;/换一行; WordApp.Selec

21、tion.MoveDown(ref WdLine, ref count, ref Nothing);/移动焦点 WordApp.Selection.TypeParagraph();/插入段落 /文档中创建表格 Word.Table newTable = WordDoc.Tables.Add(WordApp.Selection.Range, 12, 3, ref Nothing, ref Nothing); /设置表格样式 newTable.Borders.OutsideLineStyle = Word.WdLineStyle.wdLineStyleThickThinLargeGap; newT

22、able.Borders.InsideLineStyle = Word.WdLineStyle.wdLineStyleSingle; newTable.Columns1.Width = 100f; newTable.Columns2.Width = 220f; newTable.Columns3.Width = 105f; /填充表格内容 newTable.Cell(1, 1).Range.Text = 产品详细信息表; newTable.Cell(1, 1).Range.Bold = 2;/设置单元格中字体为粗体 /合并单元格 newTable.Cell(1, 1).Merge(newTab

23、le.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

24、.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; /纵向合

25、并单元格 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;/图片所在路径 obj

26、ect 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.InlineShapes1.Width = 100f;/图片宽度 Wor

27、dDoc.Application.ActiveDocument.InlineShapes1.Height = 100f;/图片高度 /将图片设置为四周环绕型 Word.Shape s = WordDoc.Application.ActiveDocument.InlineShapes1.ConvertToShape(); s.WrapFormat.Type = Word.WdWrapType.wdWrapSquare; newTable.Cell(12, 1).Range.Text = 产品特殊属性; newTable.Cell(12, 1).Merge(newTable.Cell(12, 3)

28、; /在表格中增加行 WordDoc.Content.Tables1.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 No

29、thing, 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 . m

30、essage = 文件导出异常!; return message; 使用C#读取Word表格数据2009年12月29日 星期二 下午 07:21读取Word表格数据的方法1/将读取Word表格封装与方法中。2public string ReadWord(string fileName, int rowIndex, int colIndex)34ApplicationClass cls = null;5Document doc = null;67Table table = null;8object missing = Missing.Value;910object path = fileName

31、;11cls = new ApplicationClass();1213try1415doc = cls.Documents.Open16(ref path, ref missing, ref missing, ref missing,17ref missing, ref missing, ref missing, ref missing,18ref missing, ref missing, ref missing, ref missing,19ref missing, ref missing, ref missing, ref missing);20table = doc.Tables1;

32、21string text = table.Cell(rowIndex, colIndex).Range.Text.ToString();22text = text.Substring(0, text.Length - 2);/去除尾部的mark23return text;2425catch (Exception ex)262728return ex.Message;2930finally3132if (doc != null)33doc.Close(ref missing, ref missing, ref missing);34cls.Quit(ref missing, ref missi

33、ng, ref missing);3536这个方法用于读取Word表格中某个单元格的数据。其中的参数分别为文件名(包括路径),行号,列号。=由于考虑到代码复用,我将代码写成了一个类。此外,通过审视代码可以发现,如果要多次读取同一文件中的不同的单元格数据会造成频繁的打开、关闭Word程序;因此,我将代码进行优化。在我做优化的时候突然想起来ADO.NET的SqlConnection和SqlCommand类。这两个类我常常用做数据库操作,一般用到的方法顺序都是:打开数据库连接,执行数据库查询,关闭数据库连接。我没有使用到两个类,我将这段代码封装于一个类中。使用Open、Close控制Word文档的打

34、开和关闭,使用WordTableRead方法读取表格中的数据。这样对于读取多个单元格中的数据,每次只需要打开、关闭一次Word程序即可,大大的节约了资源的开销和节省了时间,提高的读取效率。此外,对于代码的优化还有可以读取指定表格中数据。下图显示了这个类的结构,代码及相应注释附在图的下方:class WordTableRead23private string fileName;4private ApplicationClass cls = null;5private Document doc = null;6private Table table = null;7private object m

35、issing = Missing.Value;8/Word是否处于打开状态9private bool openState;101112/*/ 13/ 自定义构造方法14/ 15/ 包含路径的文件名16public WordTableRead(string fileName)1718this.fileName = fileName;192021/*/ 22/ 打开Word文档23/ 24public void Open()2526object path = fileName;27cls = new ApplicationClass();28try2930doc = cls.Documents.O

36、pen31(ref path, ref missing, ref missing, ref missing,32ref missing, ref missing, ref missing, ref missing,33ref missing, ref missing, ref missing, ref missing,34ref missing, ref missing, ref missing, ref missing);35openState = true;3637catch3839openState = false;40414243/*/ 44/ 返回指定单元格中的数据45/ 46/ 表

37、格号47/ 行号48/ 列号49/ 单元格中的数据50public string ReadWord(int tableIndex, int rowIndex, int colIndex)5152/Give the value to the tow Int32 params.5354try5556if (openState = true)5758table = doc.TablestableIndex;59string text = table.Cell(rowIndex, colIndex).Range.Text.ToString();60text = text.Substring(0, te

38、xt.Length - 2);/去除尾部的mark61return text;6263else6465return ;666768catch6970return Error;71727374/*/ 75/ 关闭Word文档76/ 77public void Close()7879if (openState = true)8081if (doc != null)82doc.Close(ref missing, ref missing, ref missing);83cls.Quit(ref missing, ref missing, ref missing);848586 using Word;

39、运行程序通过,搞定.【摘自】 周老师科研站 oMissing); object start = 0; object end = 0; Word.Range tableLocation = oDoc.Range(ref start, ref end); oDoc.Tables.Add(tableLocation, 3, 4, ref oMissing, ref oMissing); Word.Table newTable = oDoc.Tables1; object beforeRow = newTable.Rows1; newTable.Rows.Add(ref beforeRow); Wor

40、d.Cell cell = newTable.Cell(1, 1); cell.Merge(newTable.Cell(1, 2); object Rownum = 2; object Columnnum = 2; cell.Split(ref Rownum, ref Columnnum);前提:导入COM库:Microsoft word 11.0 Object Library.引用里面就增加了:创建新Word object oMissing = System.Reflection.Missing.Value; Word._Application oWord; Word._Document o

41、Doc; oWord = new Word.Application(); oWord.Visible = true; oDoc = oWord.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing);打开文档: object oMissing = System.Reflection.Missing.Value; Word._Application oWord; Word._Document oDoc; oWord = new Word.Application(); oWord.Visible = true; o

42、bject fileName = E:CCCXCXXTestDoc.doc; oDoc = oWord.Documents.Open(ref fileName, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);导入模板 obje

43、ct oMissing = System.Reflection.Missing.Value; Word._Application oWord; Word._Document oDoc; oWord = new Word.Application(); oWord.Visible = true; object fileName = E:XXXCCXTest.doc; oDoc = oWord.Documents.Add(ref fileName, ref oMissing, ref oMissing, ref oMissing);.添加新表 object oMissing = System.Ref

44、lection.Missing.Value; 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); object start = 0; object end = 0; Word.Range tableLocation = oDoc.Range(ref start, ref end);

45、oDoc.Tables.Add(tableLocation, 3, 4, ref oMissing, ref oMissing);.表插入行 object oMissing = System.Reflection.Missing.Value; Word._Application oWord; Word._Document oDoc; oWord = new Word.Application(); oWord.Visible = true; oDoc = oWord.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMiss

46、ing); object start = 0; object end = 0; Word.Range tableLocation = oDoc.Range(ref start, ref end); oDoc.Tables.Add(tableLocation, 3, 4, ref oMissing, ref oMissing); Word.Table newTable = oDoc.Tables1; object beforeRow = newTable.Rows1; newTable.Rows.Add(ref beforeRow);.单元格合并 object oMissing = System

47、.Reflection.Missing.Value; 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); object start = 0; object end = 0; Word.Range tableLocation = oDoc.Range(ref start, ref end); oDoc.Tables.Add(tableLocation, 3, 4, ref oMissing, ref oMissing); Word.Table newTable = oDoc.Tables1; object beforeRow = newTable.Rows1; newTable.Rows.Add(ref beforeRow); Word.Cell cell = newTable.Cell(1, 1); cell.Merge(newTable.Cell(1, 2);.单元格分离 object oMissing = Syste

展开阅读全文
温馨提示:
1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
2: 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
3.本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
关于我们 - 网站声明 - 网站地图 - 资源地图 - 友情链接 - 网站客服 - 联系我们

copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!