泛型List导出Excel

简介:
今天整了个Excel泛型导出,切出来大家看看
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Reflection; 
namespace Utility.Util 

         public  class Ecel<T> 
        { 
                 public  static  void DataBindTitleExcel(System.Web.HttpResponseBase response, List<T> list, List< string> columnName, List< string> propertyName,  string ExcelTitle,  string strUserMsg) 
                { 
 
                         if (list.Count == 0) 
                        { 
                                response.Write( "<script>alert('对不起,没有查询到任何记录,导出失败!')</script>"); 
                                response.End(); 
                        } 
                        response.ContentEncoding = Encoding.GetEncoding( "GB2312"); 
                        response.ContentType =  "application/ms-excel"
                        response.AppendHeader( "Content-Disposition""attachment;filename=Export.xls"); 
                         int count = list.Count; 
                        StringBuilder builder =  new StringBuilder(); 
                        builder.Append( "<html><head>\n"); 
                        builder.Append( "<meta http-equiv=\"Content-Language\" content=\"zh-cn\">\n"); 
                        builder.Append( "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=gb2312\">\n"); 
                        builder.Append( "</head>\n"); 
                        builder.Append( "<table border=1>"); 
                         if (ExcelTitle != "") 
                        { 
                                 string str =  "<font size=4><b>" + ExcelTitle +  "</b></font>"
                                 if (strUserMsg != "") 
                                { 
                                        str = str +  "(" + strUserMsg +  ")"
                                } 
                                builder.Append( string.Concat( new  object[] {  "<tr><td colspan=", count,  ">", str,  "</td></tr>" })); 
                        } 
                        builder.Append( "<tr><td colspan=" + count +  " valign=middle height=24>"); 
                        builder.Append( "查询时间:" + DateTime.Now.ToString( "G") +  "</td></tr>"); 
                        builder.Append( "<tr>\n"); 
                         for ( int i = 0; i < columnName.Count; i++) 
                        { 
 
                                builder.Append( "<td bgcolor=#969696><b>" + columnName[i] +  "</b></td>\n"); 
 
                        } 
                        Type objType =  typeof(T); 
                        BindingFlags bf = BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static; //反射标识 
                        PropertyInfo[] propInfoArr = objType.GetProperties(bf); 
 
                         foreach (T model  in list) 
                        { 
 
                                builder.Append( "<tr>"); 
 
                                 foreach (PropertyInfo propInfo  in propInfoArr) 
                                { 
 
                                         foreach ( string propName  in propertyName) 
                                        { 
 
                                                 if ( string.Compare(propInfo.Name.ToUpper(), propName.ToUpper()) == 0) 
                                                { 
 
                                                        PropertyInfo modelProperty = model.GetType().GetProperty(propName); 
                                                         if (modelProperty !=  null
                                                        { 
 
                                                                 object objResult = modelProperty.GetValue(model,  null); 
                                                                builder.Append( "<td style='vnd.ms-excel.numberformat:@'>" + ((objResult ==  null) ?  string.Empty : objResult) +  "</td>"); 
                                                        } 
                                                         else 
                                                        { 
                                                                 throw  new Exception( "Property name may be not exists!"); 
                                                        } 
                                                } 
                                        } 
                                } 
                                builder.Append( "</tr>\n"); 
                        } 
                        builder.Append( "</table>\n"); 
                        response.Write(builder.ToString()); 
                        response.End(); 
                } 
 
        } 

看看效果,这是在MVC2中的导出
在Webforms中只要你将response换成System.Web.UI.Page page对象的Response即可。


本文转自 BruceAndLee 51CTO博客,原文链接:http://blog.51cto.com/leelei/346978,如需转载请自行联系原作者

相关文章
|
12月前
|
Python
Excel中如何批量重命名工作表与将每个工作表导出到单独Excel文件
本文介绍了如何在Excel中使用VBA批量重命名工作表、根据单元格内容修改颜色,以及将工作表导出为独立文件的方法。同时提供了Python实现导出工作表的代码示例,适用于自动化处理Excel文档。
|
SQL C# 数据库
EPPlus库的安装和使用 C# 中 Excel的导入和导出
本文介绍了如何使用EPPlus库在C#中实现Excel的导入和导出功能。首先,通过NuGet包管理器安装EPPlus库,然后提供了将DataGridView数据导出到Excel的步骤和代码示例,包括将DataGridView转换为DataTable和使用EPPlus将DataTable导出为Excel文件。接着,介绍了如何将Excel数据导入到数据库中,包括读取Excel文件、解析数据、执行SQL插入操作。
EPPlus库的安装和使用 C# 中 Excel的导入和导出
|
数据格式 UED
记录一次NPOI库导出Excel遇到的小问题解决方案
【11月更文挑战第16天】本文记录了使用 NPOI 库导出 Excel 过程中遇到的三个主要问题及其解决方案:单元格数据格式错误、日期格式不正确以及合并单元格边框缺失。通过自定义单元格样式、设置数据格式和手动添加边框,有效解决了这些问题,提升了导出文件的质量和用户体验。
1187 3
|
前端开发
实现Excel文件和其他文件导出为压缩包,并导入
实现Excel文件和其他文件导出为压缩包,并导入
407 1
|
JavaScript 前端开发 数据处理
Vue导出el-table表格为Excel文件的两种方式
Vue导出el-table表格为Excel文件的两种方式
1226 6
|
存储 Java API
Java实现导出多个excel表打包到zip文件中,供客户端另存为窗口下载
Java实现导出多个excel表打包到zip文件中,供客户端另存为窗口下载
1179 4
|
Java API Apache
|
easyexcel Java UED
SpringBoot中大量数据导出方案:使用EasyExcel并行导出多个excel文件并压缩zip后下载
在SpringBoot环境中,为了优化大量数据的Excel导出体验,可采用异步方式处理。具体做法是将数据拆分后利用`CompletableFuture`与`ThreadPoolTaskExecutor`并行导出,并使用EasyExcel生成多个Excel文件,最终将其压缩成ZIP文件供下载。此方案提升了导出效率,改善了用户体验。代码示例展示了如何实现这一过程,包括多线程处理、模板导出及资源清理等关键步骤。
|
前端开发 JavaScript Java
导出excel的两个方式:前端vue+XLSX 导出excel,vue+后端POI 导出excel,并进行分析、比较
这篇文章介绍了使用前端Vue框架结合XLSX库和后端结合Apache POI库导出Excel文件的两种方法,并对比分析了它们的优缺点。
3104 0