分享
 
 
 

文档及批量导出word文档(3)

王朝学院·作者佚名  2016-05-20
窄屏简体版  字體: |||超大  

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

1 #region 获取文档的相对路径 2 public class WordFilePath 3 { 4 #region 返回文件带路径值 5 /// <summary> 6 /// 返回文件带路径值 7 /// </summary> 8 /// <param name="FilePth">文件所在文件夹名称</param> 9 /// <param name="FileName">文件名</param>10 /// <returns></returns>11 public static string FileWithFilePath(string FilePth, string FileName = null)12 {13 string strFile = "";14 if (!string.IsNullOrEmpty(FilePth))15 strFile += "~/" + FilePth + "/";16 if (!string.IsNullOrEmpty(FileName))17 strFile += FileName;18 19 return strFile;20 }21 #endregion22 23 #region 判断文件或文件夹是否存在24 /// <summary>25 /// 判断文件或文件夹是否存在26 /// </summary>27 /// <param name="FilePth">文件所在文件夹名称</param>28 /// <param name="FileName">文件名</param>29 /// <returns></returns>30 public static bool ExistFile(string FilePth, string FileName = null)31 {32 return System.IO.File.Exists(GetFilePath(FilePth, FileName));33 }34 #endregion35 36 #region 获取文件及文件夹的相对路径37 /// <summary>38 /// 获取文件及文件夹的相对路径39 /// </summary>40 /// <param name="FilePth"></param>41 /// <param name="FileName"></param>42 /// <returns></returns>43 public static string GetFilePath(string FilePth, string FileName = null)44 {45 string strPath = "";46 strPath = FileWithFilePath(FilePth, FileName);47 strPath = HttpContext.Current.Request.MapPath(strPath);48 return strPath;49 }50 #endregion51 }52 #endregion

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

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

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

1 #region 将所有信息拼凑成一个word文档 2 public void getWordInfo(WordHelper wordhelper, string tempFile, string ntid, string stype, string mid, string sequence) 3 { 4 if (tempFile.Contains("实习生学习记录表导出模板")) 5 { 6 Dictionary<string, string> dicWhere = new Dictionary<string, string>(); 7 dicWhere.Add("NTID", ntid); 8 wordhelper.GetBasicInfo(typeof(BLL.NewTraineeInfo), dicWhere); 9 PRejobTrainWord(wordhelper, ntid);10 TrainListWord(wordhelper, ntid, stype, mid);11 AwardListWord(wordhelper, ntid, stype, mid);12 ArrangeListWord(wordhelper, ntid, stype);13 ScoreListWord(wordhelper, ntid, stype, mid, sequence);14 EvalWord(wordhelper, ntid, stype, mid);15 }16 } 17 18 #region 其他信息19 //岗前培训20 public void PrejobTrainWord(WordHelper wordhelper, string ntid)21 {22 Dictionary<string, string> dicWhere = new Dictionary<string, string>();23 dicWhere.Add("NTID", ntid);24 wordhelper.GetBasicInfo(typeof(BLL.PrejobTraining), dicWhere);25 }26 27 //轮科安排表28 public void ArrangeListWord(WordHelper wordhelper, string ntid, string stype)29 {30 Dictionary<string, string> dicWhere = new Dictionary<string, string>();31 dicWhere = new Dictionary<string, string>();32 dicWhere.Add("NTID", ntid);33 dicWhere.Add("StudentType", stype.ToString());34 wordhelper.GetTableList(typeof(BLL.MajorCycle), dicWhere, "ArrangeList");35 }

View Code ArrangeList是之前在word模板里所设定的如下图,用for循环出列表, getWordInfo里其它的方法类似这两个。

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

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有