导出Excel事例

 DataTable table = new DataTable();

StringWriter sw = new StringWriter();

string tabltitle = "客户名称,电话号码,导入时间,分组编号,透传号码,预测状态";//excel表头
sw.WriteLine(tabltitle);

foreach (DataRow dr in table.Rows)
{
string tempstr = dr[0].ToString().Replace(",", "-") + "," + dr[1].ToString().Replace(",", "-") + ",";
tempstr += dr[2].ToString().Replace(",", "-") + ",";
tempstr += dr[3].ToString().Replace(",", "-") + ",";
tempstr += dr[4].ToString().Replace(",", "-") + ",";
tempstr += dr[5].ToString().Replace(",", "-");
sw.WriteLine(tempstr);
}
sw.Close();
Response.AddHeader("Content-Disposition", "attachment; filename=FORECASTInfoBackup.csv");
Response.ContentType = "application/ms-excel";
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
Response.Write(sw);
Response.End();

//ps:如果fileName是中文则需要 HttpUtility.UrlEncode(filename, Encoding.UTF8)转换下

posted @ 2016-04-28 13:11  D调灬仔  阅读(151)  评论(0编辑  收藏  举报

作者D调灬仔

出处:https://www.cnblogs.com/chj929555796/

如果觉得这篇文章对你有小小的帮助的话,记得在右下角点个“推荐”哦,博主在此感谢!