C#有哪些比较方便使用的生成Word文档的工具

word

1个回答

写回答

Nn寻优

2025-10-03 12:55

+ 关注

word
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.WdPassword.wdPassword, ref Microsoft.Office.Interop.word.WdFormatSaveOptions.wdFormatSave12x7);}public void CreateAword() {//实例化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.WdPassword.wdPassword, 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.wdSeekMAInDocument;}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应用对象,然后打开一个现有文档或者创建一个新文档。插入文本时可以设置字体大小、粗细、颜色等样式以及对齐方式。插入图片时需要先将其居中显示,并设置图片的文件名。

举报有用(4分享收藏

Copyright © 2025 IZhiDa.com All Rights Reserved.

知答 版权所有 粤ICP备2023042255号