Solved

Export Dataset to Excel xlsx

Posted on 2016-09-20
8
153 Views
Last Modified: 2016-09-26
Hello Experts,
 I have a Dataset contains few Datatables

How do I Export this Dataset to Excel xlsx Files, each DataTable will be exported to a sheet in the workbook

Thanks
0
Comment
Question by:Jamil Muammar
8 Comments
 
LVL 32

Expert Comment

by:Rob Henson
ID: 41806885
Which software is the Export being done from?
What format does the export create?
0
 

Author Comment

by:Jamil Muammar
ID: 41806895
in ASP.NET C#

the export creates an excel file type xlsx, each sheet in the excel will contain on Datatable from DataSet

Thanks
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 41806906
I see ASP.Net in your topics. Do you want to run that from a web server?

If you can afford it, Aspose.Cells would be a great tool: http://www.aspose.com/products/cells/net
0
ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

 
LVL 2

Expert Comment

by:Kyaw Wanna
ID: 41806914
If you write in c#, please refer the example as per below :
   dt = datatable.GetAllData();//your datatable
    string attachment = "attachment; filename=excelfile.xls";
    Response.ClearContent();
    Response.AddHeader("content-disposition", attachment);
    Response.ContentType = "application/vnd.ms-excel";
    string tab = "";
    foreach (DataColumn dc in dt.Columns)
    {
        Response.Write(tab + dc.ColumnName);
        tab = "\t";
    }
    Response.Write("\n");
    int i;
    foreach (DataRow dr in dt.Rows)
    {
        tab = "";
        for (i = 0; i < dt.Columns.Count; i++)
        {
            Response.Write(tab + dr[i].ToString());
            tab = "\t";
        }
        Response.Write("\n");
    }
    Response.End();

Open in new window

0
 
LVL 28

Expert Comment

by:Pawan Kumar
ID: 41807036
or may be you can try

--


using (SqlCommand cmd = new SqlCommand(query))
        {
            using (SqlDataAdapter dataadapter = new SqlDataAdapter())
            {
                cmd.Connection = con;
                dataadapter.SelectCommand = cmd;
                using (DataSet ds = new DataSet())
                {
                    dataadapter.Fill(ds);
                    ds.Tables[0].TableName = "A";
                    ds.Tables[1].TableName = "B";
					ds.Tables[2].TableName = "C"; -- example
 
                    using (XLWorkbook wb = new XLWorkbook())
                    {
                        foreach (DataTable dt in ds.Tables)
                        {                            
                            wb.Worksheets.Add(dt);
                        } 
                        
                        Response.Clear();
                        Response.Buffer = true;
                        Response.Charset = "";
                        Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
                        Response.AddHeader("content-disposition", "attachment;filename=ExportFileName.xlsx");
                        using (MemoryStream PawanMemoryStream = new MemoryStream())
                        {
                            wb.SaveAs(PawanMemoryStream);
                            PawanMemoryStream.WriteTo(Response.OutputStream);
                            Response.Flush();
                            Response.End();
                        }
                    }
                }
            }
        }


--

Open in new window

--
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 41807148
where is your XLWorkbook coming from?
0
 

Author Comment

by:Jamil Muammar
ID: 41808026
user should download it
0
 
LVL 70

Accepted Solution

by:
Éric Moreau earned 500 total points
ID: 41808431
the question about XLWorkbook was for Pawan Kumar Khowal. It is in its code and it is not a .Net intrinsic object.
0

Featured Post

ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question