.NET Core MVC 批量压缩下载文件
Controller :
public async Task<IActionResult> WordDownLoad(string ids) { var taskserviceR = App.GetService<MyTaskService>(); var taskR = App.GetService<IRepository<MyTaskEntity>>(); var idsarr = ids.ToList<Guid>(","); if (idsarr.Count == 1) { var entitydto = taskR.Entities.Find(idsarr[0]); var result = await taskserviceR.ExportWordFileByTemplate(ids); return File(result, "application/ms-word", entitydto.EmpCode+".docx");//文件流方式,指定文件流对应的ContenType; } else { var zipname = DateTime.Now.ToString("yyyyMMddHHmmss") + ".zip"; var result = await taskserviceR.ExportWordFileByTemplatezip(ids); return File(result, "application/zip", zipname);//文件流方式,指定文件流对应的ContenType; } }
Service:
程序集 Magicodes.IE.Word
System.IO.Compression.ZipFile 类
/// <summary> /// 单个文件下载 /// </summary> /// <param name="id"></param> /// <returns></returns> public async Task<byte[]> ExportWordFileByTemplate(string id) { var keyid = new Guid(id); var query = _repository.Entities.AsQueryable(); var entitydto = _repository.Entities.Find(keyid); var empcode = entitydto.EmpCode; var tplPath = Path.Combine(Directory.GetCurrentDirectory(), "WordExport", "WordTemplate", "receipt.cshtml"); var tpl = File.ReadAllText(tplPath); var exporter = new WordExporter(); var result = await exporter.ExportBytesByTemplate( new ReceiptInfo { Grade = entitydto.CostCenter, IdNo = entitydto.IDCard, Name = entitydto.UserName, TradeTime = DateTime.Now, Code = entitydto.EmpCode }, tpl, typeof(ReceiptInfo)); //此处使用默认模板导出 return result; } /// <summary> /// 多个文件批量下载,已压缩包的方式返回 /// </summary> /// <param name="ids"></param> /// <returns></returns> public async Task<byte[]> ExportWordFileByTemplatezip(string ids) { var empuser = App.User.GetValue("EmployeeCode"); var query = _repository.Entities; var Guids = ids.ToList<Guid>(","); List<byte[]> list = new List<byte[]>(); var tplPath = Path.Combine(Directory.GetCurrentDirectory(), "WordExport", "WordTemplate", "receipt.cshtml"); var tpl = File.ReadAllText(tplPath); var docxpath = Path.Combine(Directory.GetCurrentDirectory(), "WordExport", "File", empuser + "_docx"); var zipdirectoryPath = Path.Combine(Directory.GetCurrentDirectory(), "WordExport", "File", empuser + "_zip"); if (false == Directory.Exists(docxpath)) { //创建文件夹 Directory.CreateDirectory(docxpath); } DeleteDirAllFile(docxpath); if (false == Directory.Exists(zipdirectoryPath)) { //创建文件夹 Directory.CreateDirectory(zipdirectoryPath); } DeleteDirAllFile(zipdirectoryPath); foreach (var id in Guids) { var entitydto = query.Find(id); var filename = entitydto.UserName; var empcode = entitydto.EmpCode; var filePath = Path.Combine(docxpath, filename + empcode + ".docx"); var exporter = new WordExporter(); var result = await exporter.ExportBytesByTemplate( new ReceiptInfo { Grade = entitydto.CostCenter, IdNo = entitydto.IDCard, Name = entitydto.UserName, TradeTime = DateTime.Now, Code = entitydto.EmpCode }, tpl, typeof(ReceiptInfo)); using (var file = File.OpenWrite(filePath)) { file.Write(result, 0, result.Length); } File.Exists(filePath); } var zipname = DateTime.Now.ToString("yyyyMMddHHmmss")+".zip"; ZipFile.CreateFromDirectory(docxpath,zipdirectoryPath+"/"+zipname); using (FileStream fs = new FileStream(zipdirectoryPath + "/" + zipname, FileMode.Open, FileAccess.Read)) { byte[] byteArray = new byte[fs.Length]; fs.Read(byteArray, 0, byteArray.Length); fs.Close(); if (Directory.Exists(docxpath)) { Directory.Delete(docxpath, true); } if (Directory.Exists(zipdirectoryPath)) { Directory.Delete(zipdirectoryPath, true); } return byteArray; } }
模板:
<!DOCTYPE html> <html lang="en" xmlns="http://www.w3.org/1999/xhtml"> <head> <meta charset="utf-8" /> <title></title> <style type="text/css"> body { font-family: SimSun !important; } p { margin: 0px; } footer { color: #333; font-size: 1.2rem; margin-bottom: 1.5rem; margin-right: 5%; margin-top: 1.2rem; text-align: right; } table, td { border: 1px solid #444; border-collapse: collapse; /* text-align: center; */ height: 2rem; padding: 5px; } </style> </head> <body> <p style="color: #000; font-size: 1.8rem; height: 32px; text-align: center;"> @Model.Title </p> <p style="color: #333; font-size: 1.2rem; margin-left: 5%; margin-top: 1%;"> <text>NO:@Model.Data.Code</text> <text style="padding-left: 52%; text-align: right;">交易时间:@Model.Data.TradeTime.ToString("yyyy-MM-dd HH:mm:ss")</text> </p> <table width="90%" style="margin-left: 5%; margin-top: 1%;"> <tr style="font-size: 1rem;"> <td style="text-align: center; width: 11%;">交款姓名</td> <td colspan="2">@Model.Data.Name</td> <td style="text-align: center; width: 13%;">身份证号码</td> <td colspan="3">@Model.Data.IdNo</td> </tr> <tr style="font-size: 1rem;"> <td style="text-align: center">交易金额</td> <td colspan="6"> <span>¥:@Model.Data.Amount</span> <span style="padding: 0 2% 0 2%;">人民币(大写):</span> <span>@Model.Data.UppercaseAmount</span> </td> </tr> <tr style="font-size: 1rem;"> <td style="text-align: center">收款方式</td> <td colspan="2">@Model.Data.PaymentMethod</td> <td style="text-align: center; width: 13%;">交易状态</td> <td colspan="3">@Model.Data.TradeStatus</td> </tr> <tr style="font-size: 1rem;"> <td style="text-align: center">收款事由</td> <td style="width: 22.3%;">@Model.Data.Remark</td> <td style="text-align: center; width: 11%;">入学年级</td> <td style="width: 22.3%;" colspan="2">@Model.Data.Grade</td> <td style="text-align: center; width: 11%;">专业</td> <td style="width: 22.3%;">@Model.Data.Profession</td> </tr> </table> </body> </html>