spring+struts+mybatis中关于导出Excel数据

 

 前端页面主要获取ID数组值,并且将ID数组值传到后台并获取后台传来的是数据导出Excel

1.function diaoboExport(){ 2 var Export=true; 3 var ids=""; 4 $("input[name='ids']:checked").each(function(){ 5 ids += this.value + "," ; 6 }); 7 ids = ids.substring(0,ids.length-1); 8 $("input[name='ids']:checked").each(function(){ 9 var value = this.value; 10 $.each(function(i,va){ 11 if(va.id == value){ 12 //console.log(va.id+"...."+va.acceptStatus); 13 if(va.devStatus != 5){ 14 Export = false; 15 return false; 16 } 17 } 18 }); 19 }); 20 if(Export){ 21 $.ajax({ 22 type:"post", 23 url:"allot/allotExportAllot", 24 data:{ids:ids}, 25 cache:false, 26 success:function(data){ 27 $("input[name='ids']:checked").each(function(){ 28 this.checked = false; 29 }); 30 window.location.href = "<%=basePath%>download/设备调拨单.xlsx"; 31 } 32 }); 33 } 34 }

Action:主要接收前面页面传来的ID值并调用serviceImpl方法

 1 /**
 2      * 导出调拨表数据
 3      * */
 4     public void allotExport(){
 5         try {
 6             if(ids!=null &&!"".equals(ids)){
 7                 deviceService.AddExportDiaobo(ids);
 8             }else{
 9                 throw new Exception("导出传递调拨的ID值无效");
10             }
11         } catch (Exception e) {
12             e.printStackTrace();
13         }
14     }

ServiceImpl:主要讲传来的ID数组解析放到数据库查询出所有数据,并调用导出方法

 1 public void AddExportDiaobo(String ids) throws Exception{
 2         Map<String, Object> params = new HashMap<String, Object>();
 3         List<Integer> idss = new ArrayList<Integer>();
 4         String[] result = ids.split(",");
 5         for(int j =0;j<result.length;j++){
 6             idss.add(Integer.parseInt(result[j].trim()));
 7         }
 8         params.put("list", idss);
 9         List<Device> device3 = deviceDao.listDeviceByIds(params);
10         if(device3!=null){
11             AllotExcelExport.exportAllotExport(device3);
12         }else{
13             throw new Exception("调拨表传来的ID值无效");
14         }
15     }

Export:将已有的调拨模板copy,并将得到的值放到sheet中,导出数据

 1 public class AllotExcelExport {
 2   public static void exportAllotExport(List<Device> list) throws Exception{
 3       try {
 4         //定义路径
 5           String realpath= ServletActionContext.getServletContext().getRealPath( 
 6             "/download/");
 7            File file=new File(new File(realpath),"设备调拨单.xlsx");
 8            if(file.getParentFile().exists()){
 9                file.getParentFile().mkdirs();
10            }
11            File file1=new File(realpath+"/temp/设备调拨单-模板.xlsx");
12            if(file1.exists()){
13                FileUtils.copyFile(file1,file);
14                FileInputStream input = new FileInputStream(file);
15                XSSFWorkbook wb = new XSSFWorkbook(new BufferedInputStream(input));
16                XSSFSheet sheet = wb.getSheetAt(0);
17                
18                for (int i = 0; i < list.size(); i++) {
19                    if(sheet.getRow(i+2)==null ||  sheet.getRow(i+2).getCell(0)==null){
20                        continue;
21                    }
22                    
23                    sheet.getRow(i+2).getCell(0).setCellValue(list.get(i).getId());//序号
24                    sheet.getRow(i+2).getCell(1).setCellValue(list.get(i).getDataCode()==null?"":list.get(i).getDataCode());//IT资产编号
25                    sheet.getRow(i+2).getCell(2).setCellValue(list.get(i).getAssetCode()==null?"":list.get(i).getAssetCode());//固定资产编号                  
26                    sheet.getRow(i+2).getCell(3).setCellValue(list.get(i).getName()==null?"":list.get(i).getName());//设备名称
27                    sheet.getRow(i+2).getCell(4).setCellValue(list.get(i).getDeviceTypeM()==null?"":list.get(i).getDeviceTypeM().getName());//设备型号
28                    sheet.getRow(i+2).getCell(5).setCellValue(list.get(i).getHasGone()==null?"":list.get(i).getHasGone());//设备去向
29                    sheet.getRow(i+2).getCell(6).setCellValue(list.get(i).getAllotTime()==null?"":DateTimeUtil.DatetoString_ymd(list.get(i).getAllotTime()));//调拨时间  
30             }
31                FileOutputStream outputStream = new FileOutputStream(file);
32                 wb.write(outputStream);
33                 outputStream.close(); 
34                
35            }else{
36                System.out.println("源文件不存在");
37                 throw new Exception();
38            }
39     }catch (FileNotFoundException e) {
40         // TODO: handle exception
41         e.printStackTrace();
42     }catch (IOException e) {
43         // TODO 自动生成的 catch 块
44         e.printStackTrace();
45     }
46   }
47 }

此种导出数据Excel可能比较使用用于懒人方法,更多导出方法欢迎评论留下您的足迹!

转载于:https://www.cnblogs.com/jbml-154312/p/6893928.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值