asp.net 根據html模板導出excel public class ExcelHelper { /// <summary> /// 根據html模板文件生成excel文件 /// </summary> /// <param name="ds">數據源</param> /// <param na ...
asp.net 根據html模板導出excel
public class ExcelHelper { /// <summary> /// 根據html模板文件生成excel文件 /// </summary> /// <param name="ds">數據源</param> /// <param name="TemplateFilePath">html模板文件路徑(虛擬路徑而非物理路徑)</param> /// <param name="ToFileName">生成的excel文件名,帶尾碼不帶路徑</param> /// <returns></returns> public static string GetExcel(DataSet ds, string TemplateFilePath, string ToFileName) { string ToFilePath = SysHelper.GetVirtualPath() + "upload/export_excel/"; string strOutPath = HttpContext.Current.Server.MapPath(ToFilePath); if (!Directory.Exists(strOutPath)) { Directory.CreateDirectory(strOutPath); } string TemplateContent = GetStringFromTemplate(ds, TemplateFilePath); //保存文件 using (FileStream fs = new FileStream(strOutPath + ToFileName, FileMode.Create)) { using (StreamWriter sw = new StreamWriter(fs)) { sw.Write(TemplateContent); } } return ToFilePath + ToFileName; } /// <summary> /// 根據模板生成字元換 /// </summary> /// <param name="ds"></param> /// <param name="TemplateFilePath"></param> /// <returns></returns> private static string GetStringFromTemplate(DataSet ds, string TemplateFilePath) { //讀取模板 string TemplateContent = ""; using (StreamReader sr = new StreamReader(HttpContext.Current.Server.MapPath(TemplateFilePath))) { TemplateContent = sr.ReadToEnd(); } //解析每個表 for (int TableIndex = 0; TableIndex < ds.Tables.Count; TableIndex++) { DataTable dt = ds.Tables[TableIndex]; //獲取表的模板集合 string TableTag = string.Format(@"#table.{0}#", TableIndex);//表的標簽 string pattern = string.Format(@"#table\.{0}#.*?#table\.{0}#", TableIndex); Regex reg = new Regex(pattern, RegexOptions.Singleline); MatchCollection matchs = reg.Matches(TemplateContent); //解析每個模板 foreach (Match match in matchs) { string tableTemplate = match.Value; string table = "";//解析後內容 //解析每行數據 for (int i = 0; i < dt.Rows.Count; i++) { string rowTemplate = tableTemplate.Replace(TableTag, "");//去掉模板標簽 //解析行模板 for (int j = 0; j < dt.Columns.Count; j++) { string ColumnName = "#table." + TableIndex + "." + dt.Columns[j].ColumnName + "#"; rowTemplate = rowTemplate.Replace(ColumnName, dt.Rows[i][j].ToString()); } table += rowTemplate; } //解析完之後替換到模板 TemplateContent = TemplateContent.Replace(tableTemplate, table); } } return TemplateContent; } }
html模板格式
<!DOCTYPE html> <html> <head> <title></title> <meta http-equiv="content-type" content="text/html; charset=UTF-8" /> </head> <body> <table border="1"> <tr><td colspan="4" style="text-align:center;">事件彙總表</td></tr> <tr> <td style="width:200px;">事件名稱</td> <td style="width:500px;">事件簡要情況</td> <td style="width:100px;">發生時間</td> <td style="width:100px;">發生地區</td> </tr> #table.0# <tr> <td>#table.0.omtb_title#</td> <td>#table.0.omtb_content#</td> <td>#table.0.omtb_date#</td> <td>#table.0.omtb_address#</td> </tr> #table.0# </table> </body> </html>
調用方式
string templatepath = SysHelper.GetVirtualPath() + "zfb/pro_list_excel.html", outfile = "事件彙總表" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls"; string excel_file_path = ExcelHelper.GetExcel(dsSource, templatepath, outfile);
附加獲取應用程式虛擬目錄方法
public class SysHelper { public static string GetVirtualPath() { string path = HttpContext.Current.Request.ApplicationPath; return path + (path != "/" ? "/" : ""); //以/結尾 } }