首先你要是用的是maven项目,你需要下一个jar包,添加一个配置:

//在maven中配jar包<dependency>  <groupId>net.sourceforge.jexcelapi</groupId>  <artifactId>jxl</artifactId>  <version>2.6.10</version>  </dependency>  

将数据生成excle表格在controller中没什么好写的,无非是整理需要生成excle的数据,最主要是生成的过程需要考虑的问题很多,特别是大量数据的存储问题

可以新建一个工具类:

package com.ai.loc.util;import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;import jxl.Workbook;
import jxl.format.Alignment;
import jxl.format.Border;
import jxl.format.BorderLineStyle;
import jxl.format.Colour;
import jxl.format.VerticalAlignment;
import jxl.write.Label;
import jxl.write.NumberFormats;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;public class MakeExcel {private static WritableCellFormat titleFormat = null;private static WritableCellFormat bodyFormat = null;private static WritableCellFormat noteFormat = null;private static WritableCellFormat floatFormat = null;private static WritableCellFormat intFormat = null;private static boolean init = false;public MakeExcel() {}public void init() throws WriteException {WritableFont font1, font2, font3, font4;font1 = new WritableFont(WritableFont.ARIAL, 9, WritableFont.BOLD,false);titleFormat = new WritableCellFormat(font1);titleFormat.setBackground(Colour.ORANGE);titleFormat.setBorder(Border.ALL, BorderLineStyle.THIN);titleFormat.setAlignment(Alignment.CENTRE);font2 = new WritableFont(WritableFont.ARIAL, 9, WritableFont.BOLD,false);noteFormat = new WritableCellFormat(font2);noteFormat.setBackground(Colour.ORANGE);noteFormat.setBorder(Border.ALL, BorderLineStyle.THIN);noteFormat.setAlignment(Alignment.CENTRE);noteFormat.setVerticalAlignment(VerticalAlignment.CENTRE);noteFormat.setWrap(true);font3 = new WritableFont(WritableFont.ARIAL, 9, WritableFont.NO_BOLD,false);bodyFormat = new WritableCellFormat(font3);bodyFormat.setBackground(Colour.LIGHT_GREEN);bodyFormat.setBorder(Border.ALL, BorderLineStyle.THIN);font4 = new WritableFont(WritableFont.ARIAL, 9, WritableFont.NO_BOLD,false);floatFormat = new WritableCellFormat(font4, NumberFormats.FLOAT);floatFormat.setBackground(Colour.LIGHT_GREEN);floatFormat.setBorder(Border.ALL, BorderLineStyle.THIN);// Arial���壬9�ţ��Ǵ��壬��Ԫ����ɫ�����ֱ߿�font4 = new WritableFont(WritableFont.ARIAL, 9, WritableFont.NO_BOLD,false);intFormat = new WritableCellFormat(font4, NumberFormats.INTEGER);intFormat.setBackground(Colour.LIGHT_GREEN);intFormat.setBorder(Border.ALL, BorderLineStyle.THIN);init = true;}//入参是一个list,一条数据存一个map对象,map对象中存列和值得对应关系,destFile当然就是要存的文件信息。  headList很重要,它是列的展示,当然和数据的列要对应不然找不到对应的地方存储。public static void CreateExcelFile(List<Map<String,Object>> list, File destFile,List<String> headList,String message)throws WriteException, IOException {int sizeAll=list.size();int zheng=sizeAll/65534;int yu=sizeAll%65534;int sheetSize=1;int flagList=1;if(sizeAll<=65534){sheetSize=1;}else{if(yu>0){sheetSize=zheng+1;}else{sheetSize=zheng;}}if (init == false)new MakeExcel().init();WritableWorkbook book = null;book = Workbook.createWorkbook(destFile);if(list.size()==0){book.write();}else{for(int j=0;j<sheetSize;j++){int index;System.out.println("*************************sheet"+j+"***************************");WritableSheet sheet = book.createSheet(destFile.getName().replace(".xls", "")+j, j);WritableFont BoldFont=new WritableFont(WritableFont.ARIAL, 10, WritableFont.BOLD);WritableCellFormat wcf_center = new WritableCellFormat(BoldFont);wcf_center.setBorder(Border.ALL, BorderLineStyle.THIN); // 线条wcf_center.setVerticalAlignment(VerticalAlignment.CENTRE); // 文字垂直对齐wcf_center.setAlignment(Alignment.CENTRE); // 文字水平对齐wcf_center.setWrap(false); // 文字是否换行for(int i=0;i<headList.size()+1;i++){sheet.setColumnView(i, 60);}sheet.mergeCells(0, 0, headList.size()-1, 0);sheet.addCell(new Label(0, 0, message, wcf_center));index = 0;for(String  name :headList){sheet.addCell(new Label(index, 1, name,wcf_center));index++;}int i=0;int t=2;while(flagList<=list.size()){index = 0;if(i<65534){for(String  name :headList){sheet.addCell(new Label(index, t, list.get(flagList-1).get(name)+""));index++;}i++;t++;flagList++;}else{break;}}}}book.write();    if (book != null)book.close();}public static void main(String[] args) {List<Map<String, Object>> list=new ArrayList<>();for(int i=0;i<195534;i++){Map<String, Object> map=new HashMap<>();map.put("a","a"+i );map.put("b","b"+i  );map.put("c","c"+i  );map.put("d","d"+i  );list.add(map);}List<String> ll=new ArrayList<>();ll.add("a");ll.add("b");ll.add("c");ll.add("d");try {CreateExcelFile(list, new File("d:/a.xls"),ll,"hahaha");} catch (WriteException e) {// TODO Auto-generated catch block
            e.printStackTrace();} catch (IOException e) {
//            // TODO Auto-generated catch block
            e.printStackTrace();}}}

