1. 程式人生 > >項目筆記:導出Excel功能分sheet頁插入數據

項目筆記:導出Excel功能分sheet頁插入數據

mkdir system getpara creates service posit ade quest mst

  導出Excel功能分sheet頁處理數據:

/*導出EXCEL*/
public void createExcel() {
    log.info("導出Excel功能已經啟動-BEGIN");
    JxlUtil jsl = new JxlUtil();
    List<Device> dataList =new ArrayList<Device>();
    List<DeviceExport> list = new ArrayList<DeviceExport>();
    // 構建路徑
    String downLoadPath = "
/WEB-INF/download/asset/"; String rootPath = getServletContext().getRealPath(downLoadPath); String fileName = ""; File file=new File(rootPath); try { if(!(file.exists()||file.isDirectory())){ file.mkdirs(); } if (null != getRequest().getParameter("
ids")) { String ids[] = getRequest().getParameter("ids").split(","); for (int i = 0; i < ids.length; i++) { if(null!=ids[i]){ device = deviceService.queryById(ids[i].trim()); dataList.add(device); } } }
else{ dataList = deviceService.queryForExcel(); } if(dataList!=null){ for (Device device : dataList) { DeviceExport deviceExport= new DeviceExport(); if(null!=device.getId()){ deviceExport.setId(device.getId()); } if(null!=device.getIp()){ deviceExport.setIp(device.getIp()); } if(null!=device.getMac()){ deviceExport.setMac(device.getMac()); } if(null!=device.getName()){ deviceExport.setName(device.getName()); } if(null!=device.getOrganization()){ Organization organization=device.getOrganization(); String Aname =organization.getName(); String name= getAname(organization, Aname); deviceExport.setOrganizationName(name); } if(null!=device.getRegState()){ deviceExport.setRegState(device.getRegState()); } if(null!=device.getUser()){ deviceExport.setUserName(device.getUser().getName()); } if(null!=device.getProtectState()){ deviceExport.setProtectState(device.getProtectState()); } if(null!=device.getIsOpened()){ deviceExport.setIsOpened(device.getIsOpened()); } String osName = MessageUtils.getMessage(device.getOs().getName()); deviceExport.setOsName(osName); String deviceType = MessageUtils.getMessage(device.getDeviceType().getName()); deviceExport.setDeviceTypeName(deviceType); list.add(deviceExport); } } String interBase = "sys.column.name.device"; //String inter_value_key = "#isOpened#roamState#protectState#"; String inter_value_key = "#isOpened#protectState#regState#"; String[] inter_value_ary = { "isOpened.0", "isOpened.1", "protectState.0", "protectState.1","regState.0","regState.1"}; //導出不顯示漫遊狀態 //String[] inter_value_ary = { "isOpened.0", "isOpened.1","roamState.0", "roamState.1","protectState.0", "protectState.1"}; fileName = jsl.getInter(interBase.replace("column", "table")) + new Date().getTime(); String targetfile = rootPath + System.getProperty("file.separator") + fileName + ".xls"; //分sheet頁處理 int total = dataList.size();//總數 int max = 5000;//每sheet頁允許最大數 int avg = total / max;//sheet頁個數 // 創建可寫入的Excel工作薄 WritableWorkbook wwb; wwb = Workbook.createWorkbook(new File(targetfile)); for(int i=0;i<avg+1;i++){ // 創建Excel工作表 WritableSheet ws = wwb.createSheet("已註冊設備"+(i+1), i); int num = i * max; int index = 0; List<DeviceExport> exportList = new ArrayList<DeviceExport>(); for(int m = num; m < total; m++){//m即為每個sheet頁應該開始的數 if(index == max){//判斷 index = max 的時候跳出裏層的for循環 break; } DeviceExport deviceExport=list.get(m); exportList.add(deviceExport);//從總的list數據裏面取出該處於哪個sheet頁的數據,然後加進exportList,exportList即為當前sheet頁應該有的數據 index++; } // 獲取需要內容國際化的字段 jsl.creatDeviceExcel(ws, exportList, interBase, inter_value_key,inter_value_ary); } // 寫入Exel工作表 wwb.write(); // 關閉Excel工作薄對象 wwb.close(); getResponse().setContentType(getServletContext().getMimeType(fileName)); getResponse().setHeader("Content-Disposition", "attachment;fileName="+new String(fileName.getBytes("gb2312"), "ISO8859-1")+".xls"); String fullFileName = getServletContext().getRealPath(downLoadPath + fileName+ ".xls"); InputStream in = new FileInputStream(fullFileName); OutputStream out = getResponse().getOutputStream(); int b; while((b=in.read())!= -1){ out.write(b); } in.close(); out.close(); /*ServletActionContext.getRequest().setAttribute("downLoadPath", downLoadPath); ServletActionContext.getRequest().setAttribute("fileName", fileName + ".xls");*/ this.msg = RESULT_SUCCESS; log.info("導出EXCEL提示信息為:"+this.msg); } catch (Exception e) { log.error("導出EXCEL失敗:" + e.getMessage()); } log.info("導出Excel功能已經啟動-END"); /*return "downLoadUI";*/ }

項目筆記:導出Excel功能分sheet頁插入數據