在初始化WordHelper时,要获取模板的相对路径。获取文档的相对路径多个地方要用到,比如批量导出时要先保存文件到指定路径下,再压缩打包下载,所以专门写了个关于获取文档的相对路径的类。

  #region 获取文档的相对路径
public class WordFilePath
{
#region 返回文件带路径值
/// <summary>
/// 返回文件带路径值
/// </summary>
/// <param name="FilePth">文件所在文件夹名称</param>
/// <param name="FileName">文件名</param>
/// <returns></returns>
public static string FileWithFilePath(string FilePth, string FileName = null)
{
string strFile = "";
if (!string.IsNullOrEmpty(FilePth))
strFile += "~/" + FilePth + "/";
if (!string.IsNullOrEmpty(FileName))
strFile += FileName; return strFile;
}
#endregion #region 判断文件或文件夹是否存在
/// <summary>
/// 判断文件或文件夹是否存在
/// </summary>
/// <param name="FilePth">文件所在文件夹名称</param>
/// <param name="FileName">文件名</param>
/// <returns></returns>
public static bool ExistFile(string FilePth, string FileName = null)
{
return System.IO.File.Exists(GetFilePath(FilePth, FileName));
}
#endregion #region 获取文件及文件夹的相对路径
/// <summary>
/// 获取文件及文件夹的相对路径
/// </summary>
/// <param name="FilePth"></param>
/// <param name="FileName"></param>
/// <returns></returns>
public static string GetFilePath(string FilePth, string FileName = null)
{
string strPath = "";
strPath = FileWithFilePath(FilePth, FileName);
strPath = HttpContext.Current.Request.MapPath(strPath);
return strPath;
}
#endregion
}
#endregion

以上方法可以实现了单个word文档带图片的导出功能了,多个文档的生成也可以实现,只是还没有打包下载的代码。
     最后调用的方法如下,要先判断模板是否存在:

  string strpath = "Content/templates"; //模板所在的文件文件夹
string templateFile = "实习生学习记录表导出模板.doc";
if (WordFilePath.ExistFile(strpath, templateFile))
{
string saveName = WordHelper.SaveDocName(form["NTName"], form["CardNo"]); //保存的名称
saveName = HttpUtility.UrlEncode(saveName, Encoding.GetEncoding("utf-8"));
WordHelper wordhelper = new WordHelper(templateFile);
getWordInfo(wordhelper, templateFile, NTID, stype.ToString(), majorid.ToString(), sequence.ToString());
return base.File(wordhelper.ExportDoc().ToArray(), "application/msword", saveName);
}
else
{
return ShowRedirectMessage("导出的模板不存在!", strUrl);
}

getWordInfo方法是为将所需的信息拼凑成一个word文档,减少代码的重复性而提取出来的,如:

 #region 将所有信息拼凑成一个word文档
public void getWordInfo(WordHelper wordhelper, string tempFile, string ntid, string stype, string mid, string sequence)
{
if (tempFile.Contains("实习生学习记录表导出模板"))
{
Dictionary<string, string> dicWhere = new Dictionary<string, string>();
dicWhere.Add("NTID", ntid);
wordhelper.GetBasicInfo(typeof(BLL.NewTraineeInfo), dicWhere);
PrejobTrainWord(wordhelper, ntid);
TrainListWord(wordhelper, ntid, stype, mid);
AwardListWord(wordhelper, ntid, stype, mid);
ArrangeListWord(wordhelper, ntid, stype);
ScoreListWord(wordhelper, ntid, stype, mid, sequence);
EvalWord(wordhelper, ntid, stype, mid);
}
} #region 其他信息
//岗前培训
public void PrejobTrainWord(WordHelper wordhelper, string ntid)
{
Dictionary<string, string> dicWhere = new Dictionary<string, string>();
dicWhere.Add("NTID", ntid);
wordhelper.GetBasicInfo(typeof(BLL.PrejobTraining), dicWhere);
} //轮科安排表
public void ArrangeListWord(WordHelper wordhelper, string ntid, string stype)
{
Dictionary<string, string> dicWhere = new Dictionary<string, string>();
dicWhere = new Dictionary<string, string>();
dicWhere.Add("NTID", ntid);
dicWhere.Add("StudentType", stype.ToString());
wordhelper.GetTableList(typeof(BLL.MajorCycle), dicWhere, "ArrangeList");
}
    ArrangeList是之前在word模板里所设定的如下图,用for循环出列表,

