在ASP.net中如何使用NPOI.dll导出EXCEL最好是C#开发语言
展开全部
不用这个,自己写一个多快的,我用的这个,自己写的,CSV格式,可能EXCEL打开,你可另存一下就成xls了/// /// 将DataTable转换为字符串/// /// /// public static string DtToString(DataTable dt){ string data = ""; try { foreach (DataColumn column in dt.Columns) { data += column.ColumnName + ","; } data += "
";//写出数据 foreach (DataRow row in dt.Rows) { foreach (DataColumn column in dt.Columns) { string t = row[column].ToString(); if (!string.IsNullOrEmpty(t)) { t = t.Replace(",", ""); t = t.Replace("
", ""); t = t.Replace("
", ""); t = HttpContext.Current.Server.HtmlEncode(t); data += t + ","; } else data += ","; } data += "
"; } data += "
"; } catch { } return data;}/// /// 将字符串转换为CSV格式输出/// /// 字符串内容/// 文件名(无扩展名)/// 表头消息public static void StringToCSV(string content, string fileName, string msg){ try { string temp = string.Format("attachment;filename={0}.csv", fileName); HttpContext.Current.Response.ClearHeaders(); HttpContext.Current.Response.Clear(); HttpContext.Current.Response.Buffer = true; HttpContext.Current.Response.Charset = "UTF-8"; HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.Default; HttpContext.Current.Response.ContentType = "application/ms-excel"; HttpContext.Current.Response.AppendHeader("Content-disposition", temp); if (!string.IsNullOrEmpty(msg)) HttpContext.Current.Response.Write(msg + "
"); HttpContext.Current.Response.Write(content); HttpContext.Current.Response.End(); } catch { }}使用方法:查询出数据,为DataTable,在按钮事件中写StringToCSV(DtToString(你的DataTable));在ASP.net中如何使用NPOI.dll导出EXCEL最好是C#开发语言
";//写出数据 foreach (DataRow row in dt.Rows) { foreach (DataColumn column in dt.Columns) { string t = row[column].ToString(); if (!string.IsNullOrEmpty(t)) { t = t.Replace(",", ""); t = t.Replace("
", ""); t = t.Replace("
", ""); t = HttpContext.Current.Server.HtmlEncode(t); data += t + ","; } else data += ","; } data += "
"; } data += "
"; } catch { } return data;}/// /// 将字符串转换为CSV格式输出/// /// 字符串内容/// 文件名(无扩展名)/// 表头消息public static void StringToCSV(string content, string fileName, string msg){ try { string temp = string.Format("attachment;filename={0}.csv", fileName); HttpContext.Current.Response.ClearHeaders(); HttpContext.Current.Response.Clear(); HttpContext.Current.Response.Buffer = true; HttpContext.Current.Response.Charset = "UTF-8"; HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.Default; HttpContext.Current.Response.ContentType = "application/ms-excel"; HttpContext.Current.Response.AppendHeader("Content-disposition", temp); if (!string.IsNullOrEmpty(msg)) HttpContext.Current.Response.Write(msg + "
"); HttpContext.Current.Response.Write(content); HttpContext.Current.Response.End(); } catch { }}使用方法:查询出数据,为DataTable,在按钮事件中写StringToCSV(DtToString(你的DataTable));在ASP.net中如何使用NPOI.dll导出EXCEL最好是C#开发语言
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询