Spring Boot+AngularJS导出excel

这次在项目开发中,需要实现考题导出,由于本项目是使用spring Boot+AngularJS的开发模式,原来那种表单式的请求方式不是很便捷,以下是基于AngularJs异步请求的代码。


首先是JS代码

[javascript]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. function exportExam() {  
  2.             $http({  
  3.                 url: url + "export",  
  4.                 responseType: 'arraybuffer'  
  5.             }).success(function (res) {  
  6.                 var blob = new Blob([res], {type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;"});  
  7.                 //var blob = new Blob([res], {type: "application/octet-stream"});  这种类型的转换同样可行  
  8.                 saveAs(blob, "考题信息.xlsx");  
  9.             });  
  10.         }  

这里需要注意三点:1、要在http的请求中将responseType设置为‘arraybuffer’类型,因为后台会将生成的excel转换成Byte数组并传送到前台

    2、经过博主测试,Blob的类型为以上两种都可以

    3、为了更好的兼容浏览器,我们在这里使用了saveAs方法,这个方法是file-saver这个插件提供的



后台代码

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. public byte[] export(Integer[] idlist, int[] isoutput,  
  2.             String[] title) throws IOException {  
  3.         Map model = new HashMap();  
  4.         model.put("excelName""考题信息.xlsx");  
  5.         model.put("sheetName""sheet1");  
  6.         model.put("title", title);  
  7.   
  8.         if (idlist == null || idlist.length == 0)  
  9.             model.put("list", repo.findAll());  
  10.         else {  
  11.             List<Integer> ids = new ArrayList<Integer>();  
  12.             for (int i = 0; i < idlist.length; i++)  
  13.                 ids.add(idlist[i]);  
  14.             model.put("list", repo.findAll(ids));  
  15.         }  
  16.   
  17.         model.put("isoutput", isoutput);  
  18.   
  19.         XSSFWorkbook book = new XSSFWorkbook();  
  20.   
  21.         XSSFSheet sheet = book.createSheet(model.get("sheetName").toString());  
  22.         XSSFRow header = sheet.createRow(0);  
  23.   
  24.         // 产生标题列  
  25.         int j = 0;  
  26.         for (int i = 0; i < title.length; i++) {  
  27.             if (isoutput[i] == 1) header.createCell(j++).setCellValue(title[i]);  
  28.         }  
  29.         int rowNum = 1;  
  30.         for (Exam exam : (List<Exam>) model.get("list")) {  
  31.             j = 0;  
  32.             XSSFRow row = sheet.createRow(rowNum++);  
  33.             if (isoutput[0] == 1) {  
  34.                 row.createCell(j).setCellValue(exam.getTest());  
  35.                 j++;  
  36.             }  
  37.             if (isoutput[1] == 1) {  
  38.                 row.createCell(j).setCellValue(exam.getOp1());  
  39.                 j++;  
  40.             }  
  41.             if (isoutput[2] == 1) {  
  42.                 row.createCell(j).setCellValue(exam.getOp2());  
  43.                 j++;  
  44.             }  
  45.             if (isoutput[3] == 1) {  
  46.                 row.createCell(j).setCellValue(exam.getOp3());  
  47.                 j++;  
  48.             }  
  49.             if (isoutput[4] == 1) {  
  50.                 row.createCell(j).setCellValue(exam.getOp4());  
  51.                 j++;  
  52.             }  
  53.             if (isoutput[5] == 1) {  
  54.                 row.createCell(j).setCellValue(exam.getAnswer());  
  55.                 j++;  
  56.             }  
  57.             if (isoutput[6] == 1) {  
  58.                 row.createCell(j).setCellValue(exam.getTotal());  
  59.                 j++;  
  60.             }  
  61.             if (isoutput[7] == 1) {  
  62.                 row.createCell(j).setCellValue(exam.gettotalCorrect());  
  63.                 j++;  
  64.             }  
  65.             if (isoutput[8] == 1) {  
  66.                 row.createCell(j).setCellValue(  
  67.                         (double) exam.gettotalCorrect() / exam.getTotal());  
  68.                 j++;  
  69.             }  
  70.         }  
  71.   
  72.         ByteArrayOutputStream output = new ByteArrayOutputStream();  
  73.         book.write(output);  
  74.         byte[] b = output.toByteArray();  
  75.         return b;  
  76.     }  

由于这部分代码经过各种修改,还没有整理好,这里大概说下思路。首先上半部分是查询后台数据并生成xlsx文件,也就是book对象;然后book.write方法将book转化为ByteArraryOutputStream,由于异步请求是通过Json来与前台交互的,因此不能直接传输ByteArraryOutputStream,所以有将它转换为byte数组。


以上是主要代码,经测试,该文件导出功能在Chrome、IE、猎豹浏览器中都能正常导出。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值