用C#操作Wordredis 面试常用的问题题

Visual C#操作Word2007
我的图书馆
Visual C#操作Word2007
这几天,有个小任务,要把一些数据用C#输出到word里,查了查网上的资料,都是说怎么处理word2003的,而我的器上装的是office 2007,所以有些内容有些不太一样,这里跟大家分享一下我的操作过程。
首先是添加引用,从com控件中找到Microsoft Word 12.0 Object Library,或者直接选择“浏览”,找到X:\Program Files\Microsoft Office\Office12\MSWORD.OLB,添加引用就可以了,程序或自动增加Microsoft.Office.Interop.Word、Microsoft.Office.Core、VBIDE三个引用,注意,如果是Word2003,Microsoft.Office.Interop.Word 这个引用的名字是 Word,这是与2003不同的地方。
然后在程序中用using语句导入word。(如果是word2003,采用using W就可以了)
using Microsoft.Office.Interop.W另外再增加一个引用,具体的作用参加代码中的注释
using System.Runtime.InteropS下边是对word的简单操作
&&&&&&&&&&& bool createByMe;&&&&&&&&&&& Microsoft.Office.Interop.Word.Application oW&&&&&&&&&&& try&&&&&&&&&&& {&&&&&&&&&&&&&&& //如果有正在运行的word实例,则直接采用当前的word实例,&&&&&&& //否则,直接创建新实例,在最后退出时会报模板正在使用中等问题&&&&&&& oWord = (Microsoft.Office.Interop.Word.Application)Marshal.GetActiveObject("Word.Application");&&&&&&&&&&&&&&& createByMe =&&&&&&&&&&& }&&&&&&&&&&& catch&&&&&&&&&&& {&&&&&&&&&&&&&&& oWord = new Microsoft.Office.Interop.Word.Application();&&&&&&&&&&&&&&& createByMe =&&&&&&&&&&& }&&&&&&&&&&& Object Nothing = System.Reflection.Missing.V&&&&&&&&&&& Object template = System.Windows.Forms.Application.StartupPath + "\Caliber.dot";&&&&&&&&&&& Document myDoc = oWord.Documents.Add(ref template, ref Nothing, ref Nothing, ref Nothing);
&&&&&&&&&& //do something&&&&&&&&&& myDoc.Paragraphs.Last.Range.Text = "test"; &&&&&&&&&& &&&&&&&&&&& myDoc.SaveAs(ref strFileName, 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);
&&&&&&&&&&& myDoc.Close(ref Nothing, ref Nothing, ref Nothing);&&&&&&&&&&& if (createByMe)&&&&&&&&&&& {&&&&&&&&&&&&&&& oWord.Quit(ref Nothing, ref Nothing, ref Nothing);&&&&&&&&&&& }
本文来自CSDN博客,转载请标明出处:
TA的最新馆藏[转]&[转]&[转]&[转]&[转]&[转]&
喜欢该文的人也喜欢本帖子已过去太久远了,不再提供回复功能。Word.Paragraph oPara1 = wordDoc.Content.Paragraphs.Add(ref Nothing);
oPara1.Range.Font.Name = &Verdana&;
//设置字体
oPara1.Range.Font.Size = 20;
//设置字体大小
oPara1.Range.Text=&我需要居中&;
//插入文本
oPara1.Range.ParagraphFormat.Alignment =Word.WdParagraphAlignment.wdAlignParagraphC //我的意图是让这段文字居中,但这样做是不行的
oPara1.Range.InsertParagraphAfter();//插入回车
Word.Paragraph oPara2 = wordDoc.Content.Paragraphs.Add(ref Nothing);
oPara2.Range.Font.Name = &Verdana&;
//设置字体
oPara2.Range.Font.Size = 20;
//设置字体大小
oPara2.Range.Text=&我需要右对齐&;
//插入文本
oPara.Range.ParagraphFormat.Alignment =Word.WdParagraphAlignment.wdAlignParagraphR //我的意图是让这段文字右对齐,但这样做是不行的
oPara2.Range.InsertParagraphAfter();//插入回车
哪位高手知道,如何用c#操作Word,第一行,也就是标题居中,第二行开始,正文右对齐呢?问得更普通一点,如何用C#操作Word,每行都有各自的对齐方式,全文对齐我会,但分到各行不同的对齐方式,我就没办法了。求懂的高手指点。
昵称: xuefey &时间:
昵称: colinfang2006 &时间:
昵称: xuefey &时间:
昵称: rhjyy &时间:拒绝访问 | www. | 百度云加速
请打开cookies.
此网站 (www.) 的管理员禁止了您的访问。原因是您的访问包含了非浏览器特征(3ce61fef9380439a-ua98).
重新安装浏览器,或使用别的浏览器

我要回帖

更多关于 追及问题应用题及答案 的文章

 

随机推荐