用C#生成word文档的方法
2010-03-08 15:13
281 查看
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 />
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 />文本信息
/// <PARAM />字体打小
/// <PARAM />字体颜色
/// <PARAM />字体粗体
/// <PARAM />方向
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 />
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 />保存的文件名
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文档失败!");
}
}
}
{
/// <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 />
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 />文本信息
/// <PARAM />字体打小
/// <PARAM />字体颜色
/// <PARAM />字体粗体
/// <PARAM />方向
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 />
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 />保存的文件名
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文档失败!");
}
}
}
相关文章推荐
- C#也能动态生成Word文档并填充数据, 导出EXCEL 方法
- C#也能动态生成Word文档并填充数据
- C#生成mht文档相关方法
- 用C#实现生成PDF文档的方法
- [转]浅析C#编程实现动态生成Word文档_.Net教程
- C#向Word文档中添加内容控件的方法示例
- C#也能动态生成Word文档并填充数据
- asp.net下用Aspose.Words for .NET动态生成word文档中的图片或水印的方法
- C#依据word模版动态生成文档
- C#也能动态生成Word文档并填充数据
- 将网页内容转换成word或excel文档的方法(c#.net)
- c#获取txt,word,excel文档内容方法
- asp.net下用Aspose.Words for .NET动态生成word文档中的数据表格的方法(转)
- C#:简单实现动态数据生成Word文档并保存
- C# 动态生成Word文档
- 用C#实现生成PDF文档和将WORD转换为PDF
- Java POI通过模板方法生成word文档
- 程序生成word与PDF文档的方法(python)
- C#也能动态生成Word文档并填充数据
- c#生成的word文档,怎么控制选定的内容加下划线而不是整行加下划线