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功能,其实网上有很多的例子,但是我需要的是合并 ...
随机推荐
- XCode7打包上传报错
在XCode7上传应用时,上传失败遇到两个错误,提示如下: ERROR ITMS-90535: "Unexpected CFBundleExecutable Key. The bundl ...
- c语言用封装来优化程序
一.基础研究 先对函数fa进行研究,代码如下: fa函数的参数为一个字符指针,他存储要输出的字符串.因为要显示在屏幕的中央位置,所以我们要把字符串放在段地址b800处.用strlen获取字符串的长度, ...
- didEndEditingRowAtIndexPath with nil indexPath
在UITableViewController中,通过滑动删除按钮删除一行,首先收到Table view data source call: tableView:commitEditingStyle:f ...
- BZOJ 1028 [JSOI2007]麻将
1028: [JSOI2007]麻将 Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 1270 Solved: 576[Submit][Status][ ...
- 【转】Android开发学习笔记:EditText的属性介绍
原文网址:http://liangruijun.blog.51cto.com/3061169/627350 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追 ...
- CentOS安装搜狗词库
中文输入使用ibus-pinyin. 在ibus-pinyin里使用搜狗词库 # wget http://hslinuxextra.googlecode.com/files/sougou-phrase ...
- HDOJ 2131 Probability
Problem Description Mickey is interested in probability recently. One day , he played a game which i ...
- Android Studio开发环境部署
Step0:背景,那些年-- Step1:安装JDK Step2:安装Android Studio 其他问题1:安装Android Studio之前没有先安装JDK 其他问题2:No JVM inst ...
- 查看Linux发行版的名称和版本号
Method #1: /etc/*-release file 在Terminal中执行命令: cat /etc/*-release 我的输出结果: DISTRIB_ID=Ubuntu DISTRIB_ ...
- linux补包
1.挂载文件export LANG=Cmkdir -p /media/cdrommount /dev/cdrom /media/cdrommount /dev/hdc /media/cdrommoun ...