|
|
|
การ Export datatable to excel c# ของ web developer 2010 ว่าทำยังไง |
|
|
|
|
|
|
|
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
|
|
|
|
|
Date :
2012-07-16 10:06:26 |
By :
manow21 |
View :
3618 |
Reply :
2 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ลองดูตัวนี้ครับ
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 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ขอบคุณนะคะ
|
|
|
|
|
Date :
2012-07-17 11:12:47 |
By :
manow21 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 01
|