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

 
Apple sẽ sử dụng camera 3D lên các sản phẩm tiếp theo

Apple sẽ sử dụng camera 3D lên các sản phẩm tiếp theo

Apple có thể đang trong quá trình chuẩn bị để mang camera 3D lên các thiết bị tiếp theo của hãng.

Custom Android user interfaces: Time to show a little less ‘skin’

Custom Android user interfaces: Time to show a little less ‘skin’

Android Ice Cream Sandwich marks a new vision for Google’s mobile operating system. For the first time ever, it actually looks as if Google  had the consumer in mind and Android has never looked better.

Cấu hình Drupal settings file một cách chính xác

Cấu hình Drupal settings file một cách chính xác

First off, we remove the unneeded bits in the file. All of the comments and commented out code is removed. What we are left with is a short file

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

 

Diet con trung