var bytes = _service.MergeExcelWorksheets(); if (bytes == null) return null; var fileStream = new MemoryStream(bytes); var contentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; var fileDownloadName = string.Format("test{0}.xlsx", DateTime.Now.ToString("yyyyMMdd")); fileStream.Position = 0; var fsr = new FileStreamResult(fileStream, contentType); fsr.FileDownloadName = fileDownloadName;
package1.Save();
private byte[] MergeExcelWorksheets(byte[] array1, byte[] array2) { using (MemoryStream ms1 = new MemoryStream(array1)) using (MemoryStream ms2 = new MemoryStream(array2)) using (ExcelPackage package1 = new ExcelPackage(ms1)) using (ExcelPackage package2 = new ExcelPackage(ms2)) { var workbook1 = package1.Workbook; var workbook2 = package2.Workbook; // Iterate through sheets in workbook2 foreach (var sourceSheet in workbook2.Worksheets) { if (!workbook1.Worksheets.Any(sheet => sheet.Name == sourceSheet.Name)) { // Clone the sheet and add it to workbook1 var clonedSheet = workbook1.Worksheets.Add(sourceSheet.Name, sourceSheet); } } return ms1.ToArray(); } }