生成了exle之后就是传动了

package com.asiainfo.easyconfig2.util;import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;import javax.servlet.http.HttpServletResponse;/*** @author guohy**/
public class DownLoad {/*** 文件下载* * @param filepath*            文件路径* @param response*/public void send(String filepath, HttpServletResponse response) {try {File file = new File(filepath);// path是文件地址String filename = file.getName();// 获取日志文件名称InputStream fis = new BufferedInputStream(new FileInputStream(filepath));byte[] buffer = new byte[fis.available()];fis.read(buffer);fis.close();response.reset();// 先去掉文件名称中的空格,然后转换编码格式为utf-8,保证不出现乱码,这个文件名称用于浏览器的下载框中自动显示的文件名response.addHeader("Content-Disposition", "attachment;filename="+ new String(filename.replaceAll(" ", "").getBytes("utf-8"),"iso8859-1"));response.addHeader("Content-Length", "" + file.length());OutputStream os = new BufferedOutputStream(response.getOutputStream());response.setContentType("application/octet-stream");os.write(buffer);// 输出文件os.flush();os.close();} catch (Exception e) {e.printStackTrace();}}
}

  在controller中调一下这个方法即可,前端会识别出这个流,调出浏览器的下载弹框。

转载于:https://www.cnblogs.com/mengzhongyunying/p/8721394.html

java中让数据生成excle文件并且支持下载相关推荐

  1. java 将map写入文件_如何将java中map数据写入txt文件中

