C# Export DataTable, DataSet ไปเป็น excel

แนะนำ สอบถาม ภาษา C สำหรับผู้เริ่มต้น ภาษา Java ภาษา Python

Moderator: mindphp, ผู้ดูแลกระดาน

ภาพประจำตัวสมาชิก
jataz2
PHP Super Member
PHP Super Member
โพสต์: 275
ลงทะเบียนเมื่อ: 22/02/2011 11:48 am

C# Export DataTable, DataSet ไปเป็น excel

โพสต์ที่ยังไม่ได้อ่าน โดย jataz2 »

โค้ด: เลือกทั้งหมด

using System.Text;

protected void imgExportToExcel_Click(object sender, ImageClickEventArgs e)
    {
        DataTable dtlocal = null;  // DataTable dtlocal = ds.Tables[0];

        if (Session["fdt"] != null)
            dtlocal = (DataTable)Session["fdt"];

        if (dtlocal != null)
        {
            if (dtlocal.Rows.Count > 0)
            {
                string attachment = "attachment; filename=[color=#FF0000]YourFileName.xls[/color]";
                Response.ClearContent();
                Response.AddHeader("content-disposition", attachment);
                Response.ContentEncoding = Encoding.Default;
                Response.ContentType = "application/vnd.ms-excel";
                string tab = "";
                foreach (DataColumn dc in dtlocal.Columns)
                {
                    Response.Write(tab + dc.ColumnName);
                    tab = "\t";
                }
                Response.Write("\n");

                int i;
                foreach (DataRow dr in dtlocal.Rows)
                {
                    tab = "";
                    for (i = 0; i < dtlocal.Columns.Count; i++)
                    {
                        Response.Write(tab + dr[i].ToString());
                        tab = "\t";
                    }
                    Response.Write("\n");
                }

                Response.End();
            }
        }
    }
  • Similar Topics
    ตอบกลับ
    แสดง
    โพสต์ล่าสุด

ผู้ใช้งานขณะนี้

สมาชิกกำลังดูบอร์ดนี้: ไม่มีสมาชิกใหม่ และบุคลทั่วไป 117