c#生成word文档
参考:http://blog.163.com/zhouchunping_99/blog/static/7837998820085114394716/
- 生成word文档
生成word文档
view plaincopy to clipboardprint?
- 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文档失败!");
- }
- }
- }
c#生成word文档的更多相关文章
- Aspose.Words简单生成word文档
Aspose.Words简单生成word文档 Aspose.Words.Document doc = new Aspose.Words.Document(); Aspose.Words.Documen ...
- ASP.NET生成WORD文档,服务器部署注意事项
网上转的,留查备用,我服务器装的office2007所以修改的是Microsoft Office word97 - 2003 文档这一个. ASP.NET生成WORD文档服务器部署注意事项 1.Asp ...
- POI生成WORD文档
h2:first-child, body>h1:first-child, body>h1:first-child+h2, body>h3:first-child, body>h ...
- PowerDesigner将PDM导出生成WORD文档
PowerDesigner将PDM导出生成WORD文档 环境 PowerDesigner15 1.点击Report Temlates 制作模板 2.如果没有模板,单击New图标创建.有直接双击进入. ...
- velocity模板技术生成word文档
本文介绍採用velocity技术在Java中生成word文档的方法. 1.新建一个word文档,编辑内容例如以下: 2.将上述word文档另存为htm格式的文件 3.新建一个Java Project项 ...
- 使用C#动态生成Word文档/Excel文档的程序测试通过后,部署到IIS服务器上,不能正常使用的问题解决方案
使用C#动态生成Word文档/Excel文档的程序功能调试.测试通过后,部署到服务器上,不能正常使用的问题解决方案: 原因: 可能asp.net程序或iis访问excel组件时权限不够(Ps:Syst ...
- 用php生成word文档
一.用windows里面自带的com,然后用php生成word文档 <?php $word= new COM("word.application") or die(" ...
- 将HTML导出生成word文档
前言: 项目开发中遇到了需要将HTML页面的内容导出为一个word文档,所以有了这边随笔. 当然,项目开发又时间有点紧迫,第一时间想到的是用插件,所以百度了下.下面就介绍两个导出word文档的方法. ...
- POI生成word文档完整案例及讲解
一,网上的API讲解 其实POI的生成Word文档的规则就是先把获取到的数据转成xml格式的数据,然后通过xpath解析表单式的应用取值,判断等等,然后在把取到的值放到word文档中,最后在输出来. ...
随机推荐
- 使用批处理文件命令行方式快速启动和停止IIS、SqlServer
原文:使用批处理文件命令行方式快速启动和停止IIS.SqlServer 虽然现在内存便宜了,但是自己还是嫌自己的512M内存太小,没办法,后台运行的东西太多了,有很多都是有用的没法关闭的.IIS和SQ ...
- ABP应用层——权限验证
ABP应用层——权限验证 ABP是“ASP.NET Boilerplate Project (ASP.NET样板项目)”的简称. ABP的官方网站:http://www.aspnetboilerpla ...
- thinkphp学习笔记3—项目编译和调试模式
原文:thinkphp学习笔记3-项目编译和调试模式 1.项目编译 在章节2.4项目编译中作者讲到使用thinkphp的项目在第一次运行的时候会吧核心需要加载的文件去掉空白和注释合并到一个文件中编译并 ...
- 朴素贝叶斯算法(Naive Bayes)
朴素贝叶斯算法(Naive Bayes) 阅读目录 一.病人分类的例子 二.朴素贝叶斯分类器的公式 三.账号分类的例子 四.性别分类的例子 生活中很多场合需要用到分类,比如新闻分类.病人分类等等. 本 ...
- google面试题,男孩男女比例?
Google面试题: 在一个重男轻女的国家里,每一个家庭都想生男孩.假设他们生的孩子是女孩.就再生一个,直到生下的是男孩为止,这种国家.男女比例会是多少? 答案:1:1 分析: 出生男女概率是50% ...
- ZOJ 1649:Rescue(BFS)
Rescue Time Limit: 2 Seconds Memory Limit: 65536 KB Angel was caught by the MOLIGPY! He was put ...
- codeforces 438D
在大大推荐下这个标题不明的人做.而我的最后一个非常喜欢的段树,因此,愤怒出手.认为基本上相同.大值,当最大值小于取模时能够剪枝. 今后再遇到此类问题算是能攻克了 // file name: d.cpp ...
- Oracle使用并行索引需要注意的问题
当索引的结构.我们要建立索引快.它将并行加,加平行后.这将平行的列索引. 当并行度索引访问,CBO你可能会考虑并行运行,这可能会导致一些问题.作为server候用并行会引起更加严重的争用.当使用并行后 ...
- TypeScript 基本语法
TypeScript 基本语法 TypeScript 是微软开发的 JavaScript 的超集,TypeScript兼容JavaScript,可以载入JavaScript代码然后运行.TypeS ...
- hdu More is better
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1856 题意:王老师要找一些男生帮助他完成一项工程.要求最后挑选出的男生之间都是朋友关系,可以说直接的, ...