C# 导出word文档及批量导出word文档(3)
在初始化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");
}
getWordInfo里其它的方法类似这两个。
最后导出的word文档大致如下图所示:
C# 导出word文档及批量导出word文档(3)的更多相关文章
- C# 导出word文档及批量导出word文档(4)
接下来是批量导出word文档和批量打印word文件,批量导出word文档和批量打印word文件的思路差不多,只是批量打印不用打包压缩文件,而是把所有文件合成一个word,然后通过js来调用 ...
- C# 导出word文档及批量导出word文档(1)
这里用到了两个dll,一个是aspose.word.dll,另外一个是ICSharpCode.SharpZipLib.dll,ICSharpCode.SharpZipLib.dll是用于批量 ...
- 吴裕雄--天生自然python学习笔记:python文档操作批量替换 Word 文件中的文字
我们经常会遇到在不同的 Word 文件中的需要做相同的文字替换,若是一个一个 文件操作,会花费大量时间 . 本节案例可以找出指定目录中的所有 Word 文件(包含 子目录),并对每一个文件进行指定的文 ...
- C# 导出word文档及批量导出word文档(2)
aspose.word主要是通过把读取出来的数据放到datatable里,在datable里做相应的格式的调整,再导出到word文档里.mvc和webform最后导出的语句略有不同,在mvc的cont ...
- ATF批量导出工具
ATF批量导出工具 08 / 31, 2013 批量导出Atf的工具,使用是adobe atf 编码核心 先说一下关于atf的bug 当atf导出时候启用了mips选项会导致:如果纹理问长方形时上传会 ...
- C#操作Word的+ CKEditor 輸出成Word文件(包含圖案上傳)
C#操作Word 参考博文: C#操作word类文件 https://www.cnblogs.com/walking/p/3571068.html C#中的Office操作专栏(21) http:// ...
- 批量导出access某表内容到word文档
一.需求: 需要将表中每一条记录中的某些内容导出在一个word文档中,并将这些文档保存在指定文件夹目录下 二.界面,简单设计如下: 三.添加office相关引用 添加后可在解决方案资源管理器中看到: ...
- c#操作word文档之简历导出
前言 1.写这个功能之前,我得说说微软的这个类库,用着真苦逼!是他让我有程序猿,攻城尸的感觉了.首先这个类库,从没接触过,方法与属性都不懂,还没有提示.神啊,我做这功能真是一步一卡,很潇洒啊. 2.这 ...
- java使用freemarker模板导出word(带有合并单元格)文档
来自:https://blog.csdn.net/qq_33195578/article/details/73790283 前言:最近要做一个导出word功能,其实网上有很多的例子,但是我需要的是合并 ...
随机推荐
- UVA - 12627 Erratic Expansion 奇怪的气球膨胀 (分治)
紫书例题p245 Piotr found a magical box in heaven. Its magic power is that if you place any red balloon i ...
- 分布式配置管理平台 - Disconf介绍
原博客地址:http://blog.csdn.net/zhu_tianwei/article/details/47984545 Disconf专注于各种分布式系统配置管理的通用组件/通用平台,提供统一 ...
- Solr4.8.0源码分析(16)之SolrCloud索引深入(3)
Solr4.8.0源码分析(16)之SolrCloud索引深入(3) 前面两节学习了SolrCloud索引过程以及索引链的前两步,LogUpdateProcessorFactory和Distribut ...
- C++学习笔记--Season 2
一个简单的EGE程序: #include "graphics.h" //EGE库的头文件 int main(int argc, char** argv) { initgraph(, ...
- 【最大流】XMU 1595 机器调度
题目链接: http://acm.xmu.edu.cn/JudgeOnline/problem.php?id=1595 题目大意: T组数据,n个任务,m个机器,对于每个任务:有一个处理时间p(表示这 ...
- POJ1163——The Triangle
Description 73 88 1 02 7 4 44 5 2 6 5 (Figure 1) Figure 1 shows a number triangle. Write a program t ...
- #include <sys/stat.h>的作用
#include <sys/stat.h> 文件状态, 是unix/linux系统定义文件状态所在的伪标准头文件. 含有类型与函数: dev_t st_dev Device ...
- java排序算法-归并排序
public class MergeSort { private static void mergeSortTest() { int[] in = { 2, 5, 3, 8, 6, 7, 1, 4, ...
- Tomcat部署问题及解决方法
1.gradle构建web时报错:Access restriction: The type JPEGImageEncoder is not accessible due to restriction ...
- 转:jQuery LigerUI 使用教程表格篇(3) 复选框、多表头、分组、汇总和明细
阅读目录 复选框 多表头 分组 汇总 明细 复选框 grid可以设置复选框模式进行多选,只需要简单的配置 checked:true 获取选中行 如果要获取选中的行,可以用getSelecteds方法: ...