日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > C# >内容正文

C#

c# excel导出png_c#根据html模板导出excel

發布時間:2025/4/16 C# 91 豆豆
生活随笔 收集整理的這篇文章主要介紹了 c# excel导出png_c#根据html模板导出excel 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

ExcelHelper封裝

public class ExcelHelper{ /// /// 根據html模板文件生成excel文件 /// /// 數據源 /// html模板文件路徑(虛擬路徑而非物理路徑) /// 生成的excel文件名,帶后綴不帶路徑 /// 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; } /// /// 根據模板生成字符換 /// /// /// /// 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模板格式

事件匯總表 事件名稱 事件簡要情況 發生時間 發生地區 #table.0# #table.0.omtb_title# #table.0.omtb_content# #table.0.omtb_date# #table.0.omtb_address# #table.0#

調用方式

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 != "/" ? "/" : "");  //以/結尾 }}

總結

以上是生活随笔為你收集整理的c# excel导出png_c#根据html模板导出excel的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。