Solved

Export Dataset to Excel xlsx

Posted on 2016-09-20
8
224 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 33

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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
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…
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

749 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