
word
以下是改写后的内容:public class BuildReportForm {private
wordApplication _
wordApplication;private Document _
wordDocument;public BuildReportForm() {//实例化
word应用对象_
wordApplication = new
word.Application();//创建一个空白文档_
wordDocument = _
wordApplication.Documents.Add(ref Microsoft.Office.Interop.
word.WdContentControlType.wdContentControlDocument, ref Microsoft.Office.Interop.
word.WdSaveMode.wdSaveChanges, ref Microsoft.Office.Interop.
word.WdPass
word.wdPass
word, ref Microsoft.Office.Interop.
word.WdFormatSaveOptions.wdFormatSave12x7);}public void CreateA
word() {//实例化
word应用对象this._
wordApplication = new
word.ApplicationClass();//打开一个现有文档this._
wordDocument = this._
wordApplication.Documents.Open(ref Microsoft.Office.Interop.
word.WdContentControlType.wdContentControlDocument, ref Microsoft.Office.Interop.
word.WdSaveMode.wdSaveChanges, ref Microsoft.Office.Interop.
word.WdPass
word.wdPass
word, ref Microsoft.Office.Interop.
word.WdFormatSaveOptions.wdFormatSave12x7);}public void SetPageHeader(string pPageHeader) {//设置页眉this._
wordApplication.ActiveWindow.View.Type = Microsoft.Office.Interop.
word.WdViewType.wdOutlineView;this._
wordApplication.ActiveWindow.View.SeekView = Microsoft.Office.Interop.
word.WdSeekView.wdSeekPrimaryHeader;this._
wordApplication.ActiveWindow.ActivePaneselection.InsertAfter(pPageHeader);//设置页眉对齐方式this._
wordApplication.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.
word.WdParagraphAlignment.wdAlignParagraphCenter;//回到主文档视图this._
wordApplication.ActiveWindow.View.SeekView = Microsoft.Office.Interop.
word.WdSeekView.wdSeekM
AInDocument;}public void InsertText(string pText, int pFontSize, Microsoft.Office.Interop.
word.WdColor pFontColor, int pFontBold, Microsoft.Office.Interop.
word.WdParagraphAlignment pTextAlignment) {//设置字体样式以及方向this._
wordApplication.Application.Selection.Font.Size = pFontSize;this._
wordApplication.Application.Selection.Font.Bold = pFontBold;this._
wordApplication.Application.Selection.Font.Color = pFontColor;this._
wordApplication.Application.Selection.ParagraphFormat.Alignment = pTextAlignment;//插入文本this._
wordApplication.Application.Selection.TypeText(pText);}public void NewLine() {//换行this._
wordApplication.Application.Selection.TypeParagraph();}public void InsertPicture(string pPictureFileName) {//插入图片object myNothing = System.Reflection.Missing.Value;//设置图片显示方式为居中this._
wordApplication.Application.Selection.ParagraphFormat.setAlignment(Microsoft.Office.Interop.
word.WdParagraphAlignment.wdAlignParagraphCenter);this._
wordApplication.ApplicationSelection.InlineShapes.AddPicture(pPictureFileName, ref myNothing, ref myNothing, ref myNothing);}}以上是一个示例代码,实现了通过C#创建一个
word文档并进行文本和图片的插入等操作。在使用前需要先导入Microsoft.Office.Interop.
word命名空间。创建文档时需要先实例化
word应用对象,然后打开一个现有文档或者创建一个新文档。插入文本时可以设置字体大小、粗细、颜色等样式以及对齐方式。插入图片时需要先将其居中显示,并设置图片的文件名。