导出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)转换下