.net core 替换word、excel模板内容并导出
需要在NuGet搜索DotNetCore.NPOI并安装;
word替换方式
1 public async Task<FileResult> DownLoadFile() 2 { 3 var entity = "";//这里换成自己的实体 4 string webRootPath = _hostingEnvironment.WebRootPath; 5 var FullSourcePath = webRootPath + TemplatePath; 6 FileStream fileStream = new FileStream(FullSourcePath, FileMode.Open, FileAccess.Read); 7 XWPFDocument myDoc = new XWPFDocument(fileStream); 8 //遍历段落,替换内容 9 foreach (var para in myDoc.Paragraphs) 10 { 11 ReplaceKey(entity, para); 12 } 13 14 // 遍历table,替换单元格内容 15 foreach (var table in myDoc.Tables) 16 { 17 foreach (var row in table.Rows) 18 { 19 foreach (var cell in row.GetTableCells()) 20 { 21 foreach (var para in cell.Paragraphs) 22 { 23 ReplaceKey(entity, para); 24 } 25 } 26 } 27 } 28 byte[] buffer = new byte[1024 * 5]; 29 using (MemoryStream ms = new MemoryStream()) 30 { 31 myDoc.Write(ms); 32 buffer = ms.GetBuffer(); 33 ms.Close(); 34 } 35 return File(buffer, "application/msword", "文件名.docx"); 36 } 37 38 39 private void ReplaceKey<T>(T etity, XWPFParagraph para) 40 { 41 Type entityType = typeof(T); 42 PropertyInfo[] properties = entityType.GetProperties(); 43 string entityName = entityType.Name;//实体类名称 44 string paratext = para.ParagraphText; 45 var runs = para.Runs; 46 string styleid = para.Style; 47 string text = ""; 48 foreach (var run in runs) 49 { 50 text = run.ToString(); 51 foreach (var p in properties) 52 { 53 string propteryName = "{" + p.Name + "}";//Word模板中设定的需要替换的标签 54 object value = p.GetValue(etity); 55 if (value == null) 56 { 57 value = ""; 58 } 59 if (text.Contains(propteryName)) 60 { 61 var v = value.ToString(); 62 63 text = text.Replace(propteryName, v); 64 } 65 run.SetText(text); 66 } 67 } 68 }
excel替换方式
1 public async Task<FileResult> DownLoadFile() 2 { 3 var entity = "";//实体 4 string webRootPath = _hostingEnvironment.WebRootPath; 5 var FullSourcePath = webRootPath + TemplatePath; 6 FileStream fs = new FileStream(FullSourcePath, FileMode.Open, FileAccess.Read); 7 IWorkbook workbook = WorkbookFactory.Create(fs); 8 ISheet sheet = workbook.GetSheetAt(0); 9 IRow firstRow = sheet.GetRow(0); 10 int cellCount = firstRow.LastCellNum; 11 int rowCount = sheet.LastRowNum; 12 for (int i = 0; i < rowCount; i++) 13 { 14 IRow row = sheet.GetRow(i); 15 for (int j = 0; j < cellCount; j++) 16 { 17 NPOI.SS.UserModel.ICell cell = row.GetCell(j); 18 ReplaceExcelKey(entity, cell); 19 } 20 } 21 byte[] buffer = new byte[1024 * 5]; 22 using (MemoryStream ms = new MemoryStream()) 23 { 24 workbook.Write(ms); 25 buffer = ms.GetBuffer(); 26 ms.Close(); 27 } 28 return File(buffer, "application/ms-excel", "文件名.xlsx"); 29 } 30 31 32 private void ReplaceExcelKey<T>(T etity, NPOI.SS.UserModel.ICell cell) 33 { 34 Type entityType = typeof(T); 35 PropertyInfo[] properties = entityType.GetProperties(); 36 if (cell != null) 37 { 38 var text = ""; 39 if (cell.CellType == CellType.String)//这里根据不同的类型进行不同的处理 40 { 41 text = cell.ToString(); 42 } 43 if (text != "") 44 { 45 foreach (var p in properties) 46 { 47 string propteryName = "{" + p.Name + "}"; 48 object value = p.GetValue(etity); 49 if (value == null) 50 { 51 value = ""; 52 } 53 if (text.Contains(propteryName)) 54 { 55 var v = value.ToString(); 56 text = text.Replace(propteryName, v); 57 } 58 cell.SetCellValue(text); 59 } 60 } 61 62 } 63 }

浙公网安备 33010602011771号