导出excel
private void OutputContactExcel(string strFileName, DataTable dtOutPut, string strTextType)
{
StringWriter sw = new StringWriter();
string strColumns = "";
foreach (DataColumn Columns in dtOutPut.Columns)
{
strColumns = strColumns + "\t" + Columns.ColumnName;
}
sw.WriteLine(strColumns.Substring(1));
foreach (DataRow dr in dtOutPut.Rows)
{
strColumns = "";
foreach (DataColumn Columns in dtOutPut.Columns)
{
strColumns = strColumns + "\t" + dr[Columns.ColumnName];
}
sw.WriteLine(strColumns.Substring(1));
}
sw.Close();
System.Web.HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + strFileName);
System.Web.HttpContext.Current.Response.ContentType = "application/ms-excel";
if (strTextType != "")
{
System.Web.HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding(strTextType); //"GB2312"
}
System.Web.HttpContext.Current.Response.Write(sw);
System.Web.HttpContext.Current.Response.End();
}

浙公网安备 33010602011771号