C_生成的word文档(完整源代码)

时间:2025-05-03

public class BiultReportForm

{

/// <SUMMARY></SUMMARY>

/// word 应用对象

///

private Microsoft.Office.Interop.Word.Application _wordApplication;

/// <SUMMARY></SUMMARY>

/// word 文件对象

///

private Microsoft.Office.Interop.Word.Document _wordDocument;

/// <SUMMARY></SUMMARY>

/// 创建文档

///

public void CreateAWord()

{

//实例化word应用对象

this._wordApplication = new Microsoft.Office.Interop.Word.ApplicationClass(); Object myNothing = System.Reflection.Missing.Value;

this._wordDocument = this._wordApplication.Documents.Add(ref myNothing, ref myNothing, ref myNothing, ref myNothing);

}

/// <SUMMARY></SUMMARY>

/// 添加页眉

///

/// <PARAM name="pPageHeader" />

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.ActivePane.Selection.InsertAfter(pPageHeader); //设置中间对齐

this._wordApplication.Selection.ParagraphFormat.Alignment =Microsoft .Office .Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;

//跳出页眉设置

this._wordApplication.ActiveWindow.View.SeekView = Microsoft.Office.Interop.Word.WdSeekView.wdSeekMainDocument;

}

/// <SUMMARY></SUMMARY>

/// 插入文字

///

/// <PARAM name="pText" />文本信息

/// <PARAM name="pFontSize" />字体打小

/// <PARAM name="pFontColor" />字体颜色

/// <PARAM name="pFontBold" />字体粗体

/// <PARAM name="ptextAlignment" />方向

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);

}

/// <SUMMARY></SUMMARY>

/// 换行

///

public void NewLine()

{

//换行

this._wordApplication.Application.Selection.TypeParagraph();

}

/// <SUMMARY></SUMMARY>

/// 插入一个图片

///

/// <PARAM name="pPictureFileName" />

public void InsertPicture(string pPictureFileName)

{

object myNothing = System.Reflection.Missing.Value;

//图片居中显示

this._wordApplication.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;

this._wordApplication.Application.Selection.InlineShapes.AddPicture(pPictureFileName, ref myNothing, ref myNothing, ref myNothing);

}

/// <SUMMARY></SUMMARY>

/// 保存文件

/// =

/// <PARAM name="pFileName" />保存的文件名

public void SaveWord(string pFileName)

{

object myNothing = System.Reflection.Missing.Value;

object myFileName = pFileName;

object myWordFormatDocument =Microsoft .Office .Interop .Word.WdSaveFormat.wdFormatDocument;

object myLockd = false;

object myPassword = "";

object myAddto = true;

try

{

this._wordDocument.SaveAs(ref myFileName, ref myWordFormatDocument, ref myLockd, ref myPassword, ref myAddto, ref myPassword,

ref myLockd, ref myLockd, ref myLockd, ref myLockd, ref myNothing, ref myNothing, ref myNothing,

ref myNothing, ref myNothing, ref myNothing);

}

catch

{

throw new Exception("导出word文档失败!");

}

}

}

using Word;

下面的例子中包括C#对Word文档的创建、插入表格、设置样式等操作:

(例子中代码有些涉及数据信息部分被省略,重要是介绍一些C#操作word文档的方法) public string CreateWordFile(string CheckedInfo)

...{

string message = "";

try

...{

Object Nothing = System.Reflection.Missing.Value;

Directory.CreateDirectory("C:/CNSI"); //创建文件所在目录

string name = "CNSI_" + DateTime.Now.ToShortString()+".doc";

object filename = "C://CNSI//" + name; //文件保存路径

//创建Word文档

Word.Application WordApp = new Word.ApplicationClass();

Word.Document WordDoc = 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.ParagraphFormat.Alignment =

Word.WdParagraphAlignment.wdAlignParagraphRight;//设置右对齐

WordApp.ActiveWindow.View.SeekView =

WdSeekView.wdSeekMainDocument;//跳出页眉设置

WordApp.Selection.ParagraphFormat.LineSpacing = 15f;//设置文档的行间距 //移动焦点并换行

object count = 14;

object WdLine = Word.WdUnits.wdLine;//换一行;

…… 此处隐藏:10254字,全部文档内容请下载后查看。喜欢就下载吧 ……

C_生成的word文档(完整源代码).doc 将本文的Word文档下载到电脑

    精彩图片

    热门精选

    大家正在看

    × 游客快捷下载通道(下载后可以自由复制和排版)

    限时特价:7 元/份 原价:20元

    支付方式:

    开通VIP包月会员 特价:29元/月

    注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
    微信:fanwen365 QQ:370150219