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

 
Google: có 200 triệu thiết bị Android đang hoạt động trên toàn thế giới

Google: có 200 triệu thiết bị Android đang hoạt động trên toàn thế giới

Gã khổng lồ đã tự hào tuyên bố điều đó trong buổi ra mắt dịch vụ âm nhạc Google Music hôm 16/11 vừa qua.

iOS-Android lặp lại cuộc chiến Mac-PC?

iOS-Android lặp lại cuộc chiến Mac-PC?

Smartphone và máy tính bảng đang dần trở thành máy tính cá nhân của thời đại ngày nay. Hai nhân vật chính đang chiếm lĩnh thị trường là iOS của Apple và Android của Google.

thu tinh trong ong nghiem

Mang thai sau khi thụ tinh trong ống nghiệm/thụ tinh nhân tạo: điều ba mẹ cần biết.

Hiện nay có 2 phương pháp điều trị vô sinh chính được áp dụng nhiều nhất tại Việt Nam.

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

 

Diet con trung