c#输出word格式文件
来源:互联网 发布:免费彩铃制作软件 编辑:程序博客网 时间:2024/06/06 12:43
using System;using System.Collections.Generic;using System.Linq;using System.Text;using Microsoft.Office.Interop;using Word = Microsoft.Office.Interop.Word;namespace WindowsFormsApplication1{ public class myword { /// /// word 应用对象 /// private Microsoft.Office.Interop.Word.Application _wordApplication; /// /// word 文件对象 /// private Microsoft.Office.Interop.Word.Document _wordDocument; /// /// 创建文档 /// object nothing = System.Reflection.Missing.Value; //用于作为函数的默认参数 public void CreateAWord() { //实例化word应用对象 this._wordApplication = new Microsoft.Office.Interop.Word.Application(); Object myNothing = System.Reflection.Missing.Value; this._wordDocument = this._wordApplication.Documents.Add(ref myNothing, ref myNothing, ref myNothing, ref myNothing); } public void CloseWord(Word.WdSaveOptions psaveopt) { object saveopt = psaveopt; _wordDocument.Close(ref saveopt, ref nothing, ref nothing); _wordApplication.Application.Quit(ref saveopt , ref nothing, ref nothing); } /// /// 添加页眉 /// /// 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.wdAlignParagraphRight; //跳出页眉设置 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.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter; this._wordApplication.Application.Selection.InlineShapes.AddPicture(pPictureFileName, ref myNothing, ref myNothing, ref myNothing); } /// /// 保存文件 /// /// 保存的文件名 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文档失败!"); } } }}
在工程中需要添加一个对office组件的引用。具体操作可使用www.baidu.com工具,十分简单
- c#输出word格式文件
- c# word输出
- c# word输出
- C#输出Word文档
- C#对word、excel、pdf等格式文件的操作总结
- C#对word、excel、pdf等格式文件的操作总结
- 输出Excel格式文件
- MapReduce输出压缩格式文件
- sqlplus输出HTML格式文件
- C#对word、excel、pdf、csv等格式文件的操作总结
- 浅析doc(word)格式文件内部结构
- C# 解析 PE格式文件
- C#解释Json格式文件
- VBA 输出utf-8格式文件
- C# .Net输出word和excel文件方法和函数
- 在线显示PDF、WORD、EXCEL格式文件
- Java抽取Word和PDF格式文件
- .net实现导出Word、Excel格式文件
- TClientDataSet[5]: 读取数据
- good site
- Java中的线程的唤醒Thread.interrupt()
- 页面上用c标签循环输出结果集时,怎么按一定数量和整齐的格式排序
- Win8系统下VS2012添加MSComm控件
- c#输出word格式文件
- C++面试题100题之前50题
- 机器视觉中常用的数据测试集
- PHP之fsockopen提交POST数据讲解
- win8 vs2012发布Web应用程序
- jdbc常用代码总结
- 黑马程序员-java内省学习笔记
- 谱聚类
- C#编码好习惯