国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

C# 導出word文檔及批量導出word文檔(3)

2019-11-17 02:17:55
字體:
來源:轉載
供稿:網友

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文檔大致如下圖所示:

圖片

圖片


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 庆安县| 卢湾区| 察哈| 教育| 肇源县| 黔西| 贵阳市| 都匀市| 新昌县| 盱眙县| 安图县| 茶陵县| 江川县| 团风县| 于都县| 麦盖提县| 西充县| 辽宁省| 雅江县| 揭西县| 临漳县| 牡丹江市| 梅州市| 江北区| 定陶县| 昭通市| 简阳市| 施秉县| 红原县| 会昌县| 喀喇| 信阳市| 常熟市| 得荣县| 乌拉特后旗| 石家庄市| 确山县| 仙居县| 山东省| 卢湾区| 芮城县|