在初始化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. DNS解析原理

    1.在浏览器中输入www.qq.com域名,操作系统会先检查自己本地的hosts文件是否有这个网址映射关系,如果有,就先调用这个IP地址映射,完成域名解析. 2.如果hosts里没有这个域名的映射,则 ...

  2. laravel框架——上传、下载文件

    文件上传 在config文件夹下新建一个 项目名.php return [ 'title' => 'My Test', 'posts_per_page' => 5, 'uploads' = ...

  3. 初窥struts2(二)OGNL表达式

    Struts2总结 Struts2完整的处理流程: 1  客户端发送请求,交给struts2控制器(StrutsPrepareAndExecuteFilter). 2  Filter控制器进行请求过滤 ...

  4. 信号量 sem_t 进程同步

    sem_t分为有名和无名.有名的sem_t通过sem_open来创建, 而无名的sem_t通过sem_init的初始化. 用有名的sem_t来进程间同步是件很容易的事情,百度上一搜很多想相关的例子. ...

  5. GitHub NetFlow

    https://github.com/search?l=Java&p=1&q=netflow&ref=searchresults&type=Repositories&a ...

  6. org.apache.struts.chain.commands.InvalidPathException: No action config found for the specified url.

    No action config found for the specified url url路径下找不到action,原因是stuts-config.xml文件配置错误. demo的项目文件如下: ...

  7. HTTP meta 设置方法

    网页的缓存是由 HTTP 消息头中的 “Cache-control” 来控制的,常见的取值有 private.no-cache.max-age.must-revalidate 等,默认为private ...

  8. java中基于TaskEngine类封装实现定时任务

    主要包括如下几个类: 文章标题:java中基于TaskEngine类封装实现定时任务 文章地址: http://blog.csdn.net/5iasp/article/details/10950529 ...

  9. Oracle&#39;s Business Intelligence Applications Configuration Manager 基本概念

    Oracle's Business Intelligence Applications Configuration Manager :BIACM Once the BIAPPS installatio ...

  10. shell 脚本实现的守护进程

    转自:http://blog.csdn.net/cybertan/article/details/3235722 转自:http://blog.sina.com.cn/s/blog_4c451e0e0 ...