代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
Asp.Net
】
ASP.NET(C#)生成Word文档
作者:
code-cat
/ 发布于
2011/5/16
/
822
<div> <span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">仅供参考</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(0,0,255)">using</span><span style="color: rgb(0,0,0)"> MSWord </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> Microsoft.Office.Interop.Word; </span><span style="color: rgb(0,0,255)">using</span><span style="color: rgb(0,0,0)"> System.IO; </span><span style="color: rgb(0,0,255)">using</span><span style="color: rgb(0,0,0)"> Sytem.Reflection; </span><span style="color: rgb(0,0,255)">string</span><span style="color: rgb(0,0,0)"> filePath; </span><span style="color: rgb(0,0,255)">string</span><span style="color: rgb(0,0,0)"> content; MSWord.Application wordApp; MSWord.Document wordDoc; filePath </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(128,0,0)">@"</span><span style="color: rgb(128,0,0)">d:\testWord.docx</span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(0,0,0)">; wordApp </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">new</span><span style="color: rgb(0,0,0)"> MSWord.ApplicationClass(); </span><span style="color: rgb(0,0,255)">if</span><span style="color: rgb(0,0,0)">(File.Exists(filePath)) { File.Delete(filePath); } Object nothing </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> Missing.Value; wordDoc </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> wordApp.Documents.Add(</span><span style="color: rgb(0,0,255)">ref</span><span style="color: rgb(0,0,0)"> nothing,</span><span style="color: rgb(0,0,255)">ref</span><span style="color: rgb(0,0,0)"> nothing,</span><span style="color: rgb(0,0,255)">ref</span><span style="color: rgb(0,0,0)"> nothing,</span><span style="color: rgb(0,0,255)">ref</span><span style="color: rgb(0,0,0)"> nothing); </span><span style="color: rgb(0,128,0)">/*</span><span style="color: rgb(0,128,0)"> 1、写入普通文本 </span><span style="color: rgb(0,128,0)">*/</span><span style="color: rgb(0,0,0)"> content </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(128,0,0)">Hello!</span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(0,0,0)">; wordDoc.Paragraphs.Last.Range.Text </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> content; </span><span style="color: rgb(0,0,255)">object</span><span style="color: rgb(0,0,0)"> format </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> MSWord.WdSaveFormat.wdFormatDocument97; </span><span style="color: rgb(0,128,0)">/*</span><span style="color: rgb(0,128,0)"> 2、 写入需要的特殊格式文本 </span><span style="color: rgb(0,128,0)">*/</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">写入15号字体</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(0,0,0)">content </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(128,0,0)">这一行是15号字体的文本</span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(0,0,0)">; wordDoc.Paragraphs.Last.Range.Font.Size </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(128,0,128)">15</span><span style="color: rgb(0,0,0)">; wordDoc.Paragraphs.Last.Range.Text </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> content; </span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">写入斜体文本</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(0,0,0)">content </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(128,0,0)">这一行是斜体文本</span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(0,0,0)">; wordDoc.Paragraphs.Last.Range.Font.Italic </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(128,0,128)">1</span><span style="color: rgb(0,0,0)">; wordDoc.Paragraphs.Last.Range.Text </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> content; </span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">写入红色下划线文本</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(0,0,0)">content </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(128,0,0)">这一行是红色下划线的文本</span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(0,0,0)">; wordDoc.Paragraphs.Last.Range.Font.Underline </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> MSWord.WdUnderline.wdUnderlineThick; wordDoc.Paragraphs.Last.Range.Font.UnderlineColor </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> MSWord.WdColor.wdColorRed; wordDoc.Paragraphs.Last.Range.Text </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> content; </span><span style="color: rgb(0,128,0)">/*</span><span style="color: rgb(0,128,0)"> 3、写入表格 </span><span style="color: rgb(0,128,0)">*/</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">表格对象</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(0,0,0)">MSWord.Table table </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> wordDoc.Tables.Add(wordApp.Selection.Range,</span><span style="color: rgb(128,0,128)">5</span><span style="color: rgb(0,0,0)">, </span><span style="color: rgb(128,0,128)">5</span><span style="color: rgb(0,0,0)">,</span><span style="color: rgb(0,0,255)">ref</span><span style="color: rgb(0,0,0)"> nothing,</span><span style="color: rgb(0,0,255)">ref</span><span style="color: rgb(0,0,0)"> nothing); table.Border.Enable </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(128,0,128)">1</span><span style="color: rgb(0,0,0)">; </span><span style="color: rgb(0,0,255)">for</span><span style="color: rgb(0,0,0)">(</span><span style="color: rgb(0,0,255)">int</span><span style="color: rgb(0,0,0)"> i</span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(128,0,128)">1</span><span style="color: rgb(0,0,0)">;; i</span><span style="color: rgb(0,0,0)"><</span><span style="color: rgb(128,0,128)">6</span><span style="color: rgb(0,0,0)">; i</span><span style="color: rgb(0,0,0)">++</span><span style="color: rgb(0,0,0)">) { </span><span style="color: rgb(0,0,255)">for</span><span style="color: rgb(0,0,0)">(</span><span style="color: rgb(0,0,255)">int</span><span style="color: rgb(0,0,0)"> j</span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(128,0,128)">1</span><span style="color: rgb(0,0,0)">; j</span><span style="color: rgb(0,0,0)"><</span><span style="color: rgb(128,0,128)">6</span><span style="color: rgb(0,0,0)">; j</span><span style="color: rgb(0,0,0)">++</span><span style="color: rgb(0,0,0)">) { table.Cell(i,j).Range.Text </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(128,0,0)">""</span><span style="color: rgb(0,0,0)">+</span><span style="color: rgb(0,0,0)">i</span><span style="color: rgb(0,0,0)">+</span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(128,0,0)">行</span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(0,0,0)">,</span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(128,0,0)">+j+</span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(0,0,0)">列</span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(128,0,0)">;</span><span style="color: rgb(128,0,0)"> </span><span style="color: rgb(0,0,0)">} } </span><span style="color: rgb(0,128,0)">/*</span><span style="color: rgb(0,128,0)"> 4、插入图片 </span><span style="color: rgb(0,128,0)">*/</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">string</span><span style="color: rgb(0,0,0)"> jpgName </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(128,0,0)">@"</span><span style="color: rgb(128,0,0)">d:\logo.jpg</span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(0,0,0)">; Object range </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> wordDoc.Paragraphs.Last.Range; Object linkToFile </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">false</span><span style="color: rgb(0,0,0)">; Object saveWithDocument </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">true</span><span style="color: rgb(0,0,0)">; wordDoc.InlineShapes.AddPicture(jpgName,</span><span style="color: rgb(0,0,255)">ref</span><span style="color: rgb(0,0,0)"> linkToFile,</span><span style="color: rgb(0,0,255)">ref</span><span style="color: rgb(0,0,0)"> saveWithDocument,</span><span style="color: rgb(0,0,255)">ref</span><span style="color: rgb(0,0,0)"> range); </span> wordDoc.SaveAs(ref filePath, ref format, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing); wordDoc.Close(ref nothing, ref nothing,ref nothing); wordApp.Quit(ref nothing,ref nothing,ref nothing); </div>
试试其它关键字
同语言下
.
gzip压缩
.
实现http多线程断点续传下载文件
.
实现多线程断点续传下载大文件
.
生成字符串的 CheckSum
.
根据 UserAgent 获取浏览器的类型和版本
.
根据 Agent 判断是否是智能手机
.
隐藏手机号中间四位为*方法
.
合并图片(二维码和其他图片合并)
.
ASP.NET CORE中判断是否移动端打开网页
.
ASP.NET(C#)实现页面计时(定时)自动跳转
可能有用的
.
gzip压缩
.
实现http多线程断点续传下载文件
.
实现多线程断点续传下载大文件
.
生成字符串的 CheckSum
.
根据 UserAgent 获取浏览器的类型和版本
.
根据 Agent 判断是否是智能手机
.
隐藏手机号中间四位为*方法
.
合并图片(二维码和其他图片合并)
.
ASP.NET CORE中判断是否移动端打开网页
.
ASP.NET(C#)实现页面计时(定时)自动跳转
code-cat
贡献的其它代码
(
2
)
.
Global.asax实现屏蔽ip和图片防盗链
.
ASP.NET(C#)生成Word文档
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3