/// <summary>
/// 将DataTable导出到Excel表
/// </summary>
/// <param name="dt"></param>
/// <param name="FileName"></param>
public void ToExcel(DataTable dt, string FileName)
{
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8) + ".xls");
HttpContext.Current.Response.ContentType = "application/ms-excel";//image/JPEG;text/HTML;image/GIF;vnd.ms-excel/msword
int i = 0, j;
for (i = 0; i < dt.Columns.Count; i++)
{
HttpContext.Current.Response.Write(dt.Columns[i].ToString() + Constants.vbTab);
}
HttpContext.Current.Response.Write(Constants.vbCrLf);
foreach (DataRow dr in dt.Rows)
{
for (j = 0; j < dr.ItemArray.Length; j++)
HttpContext.Current.Response.Write(dr.ItemArray[j].ToString() + Constants.vbTab);
HttpContext.Current.Response.Write(Constants.vbCrLf);
}
HttpContext.Current.Response.End();
}
.net 导出Excel报表
最新推荐文章于 2018-08-29 09:45:31 发布