架构师_程序员_码农网

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

搜索
查看: 14212|回复: 0

[ASP.NET] asp.net mvc将list集合或者datatable导出成excel表格

[复制链接]
发表于 2015-11-13 10:19:13 | 显示全部楼层 |阅读模式
  1. /// <summary>
  2.     /// 将DataTable导出成Excel表格
  3.     /// </summary>
  4.     public class ExcelHelper
  5.     {
  6.         public MemoryStream RenderToExcel(DataTable table)
  7.         {
  8.             MemoryStream ms = new MemoryStream();

  9.             using (table)
  10.             {
  11.                 IWorkbook workbook = new HSSFWorkbook();

  12.                 ISheet sheet = workbook.CreateSheet();

  13.                 IRow headerRow = sheet.CreateRow(0);

  14.                 // handling header.
  15.                 foreach (DataColumn column in table.Columns)
  16.                     headerRow.CreateCell(column.Ordinal).SetCellValue(column.Caption);//If Caption not set, returns the ColumnName value

  17.                 // handling value.
  18.                 int rowIndex = 1;

  19.                 foreach (DataRow row in table.Rows)
  20.                 {
  21.                     IRow dataRow = sheet.CreateRow(rowIndex);

  22.                     foreach (DataColumn column in table.Columns)
  23.                     {
  24.                         dataRow.CreateCell(column.Ordinal).SetCellValue(row[column].ToString());
  25.                     }

  26.                     rowIndex++;
  27.                 }

  28.                 workbook.Write(ms);
  29.                 ms.Flush();
  30.                 ms.Position = 0;


  31.             }
  32.             return ms;
  33.         }
  34.     }
复制代码
  1. public ActionResult ExportBrand(int id)
  2.         {
  3.             int aaa = id;
  4.             //查询参加会议的数据
  5.             List<BLL.Entitys.InBoardRoomPersonnel> list = BLL.Brand.Index.GetJoinBrandUser(id);
  6.             //需要填写的字段
  7.             //List<BLL.Entitys.GetDictionary> dic = BLL.Until.GetUserInfo.SelectUser(Extcredits);
  8.             //转换成list集合
  9.             List<BLL.Entitys.UserInfo> userlist = new List<UserInfo>();
  10.             foreach (BLL.Entitys.InBoardRoomPersonnel ir in list)
  11.             {
  12.                 var getMS = new MemoryStream(ir.BoardRoomUser);
  13.                 getMS.Position = 0;
  14.                 BLL.Entitys.UserInfo u = BinarySerializerHelper.DeSerialize<BLL.Entitys.UserInfo>(getMS);
  15.                 userlist.Add(u);
  16.             }
  17.             DataTable dt = userlist.FillDataTable<BLL.Entitys.UserInfo>();
  18.             dt =BLL.Until.DataTableExtensions.TranslateDataTable(dt);
  19.             BLL.Until.ExcelHelper helper = new ExcelHelper();
  20.             //bll helper = new BLL.Export.ExportHelper();
  21.             MemoryStream ms = helper.RenderToExcel(dt);
  22.             return File(ms, "xls", "Brand" + DateTime.Now.ToString("yyyyMMddhhmm") + ".xls");
  23.         }
复制代码
  1. /// <summary>
  2.         /// 实体类转换成DataTable
  3.         /// </summary>
  4.         /// <param name="modelList">实体类列表</param>
  5.         /// <returns></returns>
  6.         public static DataTable FillDataTable<T>(this List<T> modelList)
  7.         {
  8.             if (modelList == null || modelList.Count == 0)
  9.             {
  10.                 return null;
  11.             }
  12.             DataTable dt = CreateData(modelList[0]);

  13.             foreach (T model in modelList)
  14.             {
  15.                 DataRow dataRow = dt.NewRow();
  16.                 foreach (PropertyInfo propertyInfo in typeof(T).GetProperties())
  17.                 {
  18.                     dataRow[propertyInfo.Name] = propertyInfo.GetValue(model, null);
  19.                 }
  20.                 dt.Rows.Add(dataRow);
  21.             }
  22.             return dt;
  23.         }

  24.         /// <summary>
  25.         /// 根据实体类得到表结构
  26.         /// </summary>
  27.         /// <param name="model">实体类</param>
  28.         /// <returns></returns>
  29.         private static DataTable CreateData<T>(T model)
  30.         {
  31.             DataTable dataTable = new DataTable(typeof(T).Name);
  32.             foreach (PropertyInfo propertyInfo in typeof(T).GetProperties())
  33.             {
  34.                 dataTable.Columns.Add(new DataColumn(propertyInfo.Name, propertyInfo.PropertyType));
  35.             }
  36.             return dataTable;
  37.         }
  38.         /// <summary>
  39.         /// DataTable字段翻译成中文
  40.         /// </summary>
  41.         /// <param name="table"></param>
  42.         /// <returns></returns>
  43.         public static DataTable TranslateDataTable(DataTable table)
  44.         {
  45.             DataTable dt = new DataTable();
  46.             dt.TableName = "TempTable";

  47.             if (table != null && table.Rows.Count > 0)
  48.             {
  49.                 //先为dt构造列信息  
  50.                 foreach (DataColumn column in table.Columns)
  51.                 {
  52.                     string name = BLL.Until.GetUserInfo.GetBewrite(column.ColumnName);
  53.                     dt.Columns.Add(name);
  54.                 }

  55.                 for (int i = 0; i < table.Rows.Count; i++)
  56.                 {
  57.                     DataRow NewRow = dt.NewRow();
  58.                     DataRow row = table.Rows[i];

  59.                     for (int j = 0; j < dt.Columns.Count; j++)
  60.                     {
  61.                         NewRow[j] = row[j].ToString();
  62.                     }

  63.                     dt.Rows.Add(NewRow);
  64.                 }
  65.             }
  66.             return dt;
  67.         }
复制代码


如果是list集合,需要先转成datatable,然后再进行导出。
NPOI.zip (553.95 KB, 下载次数: 2, 售价: 2 粒MB)




上一篇:中国多家网站被clientHold
下一篇:&quot;The ConnectionString property has not been initialized.&quot;错误解决办法
码农网,只发表在实践过程中,遇到的技术难题,不误导他人。
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

免责声明:
码农网所发布的一切软件、编程资料或者文章仅限用于学习和研究目的;不得将上述内容用于商业或者非法用途,否则,一切后果请用户自负。本站信息来自网络,版权争议与本站无关。您必须在下载后的24个小时之内,从您的电脑中彻底删除上述内容。如果您喜欢该程序,请支持正版软件,购买注册,得到更好的正版服务。如有侵权请邮件与我们联系处理。

Mail To:help@itsvse.com

QQ|手机版|小黑屋|架构师 ( 鲁ICP备14021824号-2 )|网站地图

GMT+8, 2025-11-16 05:38

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表