Cách export dữ liệu ra file excel trong asp.net

Cách export dữ liệu ra file excel trong asp.net

Có khá nhiều cách để export dữ liệu ra file excel. WEBXAULA sẽ đơn cử một phương pháp khá đơn giản (không cần phải chèn thêm thư viện bên ngoài). 

Cách export dữ liệu ra file excel trong asp.net

Ở đây, mình đưa ra một ví dụ là sẽ export một danh sách các sản phẩm trong dữ liệu ra file excel và lưu xuống máy.

string pSQL = "SELECT PRODUCTID, PRODUCTPRICE, PRODUCTNAME, PRODUCTSTATUS FROM PRODUCTS ORDER BY PRODUCTNAME";

        clsConnect pBang = new clsConnect(pSQL);

        string name_ = "tenfile"; //Tên file excel mà bạn lưu về máy

        //Tạo mới bảng để chép vào file excel

        Table tb = new Table();

        //Định dạng bảng

        tb.BorderColor = System.Drawing.Color.FromName("red");

        tb.CellPadding = 4;

        tb.GridLines = GridLines.Both;

        tb.CellSpacing = 0;

        tb.Width = Unit.Percentage(100);

        TableCell cell;

        TableRow row;

        int from = 0;

        int to = pBang.Rows.Count;

        int header = 0;

        for (int i = from; i < to - 1; i++)

        {

            DataRow dr = pBang.Rows[i];

            if (header == 0)

            {

                row = new TableRow();

                cell = new TableCell();

                //so thu tu

                cell.Height = 50;

                cell.BackColor = System.Drawing.Color.FromName("red");

                cell.Width = 50;

                cell.Text = "<b>Stt</b>";

                cell.HorizontalAlign = HorizontalAlign.Center;

                row.Cells.Add(cell);

                //Tên sản phẩm

                cell = new TableCell();

                cell.Height = 50;

                cell.Width = 140;

                cell.BackColor = System.Drawing.Color.FromName("red");

                cell.Text = "<b>Tên sản phẩm</b>";

                cell.HorizontalAlign = HorizontalAlign.Center;

                row.Cells.Add(cell);

                //Giá sản phẩm

                cell = new TableCell();

                cell.Height = 50;

                cell.BackColor = System.Drawing.Color.FromName("red");

                cell.Width = 500;

                cell.Text = "<b>Giá tiền</b>";

                cell.HorizontalAlign = HorizontalAlign.Center;

                row.Cells.Add(cell);

                tb.Rows.Add(row);

            }

            header++;

            row = new TableRow();

            cell = new TableCell();

            //stt

            cell.Height = 50;

            cell.Text = header.ToString();

            cell.HorizontalAlign = HorizontalAlign.Left;

            row.Cells.Add(cell);

            //TenSanPham

            cell = new TableCell();

            cell.Height = 50;

            cell.Text = dr["PRODUCTNAME"].ToString();

            cell.HorizontalAlign = HorizontalAlign.Left;

            row.Cells.Add(cell);

            //GiaSanPham

            cell = new TableCell();

            cell.Height = 50;

            cell.Text = dr["PRODUCTPRICE"].ToString().Trim();

            cell.HorizontalAlign = HorizontalAlign.Left;

            row.Cells.Add(cell);

            tb.Rows.Add(row);

        }

        //- Tương tự bạn có bao nhiêu trường cần export thì bạn chèn bấy nhiêu cột.

        //

        Response.Clear();

        Response.Buffer = true;

        //excel

        string ex_ = "xls";

        Context.Response.AddHeader("Content-Disposition", "attachment; filename=" + name_ + "." + ex_);

        //    Context.Response.AddHeader("Content-Length", strpath.Length.ToString());

        Response.ContentType = "application/vnd.ms-excel";

        Response.Charset = "UTF-8";

        this.EnableViewState = false;

        System.IO.StringWriter oStringWriter = new System.IO.StringWriter();

        System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);

        tb.RenderControl(oHtmlTextWriter);

        Response.Write(oStringWriter.ToString());

        Response.End();
Bạn thấy bài viết này như thế nào?: 
Average: 6.9 (15 votes)
Ảnh của Tommy Tran

Tommy owner Express Magazine

Drupal Developer having 9+ year experience, implementation and having strong knowledge of technical specifications, workflow development. Ability to perform effectively and efficiently in team and individually. Always enthusiastic and interseted to study new technologies

  • Skype ID: tthanhthuy

Advertisement

 

jobsora

Dich vu khu trung tphcm

Dich vu diet chuot tphcm

Dich vu diet con trung

Quảng Cáo Bài Viết

 
70 Best Free Google Android Apps(P4)

Những ứng dụng mua hàng trực tuyến từ android

Some months back we mentioned about 21 Best Google Android Applications, now today we present you 70 Free Google Android apps...

5 ứng dụng phải có khi dùng Facebook Messenger

5 ứng dụng phải có khi dùng Facebook Messenger

Con số những ứng dụng cho Messenger hiện đã lên tới hơn 40, hãy thử điểm qua đâu là 5 ứng dụng tốt nhất trong đó.

Google

Có phải mạng xã hội ngày càng tích cực và toàn cầu hóa ?

Theo một báo cáo của Sysomos thì những trao đổi trên các blog, diễn đàn và các loại hình truyền thông tin lớn đã tích cực tăng lên trong hai năm vừa qua ...

Công ty diệt chuột T&C

 

Diet con trung