Click here to Skip to main content
15,886,787 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a code for exporting multiple dataset to multiple sheets of single excel file. But I want for eg. I have 2 datasets and on button click I want to export first dataset value in test1.xlsx and 2nd dataset to test2.xlsx

What I have tried:

Tried to creat two instances of XLWorkbook wb = new XLWorkbook() on button click but still not working getting single file only
Posted
Updated 14-Apr-16 2:00am
Comments
Michael_Davies 14-Apr-16 6:23am    
Please show your code, when you say create two instances are they immediately after each other or are you using a new variable for the second book?
Member 10678104 14-Apr-16 6:29am    
public static void Exportds2Multiplesheets(DataSet ds, string Filename)
{
if (ds.Tables.Count == 0)
throw new ArgumentException("DataSet needs to have at least one DataTable", "dataset");
HttpResponse Response = System.Web.HttpContext.Current.Response;
string Fname = Filename + ".xlsx";
using (XLWorkbook wb = new XLWorkbook())
{
wb.Worksheets.Add(ds);
wb.Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
wb.Style.Font.Bold = true;

Response.Clear();
Response.Buffer = true;
Response.Charset = "";
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.AddHeader("content-disposition", "attachment;filename=" + Fname);

using (MemoryStream MyMemoryStream = new MemoryStream())
{
wb.SaveAs(MyMemoryStream);
MyMemoryStream.WriteTo(Response.OutputStream);

Response.Flush();
Response.End();
}
}
using (XLWorkbook wb1 = new XLWorkbook())
{
wb1.Worksheets.Add(ds);
wb1.Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
wb1.Style.Font.Bold = true;

Response.Clear();
Response.Buffer = true;
Response.Charset = "";
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.AddHeader("content-disposition", "attachment;filename=" + Fname);

using (MemoryStream MyMemoryStream = new MemoryStream())
{
wb1.SaveAs(MyMemoryStream);
MyMemoryStream.WriteTo(Response.OutputStream);

Response.Flush();
Response.End();
}
}
}
Member 10678104 14-Apr-16 6:29am    
Tried this code. but on button click it opens only on file
Moleter 17-May-16 8:43am    

1 solution

You're just writing a file to the response buffer, clearing the buffer, and re-writing the same thing into the response buffer. You need multiple sheets in one file, so you need to pass all the datasets that you want into the function and add them all as individual sheets.

C#
public static void Exportds2Multiplesheets(IEnumerable<dataset> ds, string Filename)
{
   if (ds == null || ds.Tables.Count == 0) //possible null case handled
   {
      throw new ArgumentException("DataSet needs to have at least one DataTable", 
      "dataset");
   }

   HttpResponse Response = System.Web.HttpContext.Current.Response;
   string Fname = Filename + ".xlsx";

   using (XLWorkbook wb = new XLWorkbook())
   {
      foreach(var set in ds) // Add a sheet per data set
      {
         wb.Worksheets.Add(ds);
      }

      wb.Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
      wb.Style.Font.Bold = true;

      Response.Clear();
      Response.Buffer = true;
      Response.Charset = "";
      Response.ContentType = 
         "application/vnd.openxmlformats-   officedocument.spreadsheetml.sheet";
      Response.AddHeader("content-disposition", "attachment;filename=" + Fname);

      using (MemoryStream MyMemoryStream = new MemoryStream())
      {
         wb.SaveAs(MyMemoryStream);
         MyMemoryStream.WriteTo(Response.OutputStream);

         Response.Flush();
         Response.End();
      }
   }
}
</dataset>
 
Share this answer
 
Comments
Member 10678104 14-Apr-16 8:18am    
@Nathan Minier. I have already implemented this. multiple dataset in multiple sheet of same excel file. But I want multiple dataset to be export in "Multiple Excel file" not sheets of single excel
Nathan Minier 14-Apr-16 8:22am    
Then you'll need to zip them and add the zip to the Response, you can't just add a bunch of files to the response stream.
Nathan Minier 14-Apr-16 8:27am    
Here's an SO thread about how you can assemble a zip without writing to disk.
http://stackoverflow.com/questions/17232414/creating-a-zip-archive-in-memory-using-system-io-compression
Member 10678104 18-Apr-16 9:09am    
Can anyone has another approach for this??
Nathan Minier 18-Apr-16 9:24am    
If you don't want to add sheets, your only route is sending it as a zip. HTTP is not designed to push multiple files as a download.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900