-2

i am new C# Beginner, I created a function to generate Excel using ClosedXML.Excel, but i also want to zip the excel fill, how to zip excel file using Sharpziplib? anybody could give an adivse?thanks

public FileResult Export()
{
    DataTable test = new DataTable("test");
    test.Columns.AddRange(new DataColumn[] {new DataColumn("Name")});

    foreach (var record in recordtest)
    {
         test.Rows.Add(record.name);
    }
 
    using (XLWorkbook ts = new XLWorkbook())
    {
          var testSheet = ts.Worksheets.Add(test);
          testSheet.Cell("D4").Value = "First Name";
          testSheet.Range("A4:A5").Merge();              
          testSheet.Range("A4:Q5").Columns().Style.Fill.BackgroundColor = 
     XLColor.Almond;
    }
    using (MemoryStream steam = new MemoryStream())
    {
           wb.SaveAs(steam);
           var fileName = String.Format("{0}-{1}.xlsx", "test", 
               DateTime.Now.ToString("yyyyMMdd"));
           return File(steam.ToArray(), "application/vnd.openxmlformats- 
            officedocument.spreadsheetml.sheet", fileName);
     }
 }
ericso
  • 109
  • 1
  • 9
  • 1
    The same way you'd zip anything else using sharpziplib. Please show what you have tried with regards to zipping. – ProgrammingLlama Sep 01 '21 at 02:01
  • 3
    FYI, excel files are already zipped, so zipping again won't save much space. – D Stanley Sep 01 '21 at 02:01
  • You might want to save your workbook to a stream inside the scope of the xlworkbook disposable – Burgito Sep 01 '21 at 02:10
  • Hi, thank all for the reply quickly.i have two excel file generate when i click excel export of the button, so i need to zip the two excel to the zip file, could you help me how to zip for excel file? thank a lot – ericso Sep 01 '21 at 02:16