为方便下次遇到不知道去哪找先把它存放在这里,以下是保存导出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. android 多线程(二)

    1. 使用 AsyncTask 实现进度条 package com.test.network; import android.os.AsyncTask; import android.support. ...

  2. h5-22-地理位置

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  3. collection接口的实现:set,list,queue

    在java.util包中提供了一些集合类,常用的有List.Set和Map类,其中List类和Set类继承了Collection接口.这些集合类又称为容器,长度是可变的,数组用来存放基本数据类型的数据 ...

  4. SpringBoot 2.x (7):拦截器

    类似以前SpringMVC的拦截器,但也有一些区别 SpringBoot的拦截器有两种方式: 第一种方式:过时的方式,适用于SpringBoot1.x的方式 package org.dreamtech ...

  5. php 缓存工具类 实现网页缓存

    php 缓存工具类 实现网页缓存 php程序在抵抗大流量访问的时候动态网站往往都是难以招架,所以要引入缓存机制,一般情况下有两种类型缓存 一.文件缓存 二.数据查询结果缓存,使用内存来实现高速缓存 本 ...

  6. iOS开发之cell位置contentOffset的用法

    @property(nonatomic)         CGPoint                      contentOffset;                  // default ...

  7. toast插件的简单封装(样式适用pc后台管理系统的场景)

    直接分三个步骤吧: 1.手写一个toast.vue组件 <template> <transition name="toast-fade"> <div ...

  8. js递归和数组去重(简单便捷的用法)

    1.递归例子<script type="text/javascript"> function test(num) { if(num < 0) { return; ...

  9. C++#pragma pack指令

    微软官方文档说#pragma pack 指令的作用是为结构.联合和类成员指定 pack 对齐.的主要作用就是改变编译器的内存对齐方式,这个指令在网络报文的处理中有着重要的作用,#pragma pack ...

  10. SQLite与MySQL、SQLServer等异构数据库之间的数据同步

    SQLite DBSync是开源嵌入式数据库SQLite的数据同步引擎,实现了SQLite与SQLite数据库之间以及SQLite与异构数据库(Oracle.MySQL.SQLServer)之间的增量 ...