Hi,
I have the requirement to create several Excel files, add the Excel files to a new zip file and return the zip file to the user.
I have been following https://help.syncfusion.com/file-formats/xlsio/faq#how-to-zip-files-using-the-syncfusioncompressionzip-namespace trying to accomplish my goal. The problem I am facing is that "ZipArchive" doesn't have the "AddFile" method as described in the doc. I tried using "AddItem" method but I don't know how to get the attributes. "File.GetAttributes()" doesn't exist.
Could you give me a sample in ASP.Net Core that creates multiple excel files and then adds the excel files to a new zip file? Below is some code I have been working on.
In my view I have the following javascript:
function runConRpt(Id) {
Id = parseInt(Id);
if (Id > 1) {
var popupobj = $("#customWaiting").data("ejWaitingPopup");
popupobj.show();
$.ajax({
type: "POST",
url: "/MyController/ConRpt",
data: {
"conRptId": Id},
success: function (data) {
var popupobj = $("#customWaiting").data("ejWaitingPopup");
popupobj.hide();
if (data.fileName != "") {
window.location.rel='nofollow' href = "@Url.RouteUrl(new { Controller = "MyController", Action = "DownloadConRpt" })/?file=" + data.fileName;
}
}
});
}
}
In my controller, I have:
public ActionResult ConRpt(int ConRptId)
{
ExcelEngine excelEngine = new ExcelEngine();
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Excel2016;
// create new workbook
IWorkbook workbook = application.Workbooks.Create();
//TODO: actually create the contact reports. I am only creating blank excel docs to see if I can add them to a zip file
// save excel docs to server
string fullPath = _hostingEnvironment.WebRootPath + @"\XlsIO\MyController\Download\test1.xlsx";
FileStream outputStream = new FileStream(fullPath, FileMode.Create);
workbook.SaveAs(outputStream);
string fullPath2 = _hostingEnvironment.WebRootPath + @"\XlsIO\MyController\Download\test2.xlsx";
FileStream outputStream2 = new FileStream(fullPath2, FileMode.Create);
workbook.SaveAs(outputStream);
workbook.Close();
outputStream.Close();
outputStream2.Close();
excelEngine.Dispose();
// now add the excel files to zip file
ZipArchive zipArchive = new ZipArchive();
zipArchive.DefaultCompressionLevel = Syncfusion.Compression.CompressionLevel.Best;
//FileAttributes attribute = File.GetAttributes(fullPath); //File.GetAttributes is not a method
//zipArchive.addFile(); // AddFile() is not a method
//zipArchive.AddItem(fullPath, outputStream, false, ) // don't know how to properly use this method
//zipArchive.Save(@"path_and_filename"); // the Save method that I have doesn't take the same parameters as the syncfusion documentation
//TODO: Delete the excel files
try
{
return Json(new { fileName = "zipfile.zip", errorMessage = "" });
}
catch
{
return View();
}
}
public ActionResult DownloadConRpt(string file)
{
string fullPath = _hostingEnvironment.WebRootPath + @"\XlsIO\MyController\Download\" + file;
byte[] fileByteArray = System.IO.File.ReadAllBytes(fullPath);
System.IO.File.Delete(fullPath);
return File(fileByteArray, "Application/msexcel", "test1.xlsx");
}