(C#) ASP.NET Export Record/Data To CSV |
(C#) ASP.NET Export Record To CSV ตัวอย่างการเลือกข้อมูลจาก Control ผ่าน Checkbox โดยเลือกรายการที่ได้ทำการคลิกเลือก เพื่อเขียนข้อมูลลงใน Text Files ตัวอย่างนี้ผมจะใช้ Repeater แทนครับ แต่ทั้งนี้สามารถใช้ DataGrid หรือ GridView เข้ามาใช้ได้เช่นเดียวกันครับ
Language Code : VB.NET || C#
Framework : 1,2,3,4
AspNetExportDataToCsv.aspx
<%@ Import Namespace="System.Data"%>
<%@ Import Namespace="System.Data.OleDb"%>
<%@ Import Namespace="System.IO"%>
<%@ Page Language="C#" Debug="true" %>
<script runat="server">
void Page_Load(object sender,EventArgs e)
{
if(!Page.IsPostBack)
{
BindData();
}
}
void BindData()
{
OleDbConnection objConn;
OleDbCommand objCmd;
String strConnString;
strConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" +
Server.MapPath("database/mydatabase.mdb")+";";
objConn = new OleDbConnection(strConnString);
objConn.Open();
String strSQL;
strSQL = "SELECT * FROM customer";
OleDbDataReader dtReader;
objCmd = new OleDbCommand(strSQL, objConn);
dtReader = objCmd.ExecuteReader();
//*** BindData to Repeater ***//
myRepeater.DataSource = dtReader;
myRepeater.DataBind();
dtReader.Close();
dtReader = null;
objConn.Close();
objConn = null;
}
void Button1_Click(object sender,EventArgs e)
{
System.Data.OleDb.OleDbConnection objConn;
System.Data.OleDb.OleDbCommand objCmd;
String strConnString,strSQL;
int i;
String strPath = "csv/";
String strFileName = "customer.csv";
//*** Create Text Files (Columns Default ***// ***//
StreamWriter StrWer;
StrWer = File.CreateText(Server.MapPath(strPath) + strFileName);
StrWer.Write("CustomerID,Name,Email,CountryCode,Budget,Used");
StrWer.Close();
//*** Connect to CSV ***//
strConnString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + Server.MapPath(strPath) +
";Extended Properties='TEXT;HDR=Yes;FMT=Delimited;Format=Delimited(,)'";
objConn = new System.Data.OleDb.OleDbConnection(strConnString);
objConn.Open();
CheckBox chkCusID;
Label lblCustomerID,lblName,lblEmail,lblCountryCode,lblBudget,lblUsed;
for( i = 0 ; i <= myRepeater.Items.Count - 1 ; i ++)
{
chkCusID =(CheckBox) myRepeater.Items[i].FindControl("chkCustomerID");
lblCustomerID = (Label) myRepeater.Items[i].FindControl("lblCustomerID");
lblName = (Label) myRepeater.Items[i].FindControl("lblName");
lblEmail = (Label) myRepeater.Items[i].FindControl("lblEmail");
lblCountryCode = (Label) myRepeater.Items[i].FindControl("lblCountryCode");
lblBudget = (Label) myRepeater.Items[i].FindControl("lblBudget");
lblUsed = (Label) myRepeater.Items[i].FindControl("lblUsed");
if(chkCusID.Checked == true)
{
//*** Insert to CSV ***//
strSQL = "INSERT INTO " + strFileName + " (CustomerID,Name,Email,CountryCode,Budget,Used) " +
"VALUES ('" + lblCustomerID.Text + "','" + lblName.Text + "','" + lblEmail.Text + "'" +
" ,'" + lblCountryCode.Text + "','" + lblBudget.Text + "' " +
",'" + lblUsed.Text + "')";
objCmd = new System.Data.OleDb.OleDbCommand();
objCmd.Connection = objConn;
objCmd.CommandType = CommandType.Text;
objCmd.CommandText = strSQL;
objCmd.ExecuteNonQuery() ;
}
}
objCmd = null;
objConn.Close();
objConn = null;
this.lblText.Text = "Export Successfully <a href=" + strPath + strFileName + ">Click here</a> to download. ";
}
</script>
<html>
<head>
<title>ThaiCreate.Com ASP.NET - CSV</title>
</head>
<body>
<form id="form1" runat="server">
<table border="1">
<asp:Repeater id="myRepeater" runat="server">
<HeaderTemplate>
<tr>
<th>Select</th>
<th>CustomerID</th>
<th>Name</th>
<th>Email</th>
<th>CountryCode</th>
<th>Budget</th>
<th>Used</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td align="center"><asp:CheckBox id="chkCustomerID" runat="server"></asp:CheckBox></td>
<td align="center"><asp:Label id="lblCustomerID" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "CustomerID") %>'></asp:Label></td>
<td><asp:Label id="lblName" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Name") %>'></asp:Label></td>
<td><asp:Label id="lblEmail" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Email") %>'></asp:Label></td>
<td align="center"><asp:Label id="lblCountryCode" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "CountryCode") %>'></asp:Label></td>
<td align="right"><asp:Label id="lblBudget" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Budget") %>'></asp:Label></td>
<td align="right"><asp:Label id="lblUsed" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Used") %>'></asp:Label></td>
</tr>
</ItemTemplate>
</asp:Repeater>
</table>
<br />
<asp:Button id="Button1" onclick="Button1_Click" runat="server" Text="Submit"></asp:Button>
<hr />
<asp:Label id="lblText" runat="server"></asp:Label>
</form>
</body>
</html>
Screenshot
|