using System; using System.Data; using System.Text; using System.Web; namespace iNethinkCMS.Helper { public class XLSHelper { public static void DataTableExportExcel(DataTable dt, string FileName) { HttpContext current = HttpContext.Current; current.Response.Clear(); current.Response.Charset = "UTF-8"; current.Response.Buffer = true; current.Response.ContentEncoding = Encoding.GetEncoding("GB2312"); current.Response.AppendHeader("Content-Disposition", "attachment;filename=\"" + HttpUtility.UrlEncode(FileName, Encoding.UTF8) + ".xls\""); current.Response.ContentType = "application/vnd.ms-excel"; foreach (DataColumn dataColumn in dt.Columns) { current.Response.Output.Write(dataColumn.ColumnName.ToString().Trim() + "\t"); } current.Response.Output.Write("\n"); foreach (DataRow dataRow in dt.Rows) { string text = ""; for (int i = 0; i < dt.Columns.Count; i++) { text = text + dataRow[i].ToString().Trim() + "\t"; } text = text.Substring(0, text.Length - 1) + "\n"; current.Response.Output.Write(text); } current.Response.Output.Flush(); current.Response.End(); } public static string Fun_Substring(object byInfo, int bylength) { if (byInfo.ToString().Length > bylength) { return byInfo.ToString().Substring(0, bylength) + "..."; } else { return byInfo.ToString(); } } } }