您的位置:首页 > 其它

导出数据到CSV文件

2010-05-13 11:04 411 查看
ASPX页面:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="CreateCSV.aspx.cs" Inherits="_CreateCSV" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="btnExcel" runat="server" Text="导出" OnClick="btnExcel_Click" />
<br />
<asp:HyperLink ID="hlExcel" runat="server"></asp:HyperLink>
<asp:GridView ID="gvList" runat="server">
</asp:GridView>
<br />
<asp:Label ID="lblMessage" runat="server"></asp:Label>
<p>
备注: 为了能正常的下载CSV类型的文件,需要对IIS进行相应的设置。
配置网站成功后,在HTTP header选项卡下添加新的 MIME Map,扩展名为 .csv, MIME为 text/plain 即可。
</p>
</div>
</form>
</body>
</html>

后台代码:

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.IO;
using System.Text;

public partial class _CreateCSV : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}

private DataTable getData()
{
DataTable dt = new DataTable();

try
{
DataColumn col1 = dt.Columns.Add("Name", typeof(string));
DataColumn col2 = dt.Columns.Add("English", typeof(string));
DataColumn col3 = dt.Columns.Add("Chinese", typeof(string));
DataColumn col4 = dt.Columns.Add("Math", typeof(string));

for (int i = 0; i < 20; i++)
{
DataRow rows = dt.NewRow();
rows[0] = "Kevin" + i.ToString();
rows[1] = i + 80;
rows[2] = i + 78;
rows[3] = i + 82;
dt.Rows.Add(rows);
}
}
catch (Exception exDT)
{
throw new Exception(exDT.Message);
}
finally
{
dt.Dispose();
}

return dt;
}

public bool CreateCSV(DataTable dt, string filePath, bool headFlg)
{
FileStream fp;
StreamWriter sw;
bool bValue = false;

try
{
string headerString = "";
string bodyString = "";

fp = new FileStream(filePath, FileMode.Create);
sw = new StreamWriter(fp, Encoding.GetEncoding(936));

if (headFlg)
{
for (int j = 0; j < dt.Columns.Count; j++)
{
headerString += "/"" + dt.Columns[j].ColumnName.Trim() + "/",";
}

headerString = headerString.Substring(0, headerString.Length - 1);
sw.WriteLine(headerString);
}

for (int i = 0; i < dt.Rows.Count; i++)
{
for (int j = 0; j < dt.Columns.Count; j++)
{
bodyString += "/"" + dt.Rows[i][j].ToString() + "/",";
}

bodyString = bodyString.Substring(0, bodyString.Length - 1);
sw.WriteLine(bodyString);
bodyString = "";
}

sw.Close();
fp.Close();
bValue = true;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
dt.Dispose();
}

return bValue;
}

protected void btnExcel_Click(object sender, EventArgs e)
{
DataTable dt = new DataTable();
string filename = DateTime.Now.ToString("yyyyMMHHmmss") + ".csv";
bool bFlag = false;

try
{
dt = getData();

gvList.DataSource = dt.DefaultView;
gvList.DataBind();

bFlag = CreateCSV(dt, Server.MapPath("download/") + filename, true);

if (bFlag)
{
hlExcel.NavigateUrl = "~/download/" + filename;
hlExcel.Text = filename;

lblMessage.Text = "OK";
}
}
catch (Exception ex)
{
lblMessage.Text = ex.Message;
}
finally
{
dt.Dispose();
}
}
}

本案例适用于简单的表格数据导出,要想导出复杂的,嘿嘿,以后再说,哈哈~~
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: