做者:Howie_Y
连接:https://juejin.im/post/5ab4799451882521d6577fe4git
最近的项目中遇到了一个将数据库的信息导入到一个 Excel 文件的需求,并且还要提供下载该 Excel 文件的接口 ,搞定以后,进行了一下总结,但愿给你们带来帮助github
源码: https://github.com/HowieYuan/Excel-Downloadweb
<!-- https://mvnrepository.com/artifact/net.sourceforge.jexcelapi/jxl -->
<dependency>
<groupId>net.sourceforge.jexcelapi</groupId>
<artifactId>jxl</artifactId>
<version>2.6.12</version>
</dependency>数据库
咱们须要用到 jxl 包的类,而 jxl.jar 正是操做 excel 表格的工具类库,除了 jxl 之外,poi 包也是一个 操做 excel 的类库。
而对比两个包,jxl 更适用与数据量大的状况,而 poi 在数据量不高(大约5000之内)时,效率较高,但占用内存大,更容易内存溢出。api
private int id;
private String name;
private int age;
private String gender;
public List<Person> getPersonList() { List<Person> list = new ArrayList<>(); list.add(new Person(1, "Howie", 20, "female")); list.add(new Person(2, "Wade", 25, "male")); list.add(new Person(3, "Duncan", 30, "male")); list.add(new Person(4, "Kobe", 35, "male")); list.add(new Person(5, "James", 40, "male")); return list; }
//建立文件本地文件
//直接将文件建立在项目目录中
String filePath = "人员数据.xlsx"; File dbfFile = new File(filePath); //使用 Workbook 类的工厂方法建立一个可写入的工做薄(Workbook)对象 WritableWorkbook wwb = Workbook.createWorkbook(dbfFile); //若是文件不存在,则建立一个新的文件 if (!dbfFile.exists() || dbfFile.isDirectory()) { dbfFile.createNewFile(); } //得到人员信息 list (PersonFactroy 类已经被依赖注入) List<Person> list = personFactroy.getPersonList(); //建立一个可写入的工做表 WritableSheet ws = wwb.createSheet("列表 1", 0); //添加excel表头 ws.addCell(new Label(0, 0, "序号")); ws.addCell(new Label(1, 0, "姓名")); ws.addCell(new Label(2, 0, "年龄")); ws.addCell(new Label(3, 0, "性别")); int index = 0; for (Person person : list) { //将生成的单元格添加到工做表中 //(这里须要注意的是,在Excel中,第一个参数表示列,第二个表示行) ws.addCell(new Label(0, index + 1, String.valueOf(person.getId()))); ws.addCell(new Label(1, index + 1, person.getName())); ws.addCell(new Label(2, index + 1, String.valueOf(person.getAge()))); ws.addCell(new Label(3, index + 1, person.getGender())); index++; }
//前面的代码一致
//每一个工做表格最多存储2条数据(注:excel表格一个工做表能够存储65536条)
int mus = 2;
//数据的总大小
int totle = list.size();
//总表格数
int avg = totle / mus + 1;
for (int i = 0; i < avg; i++) { //建立一个可写入的工做表 WritableSheet ws = wwb.createSheet("列表" + (i + 1), i); //添加excel表头 ws.addCell(new Label(0, 0, "序号")); ws.addCell(new Label(1, 0, "姓名")); ws.addCell(new Label(2, 0, "年龄")); ws.addCell(new Label(3, 0, "性别")); int num = i * mus; int index = 0; for (int m = num; m < list.size(); m++) { //判断index == mus的时候跳出当前for循环 if (index == mus) { break; } Person person = list.get(m); //将生成的单元格添加到工做表中 //(这里须要注意的是,在Excel中,第一个参数表示列,第二个表示行) ws.addCell(new Label(0, index + 1, String.valueOf(person.getId()))); ws.addCell(new Label(1, index + 1, person.getName())); ws.addCell(new Label(2, index + 1, String.valueOf(person.getAge()))); ws.addCell(new Label(3, index + 1, person.getGender())); index++; } }
这里是根据每一个表的数据量来分,你们也能够根据其中一个属性等等来分红各个表格bash
该方法利用文件流来写入文件,方法类型为 void,不须要 return,除此以外,接口参数中须要添加上 HttpServletResponseapp
@RequestMapping(value = "/getExcel", method = RequestMethod.GET) public void createBoxListExcel(HttpServletResponse response) throws Exception { String filePath = "人员数据.xlsx"; /** * 这部分是刚刚导入 Excel 文件的代码,省略 */ String fileName = new String("人员数据.xlsx".getBytes(), "ISO-8859-1"); //设置文件名 response.addHeader("Content-Disposition", "filename=" + fileName); OutputStream outputStream = response.getOutputStream(); FileInputStream fileInputStream = new FileInputStream(filePath); byte[] b = new byte[1024]; int j; while ((j = fileInputStream.read(b)) > 0) { outputStream.write(b, 0, j); } fileInputStream.close(); outputStream.flush(); outputStream.close(); }
而后,咱们直接在地址栏输入localhost:8080/getExcel
既可马上下载你的文件工具
注意: String fileName = new String("人员信息.xlsx".getBytes(), "ISO-8859-1");
我使用了 ISO-8859-1 编码,缘由是 ISO-8859-1编码是单字节编码,向下兼容 ASCII,而 Header 中只支持 ASCII,传输的文件名必须是 ASCIIpost