datatable导出Excel

private void dataTableToCsv()
{

 //DATATABLE 或者LIST

StringWriter sw = new StringWriter();
// \t 代表列分隔
sw.WriteLine("<table cellpadding=\"0\" cellspacing=\"0\" border='1'><tr><td>AAA</td><td>BB</td><td>CC</td><td>D</td><td>E</td><td>F</td><td>G</td><td>H</td><td>I</td><td>J</td><td>K</td><td>L</td><td>M</td><td>N</td></tr>");
foreach (bzsrEntity dr in lst)
{
sw.WriteLine("<tr><td style='vnd.ms-excel.numberformat:@'>" + dr.bh2 + "</td><td>" + dr.name + "</td><td style='vnd.ms-excel.numberformat:@'>" + dr.fwdz + "</td><td style='vnd.ms-excel.numberformat:@'>" + dr.sfz + "</td><td style='vnd.ms-excel.numberformat:@'>" + dr.tdzh
+ "</td><td>" + dr.wdj_jzmj + "</td><td>" + dr.jzmj + "</td><td>" + dr.wdj_hfmj + "</td><td>" + dr.symj + "</td><td>" + dr.hdmj + "</td><td>" + dr.ffsymj + "</td><td>" + dr.CengDif + "</td><td>" + dr.rk + "</td><td style='vnd.ms-excel.numberformat:@'>" + dr.bz + "</td></tr>");
}
sw.WriteLine("</table>");
sw.Close();
Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(XX表"+DateTime.Now.ToString("yyyy-MM-dd")+", Encoding.UTF8).ToString());
Response.ContentType = "application/ms-excel";
Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");
Response.Write(sw);
Response.End();


}

你可能感兴趣的:(Datatable)