getWordInfo里其它的方法类似这两个。

最后导出的word文档大致如下图所示:

C# 导出word文档及批量导出word文档(3)的更多相关文章

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

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

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

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

  3. 吴裕雄--天生自然python学习笔记:python文档操作批量替换 Word 文件中的文字

    我们经常会遇到在不同的 Word 文件中的需要做相同的文字替换,若是一个一个 文件操作,会花费大量时间 . 本节案例可以找出指定目录中的所有 Word 文件(包含 子目录),并对每一个文件进行指定的文 ...

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

    aspose.word主要是通过把读取出来的数据放到datatable里,在datable里做相应的格式的调整,再导出到word文档里.mvc和webform最后导出的语句略有不同,在mvc的cont ...

  5. ATF批量导出工具

    ATF批量导出工具 08 / 31, 2013 批量导出Atf的工具,使用是adobe atf 编码核心 先说一下关于atf的bug 当atf导出时候启用了mips选项会导致:如果纹理问长方形时上传会 ...

  6. C#操作Word的+ CKEditor 輸出成Word文件(包含圖案上傳)

    C#操作Word 参考博文: C#操作word类文件 https://www.cnblogs.com/walking/p/3571068.html C#中的Office操作专栏(21) http:// ...

  7. 批量导出access某表内容到word文档

    一.需求: 需要将表中每一条记录中的某些内容导出在一个word文档中,并将这些文档保存在指定文件夹目录下 二.界面,简单设计如下: 三.添加office相关引用 添加后可在解决方案资源管理器中看到: ...

  8. c#操作word文档之简历导出

    前言 1.写这个功能之前,我得说说微软的这个类库,用着真苦逼!是他让我有程序猿,攻城尸的感觉了.首先这个类库,从没接触过,方法与属性都不懂,还没有提示.神啊,我做这功能真是一步一卡,很潇洒啊. 2.这 ...

  9. java使用freemarker模板导出word(带有合并单元格)文档

    来自:https://blog.csdn.net/qq_33195578/article/details/73790283 前言:最近要做一个导出word功能,其实网上有很多的例子,但是我需要的是合并 ...

随机推荐

  1. UI基础 - UIScrollView

    scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(, , , )]; scrollView.backgroundColor = [ ...

  2. iOS常见内存泄漏解决

    iOS常见内存泄漏解决     1 OC和CF转化出现的内存警告 CFStringRef cfString = CFURLCreateStringByAddingPercentEscapes(kCFA ...

  3. 台积电16nm工艺为什么好过三星14nm

    最近,关于iPhone6s A9处理器版本的事情的话题很热,最后都闹到苹果不得不出来解释的地步,先不评判苹果一再强调的整机综合续航差2~3%的准确性,但是三星14nm工艺相比台积电16nm工艺较差已经 ...

  4. [项目构建 十四]babasport Mycat配置及使用详解.

    首先我们来看下什么是Mycat:MyCat:开源分布式数据库中间件, 这里定义的很简单, 就是分布式数据库的中间件. 其实Mycat 是可以时mysql进行集群的中间件, 我们可以对mysql来分库分 ...

  5. Currency Exchange(判断是否有正环)

    Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 16456   Accepted: 5732 Description Seve ...

  6. BZOJ1646: [Usaco2007 Open]Catch That Cow 抓住那只牛

    1646: [Usaco2007 Open]Catch That Cow 抓住那只牛 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 634  Solved ...

  7. 将使用netTcp绑定的WCF服务寄宿到IIS7上全记录 (这文章也不错)

    原文地址:http://www.cnblogs.com/wengyuli/archive/2010/11/22/wcf-tcp-host-to-iis.html 摘要 在项目开发中,我们可能会适时的选 ...

  8. 使用Jekyll搭建免费的Github Pages个人博客

    一.Git 1.Git概述 Git is a free and open source distributed version control system designed to handle ev ...

  9. 百度地图点聚合MarkerClusterer,性能优化

    参考文献:http://www.cnblogs.com/lightnull/p/6184867.html 百度的点聚合算法 是基于方格和距离的聚合算法,即开始的时候地图上没有任何已知的聚合点,然后遍历 ...

  10. 机器学习实战__安装python环境

    环境:win7 64位系统 第一步:安装python 1.下载python2.7.3 64位 msi 版本(这里选择了很多2.7的其他更高版本导致安装setuptools失败,也不知道是什么原因,暂时 ...