zoukankan      html  css  js  c++  java
  • 使用NPOI 做Excel导出

    1.先去官网:http://npoi.codeplex.com/下载需要引入dll 然后在网站中添加引用。
    2.Asp.Net mvc导出方法
    ///
    <summary> /// 导出Excel /// </summary> /// <returns></returns> public FileResult Excel() { var alist = WishWallServices.QueryWhere(c => c.status == 1).OrderByDescending(c=>c.WishThumbup.Where(q=>q.status==1).Count()).ToList(); //创建Excel文件的对象 NPOI.HSSF.UserModel.HSSFWorkbook book = new NPOI.HSSF.UserModel.HSSFWorkbook(); //添加一个sheet NPOI.SS.UserModel.ISheet sheet1 = book.CreateSheet("心愿墙"); //给sheet添加第一行的头部标题 NPOI.SS.UserModel.IRow row1 = sheet1.CreateRow(0); row1.CreateCell(0).SetCellValue("微信号"); row1.CreateCell(1).SetCellValue("姓名"); row1.CreateCell(2).SetCellValue("心愿标题"); row1.CreateCell(3).SetCellValue("心愿内容"); row1.CreateCell(4).SetCellValue("点赞数"); row1.CreateCell(5).SetCellValue("联系方式"); row1.CreateCell(6).SetCellValue("联系地址"); //....N行 //将数据逐步写入sheet1各个行 for (int i = 0; i < alist.Count; i++) { NPOI.SS.UserModel.IRow rowtemp = sheet1.CreateRow(i + 1); rowtemp.CreateCell(0).SetCellValue(alist[i].WeiXinInfo.WeiXinName); rowtemp.CreateCell(1).SetCellValue(alist[i].name); rowtemp.CreateCell(2).SetCellValue(alist[i].title); rowtemp.CreateCell(3).SetCellValue(alist[i].content); rowtemp.CreateCell(4).SetCellValue(alist[i].WishThumbup.Where(q=>q.status==1).Count()); rowtemp.CreateCell(5).SetCellValue(alist[i].phone); //....N行 } System.IO.MemoryStream ms = new System.IO.MemoryStream(); book.Write(ms); ms.Seek(0, SeekOrigin.Begin); return File(ms, "application/vnd.ms-excel", "心愿墙.xls"); }
  • 相关阅读:
    单例模式(Singleton)的6种实现
    深入浅出单实例Singleton设计模式
    Singleton单例模式
    面试中的Singleton
    海量数据存储之Key-Value存储简介
    大数据时代的 9 大Key-Value存储数据库
    python 多线程两种实现方式,Python多线程下的_strptime问题,
    pycURL的内存问题
    百万级访问网站前期的技术准备
    IPv6 tutorial – Part 6: Site-local addresses and link-local addresses
  • 原文地址:https://www.cnblogs.com/MingqiSs/p/7827537.html
Copyright © 2011-2022 走看看