为方便下次遇到不知道去哪找先把它存放在这里,以下是保存导出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文档的更多相关文章

  1. .NET通过调用Office组件导出Word文档

    .NET通过调用Office组件导出Word文档 最近做项目需要实现一个客户端下载word表格的功能,该功能是用户点击"下载表格",服务端将该用户的数据查询出来并生成数据到Word ...

  2. C# 导出word文档及批量导出word文档(1)

         这里用到了两个dll,一个是aspose.word.dll,另外一个是ICSharpCode.SharpZipLib.dll,ICSharpCode.SharpZipLib.dll是用于批量 ...

  3. C# 导出word文档及批量导出word文档(4)

          接下来是批量导出word文档和批量打印word文件,批量导出word文档和批量打印word文件的思路差不多,只是批量打印不用打包压缩文件,而是把所有文件合成一个word,然后通过js来调用 ...

  4. C#导出Word文档开源组件DocX

    1.帮助文档,这东西找了很久,而且它版本很旧,还是英文,W8.1系统上打不开 http://download.csdn.net/detail/zuofangyouyuan/7673573 2.开源网址 ...

  5. freemarker导出word文档——WordXML格式解析

    前不久,公司一个项目需要实现导出文档的功能,之前是一个同事在做,做了3个星期,终于完成了,但是在项目上线之后却发现导出的文档有问题,此时,这个同事已经离职,我自然成为接班者,要把导出功能实现,但是我看 ...

  6. 自动生成并导出word文档

    今天很荣幸又破解一现实难题:自动生成并导出word文档 先看页面效果: word效果: 代码: 先搭建struts2项目 创建action,并在struts.xml完成注册 <?xml vers ...

  7. Java 用Freemarker完美导出word文档(带图片)

    Java  用Freemarker完美导出word文档(带图片) 前言 最近在项目中,因客户要求,将页面内容(如合同协议)导出成word,在网上翻了好多,感觉太乱了,不过最后还是较好解决了这个问题. ...

  8. freemarker导出word文档

    使用freemarker导出word文档的过程 **************************************************************************** ...

  9. 【Java】导出word文档之freemarker导出

    Java导出word文档有很多种方式,本例介绍freemarker导出,根据现有的word模板进行导出 一.简单导出(不含循环导出) 1.新建一个word文件.如下图: 2.使用word将文件另存为x ...

  10. PowerDesigner导出word,PowerDesigner把表导出到word,PDM导出word文档

    PowerDesigner导出word,PowerDesigner把表导出到word,PDM导出word文档 >>>>>>>>>>>& ...

随机推荐

  1. JS判断字符串长度(英文占1个字符,中文汉字占2个字符)

    //计算字符串长度(英文占1个字符,中文汉字占2个字符) 方法一: String.prototype.gblen = function() { var len = 0; for (var i=0; i ...

  2. AtCoder Beginner Contest 058 ABCD题

    A - ι⊥l Time limit : 2sec / Memory limit : 256MB Score : 100 points Problem Statement Three poles st ...

  3. ACM_X章求和(数学)

    X章求和 Time Limit: 2000/1000ms (Java/Others) Problem Description: X章最喜欢求和了,他一看到什么鬼就什么鬼都加起来.one day,他得到 ...

  4. hdu 1979 DFS + 字典树剪枝

    http://acm.hdu.edu.cn/showproblem.php?pid=1979 Fill the blanks Time Limit: 3000/1000 MS (Java/Others ...

  5. C# 部分命名规则

    接触C#开发已经四个月,整理下C#中的命名规则: 一:变量的命名规则(和Java相似) 1.变量名由字母.数字.下划线组成 2.变量名开头只能以字母.下划线开头,不能以数字开头 3.区分大小写 4.命 ...

  6. 浏览器上传文件,存到oracle数据库示例。

    这里只贴了一张图, 旨在说明,思路: 将文件转换为字节,存入数据库的类型为 Blob字段. 当下载的时候,从数据库读出来通过流写回浏览器即可 文件的下载. 从数据库读出来通过流写回浏览器即可

  7. 生成器的send方法

    send 和next区别 next:唤醒并继续执行 send:唤醒并继续执行 发送信息到生成器内部. def fib(max): n,a,b = 0,0,1 while n < max: msg ...

  8. 【学习笔记】Sass入门指南

    本文将介绍Sass的一些基本概念,比如说“变量”.“混合参数”.“嵌套”和“选择器继承”等.著作权归作者所有. 什么是Sass? Sass是一门非常优秀的CSS预处语言,他是由Hampton Catl ...

  9. idea 下maven 导入本地jar,以及导入之后 java不能引用问题

    1.在当前的项目中新建立一个lib文件夹,将需要导入的jar放入其中. 2.配置pom.xml 文件 <!--导入本地jar--> <dependency> <group ...

  10. FileZilla Server 端设置passive模式注意事项

    1,需求和问题的产生 实践中需要分布在各地的各个客户端向云端服务器上传文件,因此在阿里云服务器上安装了FileZilla Server软件作为文件FTP服务端. 客户端程序采用FTP方式向服务端传输文 ...