    用户提问 例如 static{ //初始化默认用户 user.setUsername("admin"); user.setPassword("123"); tr ...

  2. java 字符串转pdf文件_java中根据模板生成pdf文件

    原标题:java中根据模板生成pdf文件 阅读目录 简介 业务需求 引入jar包 pdf模板文件与方法参数 代码部分 总结归纳 回到顶部 简介 本文使用java引入apach提供的pdf操作工具生成p ...

  3. C# 将List中的数据导入csv文件中

    //http://www.cnblogs.com/mingmingruyuedlut/archive/2013/01/20/2849906.html C# 将List中的数据导入csv文件中 将数据保 ...

  4. java在linux生成pdf文件,从 Java 应用程序动态生成 PDF 文件

    简介: 如果您的应用程序需要动态生成 PDF 文档,那么您需要 iText 库.开源的 iText 库使得 PDF 的创建变得轻松易行.本文介绍了 iText 并提供了一个使用它从 Java 技术应用 ...

  5. Java中IO(一、文件流)

    文件流 FileInputStream从文件系统中的文件获取输入字节.哪些文件可用取决于主机环境. FileInputStream用于读取原始字节流,如图像数据.要读取字符流,请考虑使用FileRea ...

  6. Java中API的两个文件CHM和CHW

    Java中API的两个文件CHM和CHW API.CHM: CHM(Compiled Help Manual)叫已编译的帮助文件,可以通过它来查找你想要的类和方法 API.CHW: CHW文件是索引文 ...

  7. Java中“/”,“.”所代表的文件路径

    转载自  Java中"/","."所代表的文件路径 我们在开发的过程中,经常会去读.写文件.在读写文件的时候,就不得不写文件的路径,使用相对路径的方式有两种:& ...

  8. excel使用MySQL数据,如何使用mysql完成excel中的数据生成

    Excel是数据分析中最常用的工具,本篇文章通过mysql与excel的功能对比介绍如何使用mysql完成excel中的数据生成,数据清洗,预处理,以及最常见的数据分类,数据筛选,分类汇总,以及数据透 ...

  9. java指令导出data文件_直接用 java 命令行动态生成jpg文件 (转)

    直接用 java 命令行动态生成jpg文件 (转)[@more@] /** * jeruGraphics v 1.0 * * 看到一些动态生成图象的例子都是完成的, * 而且很长,觉得不是无论从实用性 ...

最新文章

  1. 项目ITP(五) spring4.0 整合 Quartz 实现任务调度
  2. 误差向量幅度(EVM)介绍
  3. android 资源如何下沉,关于Android业务模块下沉的一些实践及总结
  4. linux常用网络命令ping和arping
  5. SpringAOP的几大通知
  6. 【HNOI2017】影魔
  7. python生成个性二维码学习笔记
  8. GreenDroid 开源UI组件
  9. nfs总结之生产实例
  10. Loadrunner报错汇总
  11. scholarscope不显示影响因子_你的pubmed又不能显示影响因子了,因为 ……
  12. 耐得住寂寞方能不寂寞
  13. BZOJ P1189[HNOI2007]紧急疏散evacuate
  14. 教你如何编写游戏外挂
  15. 教你去掉 U盘写保护
  16. 机器学习-疑点1 :shape[ ]的理解
  17. 前端~css~基准线与行高、行距和半行距/圆角矩形~
  18. 推荐12个优质技术公众号!
  19. 阿里巴巴1688诚信通通过市场全面分析选品策略
  20. 阿迪达斯携手麦当劳推出篮球明星鞋服;拜耳联合导师计划支持中国医药初创企业 | 美通企业日报...

热门文章

  1. 每天一道LeetCode-----计算直方图中最大矩形的面积
  2. mybatis里的日志实现顺序
  3. php json返回sql,php – 如何从我的特定SQL查询中返回json?
  4. 开始体验Kali Linux
  5. EditPlus3.21注册码
  6. pytorch 之 保存不同形式的预训练模型
  7. LTS计算机,Pine64单板计算机(SBC)和模块入门
  8. 在Ubuntu中实验环境配置《操作系统原理与实践-李治军》
  9. python 通信中间件_apachemiddleware-有用的Python中间件,用于mod\wsgi部署-James Gardner 0.1.1 0.1.0...
  10. kl散度度量分布_论“邻里关系”的学问:度量和改进图信息在图神经网络中的使用 | AI Time PhD ICLR...