Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,027

HOME > .NET Framework > Forum > การ Export datatable to excel c# ของ web developer 2010 ว่าทำยังไง



 

การ Export datatable to excel c# ของ web developer 2010 ว่าทำยังไง

 



Topic : 081113



โพสกระทู้ ( 5 )
บทความ ( 0 )



สถานะออฟไลน์




Code (C#)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MySql.Data.MySqlClient;
using System.Data;
using System.IO;


public partial class Main : System.Web.UI.Page
{
    MySqlConnection objConn;
    MySqlCommand objCmd;
    String strSQL;

    protected void Page_Load(object sender, EventArgs e)
    {
    String strConnString;
        strConnString = "Server=localhost;User Id=root; Password=root; Database=MyProject; Pooling=false";
        objConn = new MySqlConnection(strConnString);
        objConn.Open();

            BindData();
       
    }
    public void BindData()
    {
        strSQL = "SELECT * FROM student";

        MySqlDataReader dtReader;
        objCmd = new MySqlCommand(strSQL, objConn);
        dtReader = objCmd.ExecuteReader();

        myRepeater.DataSource = dtReader;
        myRepeater.DataBind();

        dtReader.Close();
        dtReader = null;
    }

    void Page_Unload()
    {
        objConn.Close();
        objConn = null;
    }


    protected void myRepeater_ItemDataBound(Object sender, RepeaterItemEventArgs e)
    {
        //StudentID
        Label lblStudentID = (Label)(e.Item.FindControl("lblStudentID"));
        if (lblStudentID != null)
        {
            lblStudentID.Text = (string)DataBinder.Eval(e.Item.DataItem, "StudentID");
        }

        //FirstName
        Label lblFirstName = (Label)(e.Item.FindControl("lblFirstName"));
        if (lblFirstName != null)
        {
            lblFirstName.Text = (string)DataBinder.Eval(e.Item.DataItem, "FirstName");
        }

        //LastName
        Label lblLastName = (Label)(e.Item.FindControl("lblLastName"));
        if (lblLastName != null)
        {
            lblLastName.Text = (string)DataBinder.Eval(e.Item.DataItem, "LastName");
        }

        //Email
        Label lblEmail = (Label)(e.Item.FindControl("lblEmail"));
        if (lblEmail != null)
        {
            lblEmail.Text = (string)DataBinder.Eval(e.Item.DataItem, "Email");
        }

        //Program
        Label lblProgram = (Label)(e.Item.FindControl("lblProgram"));
        if (lblProgram != null)
        {
            lblProgram.Text = (string)DataBinder.Eval(e.Item.DataItem, "Program");
        }

        //Status
        Label lblStatus = (Label)(e.Item.FindControl("lblStatus"));
        if (lblStatus != null)
        {
            lblStatus.Text = (string)DataBinder.Eval(e.Item.DataItem, "CurrentFlag");
        }

        //Button Edit

        HyperLink hplEdit = (HyperLink)(e.Item.FindControl("hplEdit"));
        if (hplEdit != null)
        {
            hplEdit.Text = "EDIT";
            hplEdit.NavigateUrl = "EditStudent.aspx?StudentID=" + (string)DataBinder.Eval(e.Item.DataItem, "StudentID");
           
        }

        //Button insert

        HyperLink hplinsert = (HyperLink)(e.Item.FindControl("hplinsert"));
        if (hplinsert != null)
        {
            hplinsert.Text = "INSERT";
            hplinsert.NavigateUrl = "Insert.aspx?StudentID=" + (string)DataBinder.Eval(e.Item.DataItem, "StudentID");

        }

        //Button delete

        HyperLink hpldelete = (HyperLink)(e.Item.FindControl("hpldelete"));
        if (hpldelete != null)
        {
            hpldelete.Text = "DELETE";
            hpldelete.NavigateUrl = "DeleteStudent.aspx?StudentID=" + (string)DataBinder.Eval(e.Item.DataItem, "StudentID");
         
        }


    }


    public void ExportToExcel(DataTable dt)
    {
        if (dt.Rows.Count > 0)
        {
            string filename = "DataExcel.xls";
            System.IO.StringWriter tw = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
            DataGrid dgGrid = new DataGrid();
            dgGrid.DataSource = dt;
            dgGrid.DataBind();

            //Get the HTML for the control.
            dgGrid.RenderControl(hw);
            //Write the HTML back to the browser.
            //Response.ContentType = application/vnd.ms-excel;
            Response.ContentType = "application/vnd.ms-excel";
            Response.AppendHeader("Content-Disposition", "attachment; filename=" + filename + "");
            this.EnableViewState = false;
            Response.Write(tw.ToString());
            Response.End();
        }
    }


    //// DataTable

    //  DataTable dtExccel=new DataTable();
    //    string attachment = "attahment; filename=sales.xls";
    //    Response.ClearContent();
    //    Response.AddHeader("content-disposition", attachment);
    //    Response.ContentType = "application/vnd.ms-excel";
    //    string tab = "";
 
    //    foreach (DataColumn dc in dtExccel.Columns)
    //    {
    //        Response.Write(tab + dc.ColumnName);
    //        tab = "\t";
    //    }
    //    Response.Write("\n");
 
    //    int i;
    //    foreach (DataRow dr in dtExccel.Rows)
    //    {
    //        tab = "";
    //        for (i = 0; i < dtExccel.Columns.Count; i++)
    //        {
    //            Response.Write(tab + dr[i].ToString());
    //            tab = "\t";
    //        }
    //        Response.Write("\n");
    //    }
    //    Response.End();


    //protected void RadioButton1_CheckedChanged(object sender, EventArgs e)
    //{
    //    switch (RadioButton1.SelectedIndex)
    //    {
    //        case 0://Exporting datagrid to Excel
    //            ExportData("application/vnd.xls", "FileName.xls");
    //            break;
    //        case 1://Exporting datagrid to word
    //            ExportData("application/vnd.word", "FileName.doc");
    //            break;

    //        default://Exporting datagrid to Excel
    //            ExportData("application/vnd.xls", "FileName.xls");
    //            break;
    //    }
    //}

    protected void Button2_Click(object sender, EventArgs e)
    {

  
       ExportToExcel((DataTable)ViewState["ExportToExcel"]);  


    }
    protected void RadioButton1_CheckedChanged1(object sender, EventArgs e)
    {

    }
}

*********************************************************************************************


(โค๊ดเป็นประมาณนี้นะคะ ได้โปรด...รบกวนดูให้ทีนะคะ ขอบคุณค่ะ) T^T



Tag : ASP.NET MySQL, Web (ASP.NET), C#









ประวัติการแก้ไข
2012-07-16 10:29:32
Move To Hilight (Stock) 
Send To Friend.Bookmark.
Date : 2012-07-16 10:06:26 By : manow21 View : 3498 Reply : 2
 

 

No. 1



โพสกระทู้ ( 74,058 )
บทความ ( 838 )

สมาชิกที่ใส่เสื้อไทยครีเอท

สถานะออฟไลน์
Twitter Facebook

ลองดูตัวนี้ครับ

Code (C#)
try
			{			
				// Get the datatable to export			
				DataTable dtEmployee = dsEmployee.Tables["Employee"].Copy();

				// Export all the details to Excel
				RKLib.ExportData.Export objExport = new RKLib.ExportData.Export("Win");				
				objExport.ExportDetails(dtEmployee, Export.ExportFormat.Excel, "C:\\EmployeesInfo.xls");
				lblMessage.Text = "Successfully exported to C:\\EmployeesInfo.xls";
			}
			catch(Exception Ex)
			{
				lblMessage.Text = Ex.Message;
			}


Go to : Export Excel asp.net (C#) ใครพอทราบการ export ข้อมูลจาก GridView ออกมาเป็นไฟล์ Excel ด้วยครับ






แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2012-07-16 19:09:22 By : mr.win
 


 

No. 2



โพสกระทู้ ( 5 )
บทความ ( 0 )



สถานะออฟไลน์


ขอบคุณนะคะ
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2012-07-17 11:12:47 By : manow21
 

   

ค้นหาข้อมูล


   
 

แสดงความคิดเห็น
Re : การ Export datatable to excel c# ของ web developer 2010 ว่าทำยังไง
 
 
รายละเอียด
 
ตัวหนา ตัวเอียง ตัวขีดเส้นใต้ ตัวมีขีดกลาง| ตัวเรืองแสง ตัวมีเงา ตัวอักษรวิ่ง| จัดย่อหน้าอิสระ จัดย่อหน้าชิดซ้าย จัดย่อหน้ากึ่งกลาง จัดย่อหน้าชิดขวา| เส้นขวาง| ขนาดตัวอักษร แบบตัวอักษร
ใส่แฟลช ใส่รูป ใส่ไฮเปอร์ลิ้งค์ ใส่อีเมล์ ใส่ลิ้งค์ FTP| ใส่แถวของตาราง ใส่คอลัมน์ตาราง| ตัวยก ตัวห้อย ตัวพิมพ์ดีด| ใส่โค้ด ใส่การอ้างถึงคำพูด| ใส่ลีสต์
smiley for :lol: smiley for :ken: smiley for :D smiley for :) smiley for ;) smiley for :eek: smiley for :geek: smiley for :roll: smiley for :erm: smiley for :cool: smiley for :blank: smiley for :idea: smiley for :ehh: smiley for :aargh: smiley for :evil:
Insert PHP Code
Insert ASP Code
Insert VB.NET Code Insert C#.NET Code Insert JavaScript Code Insert C#.NET Code
Insert Java Code
Insert Android Code
Insert Objective-C Code
Insert XML Code
Insert SQL Code
Insert Code
เพื่อความเรียบร้อยของข้อความ ควรจัดรูปแบบให้พอดีกับขนาดของหน้าจอ เพื่อง่ายต่อการอ่านและสบายตา และตรวจสอบภาษาไทยให้ถูกต้อง

อัพโหลดแทรกรูปภาพ

Notice

เพื่อความปลอดภัยของเว็บบอร์ด ไม่อนุญาติให้แทรก แท็ก [img]....[/img] โดยการอัพโหลดไฟล์รูปจากที่อื่น เช่นเว็บไซต์ ฟรีอัพโหลดต่าง ๆ
อัพโหลดแทรกรูปภาพ ให้ใช้บริการอัพโหลดไฟล์ของไทยครีเอท และตัดรูปภาพให้พอดีกับสกรีน เพื่อความโหลดเร็วและไฟล์ไม่ถูกลบทิ้ง

   
  เพื่อความปลอดภัยและการตรวจสอบ กระทู้ที่แทรกไฟล์อัพโหลดไฟล์จากที่อื่น อาจจะถูกลบทิ้ง
 
โดย
อีเมล์
บวกค่าให้ถูก
<= ตัวเลขฮินดูอารบิก เช่น 123 (หรือล็อกอินเข้าระบบสมาชิกเพื่อไม่ต้องกรอก)







Exchange: นำเข้าสินค้าจากจีน, Taobao, เฟอร์นิเจอร์, ของพรีเมี่ยม, ร่ม, ปากกา, power bank, แฟลชไดร์ฟ, กระบอกน้ำ

Load balance : Server 01
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่