c#导出word文档
为方便下次遇到不知道去哪找先把它存放在这里,以下是保存导出word主要类方法
public class BiultReportForm
{
/// <summary>word 应用对象 </summary>
private Microsoft.Office.Interop.Word.Application _wordApplication; /// <summary>word 文件对象 </summary>
private Microsoft.Office.Interop.Word.Document _wordDocument; /// <summary>
/// 创建word应用对象
/// </summary>
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);
} /// <summary>
/// 创建word应用对象
/// </summary>
/// <param name="strPath">文件目录</param>
public void CreateAWord(string strPath)
{
//判断是否存在目录没有就创建
if (!Directory.Exists(strPath))
{
Directory.CreateDirectory(strPath);
}
//实例化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);
} /// <summary>
/// 添加页眉
/// </summary>
/// <param name="pPageHeader">标题内容</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 name="pText">文本信息</param>
/// <param name="pFontSize">字体打小</param>
/// <param name="pFontColor">字体颜色</param>
/// <param name="pFontBold">字体粗体</param>
/// <param name="ptextAlignment">方向</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 name="pPictureFileName">图片名称</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 name="pFileName">传入路径和保存的文件名称</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>
/// <param name="_directory">目录</param>
/// <param name="strPath">地址</param>
private void CreatrDirectory(string _directory, string strPath)
{
//判断是否存在目录没有就创建
if (!Directory.Exists(_directory))
{
Directory.CreateDirectory(_directory);
}
//...
}
/// <summary>
/// 获得当前绝对路径
/// </summary>
/// <param name="strPath">指定的路径</param>
/// <returns>绝对路径</returns>
public string GetMapPath(string strPath)
{
if (strPath.ToLower().StartsWith("http://"))
{
return strPath;
}
if (HttpContext.Current != null)
{
return HttpContext.Current.Server.MapPath(strPath);
}
else //非web程序引用
{
strPath = strPath.Replace("/", "\\");
if (strPath.StartsWith("\\"))
{
strPath = strPath.Substring(strPath.IndexOf('\\', )).TrimStart('\\');
}
return System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, strPath);
}
} }
前台调用部分
先保证当前文档名称不重复
//时间+随机数
private string chkCodeRequest()
{ string chkCode = string.Empty; //随机的字符集
char[] character = { '', '', '', '', '', '', '', 'a', 'b', 'd', 'e', 'f', 'h', 'k', 'm', 'n', 'r', 'x', 'y', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'R', 'S', 'T', 'W', 'X', 'Y' };
Random rnd = new Random();
//字符串
for (int i = ; i < ; i++)
{
chkCode += character[rnd.Next(character.Length)];
}
chkCode += DateTime.Now.ToString("yyyyMMddHHmmssffff");
return chkCode ;
}
保存部分
//写入word与保存
private void SavWord()
{
BiultReportForm word = new BiultReportForm();
string patc = @"f:\测试文件名称\";//目录
word.CreateAWord(patc);//可以带目录参数也可为空
word.SetPageHeader("测试页眉");//设置页面 如果没有就不调用
string str = "dsws";
for (int i = ; i < ; i++)
{
str += i.ToString();
word.InsertText(str, , Microsoft.Office.Interop.Word.WdColor.wdColorBlue, i, Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter);//插入内容
word.NewLine();//换行
} patc += "测试文件名称";
patc += chkCodeRequest();
word.SaveWord(patc);
}
或者
SaveFileDialog save = new SaveFileDialog(); //过滤器
save.Filter = "*.doc|*.doc|(*.*)|*.*"; //显示
if (save.ShowDialog() == DialogResult.OK)
{
string name = save.FileName;
// FileInfo info = new FileInfo(name);
//info.Create();
BiultReportForm word = new BiultReportForm();
word.CreateAWord();
word.SetPageHeader("测试页眉");
string str = "dsws";
for (int i = ; i < ; i++)
{
str += i.ToString();
word.InsertText(str, , Microsoft.Office.Interop.Word.WdColor.wdColorBlue, i, Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter);//插入内容
word.NewLine();//换行
}
word.SaveWord(name); }
c#导出word文档的更多相关文章
- .NET通过调用Office组件导出Word文档
.NET通过调用Office组件导出Word文档 最近做项目需要实现一个客户端下载word表格的功能,该功能是用户点击"下载表格",服务端将该用户的数据查询出来并生成数据到Word ...
- C# 导出word文档及批量导出word文档(1)
这里用到了两个dll,一个是aspose.word.dll,另外一个是ICSharpCode.SharpZipLib.dll,ICSharpCode.SharpZipLib.dll是用于批量 ...
- C# 导出word文档及批量导出word文档(4)
接下来是批量导出word文档和批量打印word文件,批量导出word文档和批量打印word文件的思路差不多,只是批量打印不用打包压缩文件,而是把所有文件合成一个word,然后通过js来调用 ...
- C#导出Word文档开源组件DocX
1.帮助文档,这东西找了很久,而且它版本很旧,还是英文,W8.1系统上打不开 http://download.csdn.net/detail/zuofangyouyuan/7673573 2.开源网址 ...
- freemarker导出word文档——WordXML格式解析
前不久,公司一个项目需要实现导出文档的功能,之前是一个同事在做,做了3个星期,终于完成了,但是在项目上线之后却发现导出的文档有问题,此时,这个同事已经离职,我自然成为接班者,要把导出功能实现,但是我看 ...
- 自动生成并导出word文档
今天很荣幸又破解一现实难题:自动生成并导出word文档 先看页面效果: word效果: 代码: 先搭建struts2项目 创建action,并在struts.xml完成注册 <?xml vers ...
- Java 用Freemarker完美导出word文档(带图片)
Java 用Freemarker完美导出word文档(带图片) 前言 最近在项目中,因客户要求,将页面内容(如合同协议)导出成word,在网上翻了好多,感觉太乱了,不过最后还是较好解决了这个问题. ...
- freemarker导出word文档
使用freemarker导出word文档的过程 **************************************************************************** ...
- 【Java】导出word文档之freemarker导出
Java导出word文档有很多种方式,本例介绍freemarker导出,根据现有的word模板进行导出 一.简单导出(不含循环导出) 1.新建一个word文件.如下图: 2.使用word将文件另存为x ...
- PowerDesigner导出word,PowerDesigner把表导出到word,PDM导出word文档
PowerDesigner导出word,PowerDesigner把表导出到word,PDM导出word文档 >>>>>>>>>>>& ...
随机推荐
- only-child选择器
:only-child选择器用于匹配属于某一个父元素的唯一子元素的元素,也就是说,如果某个父元素仅有一个子元素,则使用 :only-chlid选择器 ,可以选择这个子元素
- js实时获取并显示当前时间的方法
- Codeforces Round #302 (Div. 1) 训练
链接: http://codeforces.com/contest/543 过程: 惨淡的只做出了A和C 题解: A 题解: 简单的一道题 我们用$dp[i][j]$表示当前考虑到前num个人(这个另 ...
- 模拟 百度之星资格赛 1003 IP聚合
题目传送门 /* 模拟水题,排序后找出重复的ip就可以了 */ #include <cstdio> #include <iostream> #include <algor ...
- C. Tennis Championship dp递推 || 找规律
http://codeforces.com/contest/735/problem/C C. Tennis Championship time limit per test 2 seconds mem ...
- poj2112Optimal Milking(二分+最大流)
链接 floyd求出牛到机器的最短距离,二分距离,小于当前距离的边容量设为1,求出满容量下的最短距离. EK算法 #include <iostream> #include<cstdi ...
- 虚方法(virtual)
虚方法(virtual) Virtual 关键字用于修饰方法.属性.索引器或事件声明,并且允许在派生类中重写这些对象. 看一段代码: using System ; class A { public v ...
- spring boot 的redis 之初理解
项目到末尾了快, 这几天安排我结合业务场景给项目加上redis 缓存, 我接到这个任务也是懵逼了一会儿: 问了一句让我自己先想办法,没办法硬着头皮查吧, 要不不得不说spring boot 还是好用, ...
- 04.Java多线程并发库API使用3
1.java5的Semaphere同步工具 Semaphore可以维护当前访问自身的线程个数,并提供了同步机制.使用Semaphore可以控制同时访问资源的线程个数,例如,实现一个文件允许的并发访问数 ...
- ZIP解压缩文件的工具类【支持多级目录|全】
ZIP解压缩文件的工具类[支持多级目录|全] 作者:Vashon 网上有很多的加压缩示例代码,但是都只是支持一级目录的操作,如果存在多级目录的话就不行了.本解压缩工具类经过多次检查及重构,最终分享给大 ...