最近在項目中遇到需求 須要按照必定的模板導出數據
仍是直接上代碼 這裏貼一部分模板長什麼樣吧
前端
而後就是代碼
大體就是找到模板
複製一份臨時文件
而後修改臨時文件而後導出數據app
代碼以下code
string appRoot = AppContext.BaseDirectory; // 獲取模板可是不能修改 string fileName = Path.Combine(appRoot, "customerTemplate.xlsx"); FileInfo file = new FileInfo(fileName); // 創造臨時文件用於修改編輯,用模板的文件代替輸出的文件 string temporaryName = Path.Combine(appRoot, "customer.xlsx"); //FileInfo tempoaryPrev = new FileInfo(temporaryName); FileInfo temporaryExcel = file.CopyTo(temporaryName, true);
這裏是生成文件內容到代碼
這裏一次就只是生成一個文件
很簡單的方法 可是不知足批量導出blog
using (ExcelPackage package = new ExcelPackage(temporaryExcel)){ ExcelWorksheet ws = package.Workbook.Worksheets[0]; //第一張表 ws.Cells["C3"].Value = cmCust.Customername;//表明C3這個單元格 ws.Cells["F3"].Value = cmCust.Customercode; ws.Cells["C4"].Value = salse; ws.Cells["F4"].Value = cmCust.Divisionname;//下面的也不一一貼出來了 稍微看一下就懂了 package.Save();//保存 })
生成文件返回帶前端 前端直接下載就能夠了string
var stream = File(fs, "application/octet-stream", $"customer{DateTime.Now.ToString("yyyyMMddHHmmss")}.xlsx");io
大體的方法就只有這麼一點
具體的本身看着代碼寫個方法就好了
有什麼寫的不對的地方麻煩各位大佬指出模板
這是導出後的樣子
stream