List of usage examples for org.apache.poi.hssf.usermodel HSSFWorkbook HSSFWorkbook
public HSSFWorkbook()
From source file:com.dawg6.web.dhcalc.server.ExportExcel.java
License:Open Source License
public byte[] export() throws Exception { wb = new HSSFWorkbook(); boldFont = wb.createFont();//from w ww. j av a 2 s.co m boldFont.setFontHeightInPoints((short) 10); boldFont.setFontName("Arial"); boldFont.setBoldweight(Font.BOLDWEIGHT_BOLD); boldStyle = wb.createCellStyle(); boldStyle.setFont(boldFont); doubleStyle = wb.createCellStyle(); doubleFormat = wb.createDataFormat(); doubleStyle.setDataFormat(doubleFormat.getFormat("#,##0.0###")); timeStyle = wb.createCellStyle(); timeFormat = wb.createDataFormat(); timeStyle.setDataFormat(timeFormat.getFormat("0.00")); largeDoubleStyle = wb.createCellStyle(); largeDoubleFormat = wb.createDataFormat(); largeDoubleStyle.setDataFormat(largeDoubleFormat.getFormat("#,###")); intStyle = wb.createCellStyle(); intFormat = wb.createDataFormat(); intStyle.setDataFormat(intFormat.getFormat("#,##0")); pctStyle = wb.createCellStyle(); pctFormat = wb.createDataFormat(); pctStyle.setDataFormat(pctFormat.getFormat("0.00%")); inputs = wb.createSheet("Inputs"); summary = wb.createSheet("Summary"); HSSFSheet damageLog = wb.createSheet("DamageLog"); HSSFSheet typeSummary = wb.createSheet("DamageTypeSummary"); HSSFSheet skillSummary = wb.createSheet("SkillSummary"); HSSFSheet shooterSummary = wb.createSheet("ShooterSummary"); addInputs(); addSummary(); addDamageLog(damageLog); addTypeSummary(typeSummary); addSkillSummary(skillSummary); addShooterSummary(shooterSummary); ByteArrayOutputStream stream = new ByteArrayOutputStream(); wb.write(stream); return stream.toByteArray(); }
From source file:com.dayuan.action.BusFileAction.java
/**???Excel*/ @RequestMapping("/exportExcel") public ModelAndView exportExcel(HttpServletRequest request, HttpServletResponse response) { Map<String, Object> context = getRootMap(); SysUser user = SessionUtils.getUser(request); BusFileModel busFileModel = new BusFileModel(); if (SuperAdmin.YES.key != user.getSuperAdmin() && user.getExcelAuth() == 0) { //busFileModel.setlUserName(user.getNickName()); busFileModel.setlUId(user.getId().toString()); }// w w w . ja v a 2 s .c o m busFileModel.setRows(500); try { List<BusFiles> list = busFileService.queryByList(busFileModel); if (list != null && list.size() > 0) { // webbookExcel HSSFWorkbook wb = new HSSFWorkbook(); // webbooksheet,Excelsheet HSSFSheet sheet = wb.createSheet("?"); HSSFSheet sheetLoan = wb.createSheet("?"); // sheet0,??poiExcel?short HSSFRow row = sheet.createRow((int) 0); HSSFRow rowLoan = sheetLoan.createRow((int) 0); // ? HSSFCellStyle style = wb.createCellStyle(); // ? style.setAlignment(HSSFCellStyle.ALIGN_CENTER); /**sheet1 ?*/ HSSFCell cell = row.createCell((short) 0); cell.setCellValue("??"); cell.setCellStyle(style); cell = row.createCell((short) 1); cell.setCellValue("??");//busfiles.lUserName cell.setCellStyle(style); cell = row.createCell((short) 2); cell.setCellValue("??");//busLoanInfo.applicationName cell.setCellStyle(style); cell = row.createCell((short) 3); cell.setCellValue("?"); //busfiles.lStatus cell.setCellStyle(style); cell = row.createCell((short) 4); cell.setCellValue(""); //busfiles.createTime cell.setCellStyle(style); cell = row.createCell((short) 5); cell.setCellValue("??"); //busLoanInfo.channel cell.setCellStyle(style); cell = row.createCell((short) 6); cell.setCellValue("?"); //buslending.loanAmount cell.setCellStyle(style); cell = row.createCell((short) 7); cell.setCellValue("?"); //buslending.openingQuota cell.setCellStyle(style); cell = row.createCell((short) 8); cell.setCellValue(""); //busBiling.creditEndDate cell.setCellStyle(style); cell = row.createCell((short) 9); cell.setCellValue("???"); //busBiling.loanAccount cell.setCellStyle(style); cell = row.createCell((short) 10); cell.setCellValue("?"); //legal.deliveryAddress cell.setCellStyle(style); /**sheet2 ?*/ HSSFCell cellLoan = rowLoan.createCell((short) 0); cellLoan.setCellValue("??"); cellLoan.setCellStyle(style); cellLoan = rowLoan.createCell((short) 1); cellLoan.setCellValue("??");//busLoanInfo.applicationName cellLoan.setCellStyle(style); cellLoan = rowLoan.createCell((short) 2); cellLoan.setCellValue("?"); //busLoanInfo.urgentCont cellLoan.setCellStyle(style); cellLoan = rowLoan.createCell((short) 3); cellLoan.setCellValue("");//busLoanInfo.relationship cellLoan.setCellStyle(style); cellLoan = rowLoan.createCell((short) 4); cellLoan.setCellValue("?");//busLoanInfo.urgentContPhone cellLoan.setCellStyle(style); cellLoan = rowLoan.createCell((short) 5); cellLoan.setCellValue("?");//busLoanInfo.urgentContAddress cellLoan.setCellStyle(style); cellLoan = rowLoan.createCell((short) 6); cellLoan.setCellValue("??"); //biling.loanCardNumber cellLoan.setCellStyle(style); cellLoan = rowLoan.createCell((short) 7); cellLoan.setCellValue("1"); cellLoan.setCellStyle(style); cellLoan = rowLoan.createCell((short) 8); cellLoan.setCellValue("?1"); cellLoan.setCellStyle(style); cellLoan = rowLoan.createCell((short) 9); cellLoan.setCellValue("???1"); cellLoan.setCellStyle(style); cellLoan = rowLoan.createCell((short) 10); cellLoan.setCellValue("?1"); cellLoan.setCellStyle(style); cellLoan = rowLoan.createCell((short) 11); cellLoan.setCellValue("2"); cellLoan.setCellStyle(style); cellLoan = rowLoan.createCell((short) 12); cellLoan.setCellValue("?2"); cellLoan.setCellStyle(style); cellLoan = rowLoan.createCell((short) 13); cellLoan.setCellValue("???2"); cellLoan.setCellStyle(style); cellLoan = rowLoan.createCell((short) 14); cellLoan.setCellValue("?2"); cellLoan.setCellStyle(style); cellLoan = rowLoan.createCell((short) 15); cellLoan.setCellValue(""); cellLoan.setCellStyle(style); cellLoan = rowLoan.createCell((short) 16); cellLoan.setCellValue("??"); cellLoan.setCellStyle(style); cellLoan = rowLoan.createCell((short) 17); cellLoan.setCellValue("????"); cellLoan.setCellStyle(style); cellLoan = rowLoan.createCell((short) 18); cellLoan.setCellValue("??"); cellLoan.setCellStyle(style); cellLoan = rowLoan.createCell((short) 19); cellLoan.setCellValue("??"); cellLoan.setCellStyle(style); cellLoan = rowLoan.createCell((short) 20); cellLoan.setCellValue("?"); cellLoan.setCellStyle(style); for (int i = 0; i < list.size(); i++) { BusFiles busFiles = list.get(i); Integer lId = busFiles.getId(); BusLoanInfo busLoanInfo = busLoanInfoService.queryByLId(lId); BusLending busLending = busLendingService.queryByBId(lId); BusBiling busBiling = busBilingService.queryByBId(lId); BusLoanInfoLegal legal = busLoanInfoLegalService.getBusLoanInfoLegal(lId); List<BusLoanInfoShop> shopList = busLoanInfoShopService.queryListByBId(lId); /**rowsheet1*/ row = sheet.createRow((int) 1 + i); row.createCell((short) 0).setCellValue(i + 1); row.createCell((short) 1).setCellValue(busFiles.getlUserName()); row.createCell((short) 3).setCellValue(busFiles.getlStatus()); row.createCell((short) 4).setCellValue( DateUtil.getFormattedDateUtil((Date) busFiles.getCreateTime(), "yyyy-MM-dd HH:mm:ss")); /**rowLoansheet2*/ rowLoan = sheetLoan.createRow((int) 1 + i); rowLoan.createCell((short) 0).setCellValue(i + 1); if (busLoanInfo != null) { row.createCell((short) 2).setCellValue(busLoanInfo.getApplicationName()); row.createCell((short) 5).setCellValue(busLoanInfo.getChannel()); rowLoan.createCell((short) 1).setCellValue(busLoanInfo.getApplicationName()); rowLoan.createCell((short) 2).setCellValue(busLoanInfo.getUrgentCont()); rowLoan.createCell((short) 3).setCellValue(busLoanInfo.getRelationship()); rowLoan.createCell((short) 4).setCellValue(busLoanInfo.getUrgentContPhone()); rowLoan.createCell((short) 5).setCellValue(busLoanInfo.getUrgentContAddress()); } else { row.createCell((short) 2).setCellValue(""); row.createCell((short) 5).setCellValue(""); rowLoan.createCell((short) 1).setCellValue(""); rowLoan.createCell((short) 2).setCellValue(""); rowLoan.createCell((short) 3).setCellValue(""); rowLoan.createCell((short) 4).setCellValue(""); rowLoan.createCell((short) 5).setCellValue(""); } if (busLending != null) { row.createCell((short) 6).setCellValue(busLending.getLoanAmount()); row.createCell((short) 7).setCellValue(busLending.getOpeningQuota()); } else { row.createCell((short) 6).setCellValue(""); row.createCell((short) 7).setCellValue(""); } if (busBiling != null) { row.createCell((short) 8).setCellValue(busBiling.getCreditEndDate()); row.createCell((short) 9).setCellValue(busBiling.getLoanAccount()); rowLoan.createCell((short) 6).setCellValue(busBiling.getLoanCardNumber());//?? rowLoan.createCell((short) 15).setCellValue(DateUtil .getFormattedDateUtil((Date) busBiling.getCheckDate(), "yyyy-MM-dd HH:mm:ss"));// rowLoan.createCell((short) 16).setCellValue(busBiling.getCreditorIfNormal());//?? rowLoan.createCell((short) 17).setCellValue(busBiling.getGuarantorIfNormal());//???? rowLoan.createCell((short) 18).setCellValue(busBiling.getCloudLoanIfWarning());//?? rowLoan.createCell((short) 19).setCellValue(busBiling.getShopOperation());//?? rowLoan.createCell((short) 20).setCellValue(busBiling.getOtherNeedToExplained());//? } else { row.createCell((short) 8).setCellValue(""); row.createCell((short) 9).setCellValue(""); rowLoan.createCell((short) 6).setCellValue("");//?? rowLoan.createCell((short) 15).setCellValue("");// rowLoan.createCell((short) 16).setCellValue("");//?? rowLoan.createCell((short) 17).setCellValue("");//???? rowLoan.createCell((short) 18).setCellValue("");//?? rowLoan.createCell((short) 19).setCellValue("");//?? rowLoan.createCell((short) 20).setCellValue("");//? } if (legal != null) { row.createCell((short) 10).setCellValue(legal.getDeliveryAddress()); } else { row.createCell((short) 10).setCellValue(""); } if (shopList != null && shopList.size() > 0) { if (shopList.size() == 1) { BusLoanInfoShop shop = shopList.get(0); rowLoan.createCell((short) 7).setCellValue(shop.getShopName());// rowLoan.createCell((short) 8).setCellValue(shop.getPlatformName());//? rowLoan.createCell((short) 9).setCellValue(shop.getSubAccount());//??? rowLoan.createCell((short) 10).setCellValue(shop.getSbuPassword());//? rowLoan.createCell((short) 11).setCellValue("");// rowLoan.createCell((short) 12).setCellValue("");//? rowLoan.createCell((short) 13).setCellValue("");//??? rowLoan.createCell((short) 14).setCellValue("");//? } else if (shopList.size() == 2) { BusLoanInfoShop shop = shopList.get(0); rowLoan.createCell((short) 7).setCellValue(shop.getShopName());// rowLoan.createCell((short) 8).setCellValue(shop.getPlatformName());//? rowLoan.createCell((short) 9).setCellValue(shop.getSubAccount());//??? rowLoan.createCell((short) 10).setCellValue(shop.getSbuPassword());//? BusLoanInfoShop shop1 = shopList.get(1); rowLoan.createCell((short) 11).setCellValue(shop1.getShopName());// rowLoan.createCell((short) 12).setCellValue(shop1.getPlatformName());//? rowLoan.createCell((short) 13).setCellValue(shop1.getSubAccount());//??? rowLoan.createCell((short) 14).setCellValue(shop1.getSbuPassword());//? } } else { rowLoan.createCell((short) 7).setCellValue(""); rowLoan.createCell((short) 8).setCellValue(""); rowLoan.createCell((short) 9).setCellValue(""); rowLoan.createCell((short) 10).setCellValue(""); rowLoan.createCell((short) 11).setCellValue(""); rowLoan.createCell((short) 12).setCellValue(""); rowLoan.createCell((short) 13).setCellValue(""); rowLoan.createCell((short) 14).setCellValue(""); } } String savePath = request.getSession().getServletContext().getRealPath( File.separator + "WEB-INF" + File.separator + "downloads" + File.separator + "excelfiles");//??,?? File savePathFile = new File(savePath); if (!savePathFile.exists()) { savePathFile.mkdirs(); } savePath = savePath + File.separator + UUID.randomUUID();//? File fileSavePath = new File(savePath); /**??*/ if (fileSavePath.exists()) { if (fileSavePath.isDirectory()) { File[] files = fileSavePath.listFiles(); for (File file : files) { file.delete(); } fileSavePath.delete(); } else { fileSavePath.delete(); } fileSavePath.mkdirs(); } else { fileSavePath.mkdirs(); } String excel = "????" + DateUtil.getNowLongTime() + ".xls";//eccel?? BufferedOutputStream fout = new BufferedOutputStream( new FileOutputStream(savePath + File.separator + excel)); //excel wb.write(fout); //excel? fout.flush(); fout.close(); //MIME response.setContentType(request.getSession().getServletContext().getMimeType(excel)); //Content-Disposition response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(excel, "UTF-8")); BufferedInputStream in = new BufferedInputStream( new FileInputStream(savePath + File.separator + excel));//,io? OutputStream out = new BufferedOutputStream(response.getOutputStream()); byte buffer[] = new byte[1024]; int len = 0; while ((len = in.read(buffer)) > 0) { out.write(buffer, 0, len);//?response? } in.close(); out.flush(); out.close(); /***/ File file = new File(savePath + File.separator + excel); if (file != null) { if (file.exists()) { file.delete();// } file = null; } /***/ if (fileSavePath != null) { if (fileSavePath.exists()) { fileSavePath.delete(); } fileSavePath = null; } if (savePathFile != null) { savePathFile = null; } log.info("Excel?"); } } catch (Exception e) { //e.printStackTrace(); log.error("exportExcel" + e.getMessage()); context.put("message", "??"); return forword("message/message", context); } return null; }
From source file:com.dayuan.action.BusinessLoanAction.java
/** * ?excel/*w w w. ja v a 2 s . co m*/ * */ @RequestMapping("/exportAllExcel") public void exportAllExcel(HttpServletRequest request, HttpServletResponse response) throws Exception { SysUser user = SessionUtils.getUser(request); BusLoanInfoModel busLoanInfoModel = new BusLoanInfoModel(); busLoanInfoModel.setuId(user.getId().toString()); busLoanInfoModel.setuName(user.getNickName()); List<BusLoanInfo> list = busLoanInfoService.queryList(busLoanInfoModel); if (list != null && list.size() > 0) { // webbookExcel HSSFWorkbook wb = new HSSFWorkbook(); // webbooksheet,Excelsheet HSSFSheet sheet = wb.createSheet("???"); // sheet0,??poiExcel?short HSSFRow row = sheet.createRow((int) 0); // ? HSSFCellStyle style = wb.createCellStyle(); // ? style.setAlignment(HSSFCellStyle.ALIGN_CENTER); HSSFCell cell = row.createCell((short) 0); cell.setCellValue("??"); cell.setCellStyle(style); cell = row.createCell((short) 1); cell.setCellValue("??"); //applicationName cell.setCellStyle(style); cell = row.createCell((short) 2); cell.setCellValue("?");//urgentCont cell.setCellStyle(style); cell = row.createCell((short) 3); cell.setCellValue(""); // relationship cell.setCellStyle(style); cell = row.createCell((short) 4); cell.setCellValue("?"); //legalPhone cell.setCellStyle(style); cell = row.createCell((short) 5); cell.setCellValue("?"); //houseAddress cell.setCellStyle(style); cell = row.createCell((short) 6); cell.setCellValue("?"); //companyName cell.setCellStyle(style); cell = row.createCell((short) 7); cell.setCellValue("?");// ?platformName cell.setCellStyle(style); cell = row.createCell((short) 8); cell.setCellValue(""); //?shopName cell.setCellStyle(style); cell = row.createCell((short) 9); cell.setCellValue("???"); //?,subAccount cell.setCellStyle(style); cell = row.createCell((short) 10); cell.setCellValue("?"); //?,sbuPassword cell.setCellStyle(style); for (int i = 0; i < list.size(); i++) { BusLoanInfo busLoanInfo = list.get(i); Integer bid = busLoanInfo.getId(); BusLoanInfoLegal busLoanInfoLegal = this.busLoanInfoLegalService.getBusLoanInfoLegal(bid); BusLoanInfoShop busLoanInfoShop = this.busLoanInfoShopService.getBusLoanInfoShop(bid); row = sheet.createRow((int) 1 + i); row.createCell((short) 0).setCellValue(i + 1); row.createCell((short) 1).setCellValue(StringUtil.getNotNullStr(busLoanInfo.getApplicationName())); row.createCell((short) 2).setCellValue(StringUtil.getNotNullStr(busLoanInfo.getUrgentCont())); row.createCell((short) 3).setCellValue(StringUtil.getNotNullStr(busLoanInfo.getRelationship())); if (busLoanInfoLegal != null) { row.createCell((short) 4) .setCellValue(StringUtil.getNotNullStr(busLoanInfoLegal.getLegalPhone())); row.createCell((short) 5) .setCellValue(StringUtil.getNotNullStr(busLoanInfoLegal.getHouseAddress())); row.createCell((short) 6) .setCellValue(StringUtil.getNotNullStr(busLoanInfoLegal.getCompanyName())); } else { row.createCell((short) 4).setCellValue(""); row.createCell((short) 5).setCellValue(""); row.createCell((short) 6).setCellValue(""); } if (busLoanInfoShop != null) { row.createCell((short) 7) .setCellValue(StringUtil.getNotNullStr(busLoanInfoShop.getPlatformName())); row.createCell((short) 8).setCellValue(StringUtil.getNotNullStr(busLoanInfoShop.getShopName())); row.createCell((short) 9) .setCellValue(StringUtil.getNotNullStr(busLoanInfoShop.getSubAccount())); row.createCell((short) 10) .setCellValue(StringUtil.getNotNullStr(busLoanInfoShop.getSbuPassword())); } else { row.createCell((short) 7).setCellValue(""); row.createCell((short) 8).setCellValue(""); row.createCell((short) 9).setCellValue(""); row.createCell((short) 10).setCellValue(""); } } String savePath = request.getSession().getServletContext().getRealPath( File.separator + "WEB-INF" + File.separator + "downloads" + File.separator + "excelfiles");//??,?? savePath = savePath + File.separator + UUID.randomUUID();//? File fileSavePath = new File(savePath); /**??*/ if (fileSavePath.exists()) { if (fileSavePath.isDirectory()) { File[] files = fileSavePath.listFiles(); for (File file : files) { file.delete(); } fileSavePath.delete(); } else { fileSavePath.delete(); } fileSavePath.mkdirs(); } else { fileSavePath.mkdirs(); } String excel = "????" + DateUtil.getNowPlusTimeMill() + ".xls";//eccel?? BufferedOutputStream fout = new BufferedOutputStream( new FileOutputStream(savePath + File.separator + excel)); // wb.write(fout); //excel? fout.flush(); fout.close(); //MIME response.setContentType(request.getSession().getServletContext().getMimeType(excel)); //Content-Disposition response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(excel, "UTF-8")); BufferedInputStream in = new BufferedInputStream( new FileInputStream(savePath + File.separator + excel));//,io? OutputStream out = new BufferedOutputStream(response.getOutputStream()); byte buffer[] = new byte[1024]; int len = 0; while ((len = in.read(buffer)) > 0) { out.write(buffer, 0, len);//?response? } in.close(); out.flush(); out.close(); /***/ File file = new File(savePath + File.separator + excel); if (file != null) { if (file.exists()) { file.delete();// } file = null; } /***/ if (fileSavePath != null) { if (fileSavePath.exists()) { fileSavePath.delete(); } fileSavePath = null; } log.info("excel?"); } }
From source file:com.dayuan.action.BusinessLoanAction.java
/** * excel//from ww w . j a v a 2s . c o m * * */ @RequestMapping("/exportExcel") public void exportExcel(Integer id, HttpServletRequest request, HttpServletResponse response) throws Exception { if (id == null || id.equals("")) { log.error("?"); return; } BusLoanInfo busLoanInfo = this.busLoanInfoService.queryById(id); if (busLoanInfo == null) { log.error("busLoanInfo?"); return; } Integer bid = busLoanInfo.getId(); if (bid == null) { log.error("busLoanInfo?"); return; } BusLoanInfoLegal busLoanInfoLegal = this.busLoanInfoLegalService.getBusLoanInfoLegal(bid); BusLoanInfoShop busLoanInfoShop = this.busLoanInfoShopService.getBusLoanInfoShop(bid); // webbookExcel HSSFWorkbook wb = new HSSFWorkbook(); // webbooksheet,Excelsheet HSSFSheet sheet = wb.createSheet("???"); // sheet0,??poiExcel?short HSSFRow row = sheet.createRow((int) 0); // ? HSSFCellStyle style = wb.createCellStyle(); // ? style.setAlignment(HSSFCellStyle.ALIGN_CENTER); HSSFCell cell = row.createCell((short) 0); cell.setCellValue("??"); cell.setCellStyle(style); cell = row.createCell((short) 1); cell.setCellValue("??"); //applicationName cell.setCellStyle(style); cell = row.createCell((short) 2); cell.setCellValue("?");//urgentCont cell.setCellStyle(style); cell = row.createCell((short) 3); cell.setCellValue(""); // relationship cell.setCellStyle(style); cell = row.createCell((short) 4); cell.setCellValue("?"); //legalPhone cell.setCellStyle(style); cell = row.createCell((short) 5); cell.setCellValue("?"); //houseAddress cell.setCellStyle(style); cell = row.createCell((short) 6); cell.setCellValue("?"); //companyName cell.setCellStyle(style); cell = row.createCell((short) 7); cell.setCellValue("?");// ?platformName cell.setCellStyle(style); cell = row.createCell((short) 8); cell.setCellValue(""); //?shopName cell.setCellStyle(style); cell = row.createCell((short) 9); cell.setCellValue("???"); //?,subAccount cell.setCellStyle(style); cell = row.createCell((short) 10); cell.setCellValue("?"); //?,sbuPassword cell.setCellStyle(style); // ? ?? try { row = sheet.createRow((int) 0 + 1); row.createCell((short) 0).setCellValue(1); row.createCell((short) 1).setCellValue(StringUtil.getNotNullStr(busLoanInfo.getApplicationName())); row.createCell((short) 2).setCellValue(StringUtil.getNotNullStr(busLoanInfo.getUrgentCont())); row.createCell((short) 3).setCellValue(StringUtil.getNotNullStr(busLoanInfo.getRelationship())); if (busLoanInfoLegal != null) { row.createCell((short) 4).setCellValue(StringUtil.getNotNullStr(busLoanInfoLegal.getLegalPhone())); row.createCell((short) 5) .setCellValue(StringUtil.getNotNullStr(busLoanInfoLegal.getHouseAddress())); row.createCell((short) 6).setCellValue(StringUtil.getNotNullStr(busLoanInfoLegal.getCompanyName())); } else { row.createCell((short) 4).setCellValue(""); row.createCell((short) 5).setCellValue(""); row.createCell((short) 6).setCellValue(""); } if (busLoanInfoShop != null) { row.createCell((short) 7).setCellValue(StringUtil.getNotNullStr(busLoanInfoShop.getPlatformName())); row.createCell((short) 8).setCellValue(StringUtil.getNotNullStr(busLoanInfoShop.getShopName())); row.createCell((short) 9).setCellValue(StringUtil.getNotNullStr(busLoanInfoShop.getSubAccount())); row.createCell((short) 10).setCellValue(StringUtil.getNotNullStr(busLoanInfoShop.getSbuPassword())); } else { row.createCell((short) 7).setCellValue(""); row.createCell((short) 8).setCellValue(""); row.createCell((short) 9).setCellValue(""); row.createCell((short) 10).setCellValue(""); } // Student stu = (Student) list.get(i); // ? // cell = row.createCell((short) 3); // cell.setCellValue(new SimpleDateFormat("yyyy-mm-dd").format(stu.getBirth())); // row.createCell((short) 10).setCellValue(busLoanInfoShop.getSbuPassword());? String savePath = request.getSession().getServletContext().getRealPath( File.separator + "WEB-INF" + File.separator + "downloads" + File.separator + "excelfiles");//??,?? savePath = savePath + File.separator + UUID.randomUUID();//? File fileSavePath = new File(savePath); /**??*/ if (fileSavePath.exists()) { if (fileSavePath.isDirectory()) { File[] files = fileSavePath.listFiles(); for (File file : files) { file.delete(); } fileSavePath.delete(); } else { fileSavePath.delete(); } fileSavePath.mkdirs(); } else { fileSavePath.mkdirs(); } String excel = "????" + DateUtil.getNowPlusTimeMill() + ".xls";//eccel?? BufferedOutputStream fout = new BufferedOutputStream( new FileOutputStream(savePath + File.separator + excel)); // wb.write(fout); //excel? fout.flush(); fout.close(); //MIME response.setContentType(request.getSession().getServletContext().getMimeType(excel)); //Content-Disposition response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(excel, "UTF-8")); //,io? BufferedInputStream in = new BufferedInputStream( new FileInputStream(savePath + File.separator + excel)); OutputStream out = new BufferedOutputStream(response.getOutputStream()); byte buffer[] = new byte[1024]; int len = 0; while ((len = in.read(buffer)) > 0) { out.write(buffer, 0, len);//?response? } in.close(); out.flush(); out.close(); /***/ File file = new File(savePath + File.separator + excel); if (file != null) { if (file.exists()) { file.delete();// } file = null; } /***/ if (fileSavePath != null) { if (fileSavePath.exists()) { fileSavePath.delete(); } fileSavePath = null; } log.info("???excel?"); } catch (Exception e) { e.printStackTrace(); } }
From source file:com.dbumama.market.web.core.render.excel.PoiExporter.java
License:Apache License
public Workbook export() { Preconditions.checkNotNull(data, "data can not be null"); Preconditions.checkNotNull(headers, "headers can not be null"); Preconditions.checkNotNull(columns, "columns can not be null"); Preconditions.checkArgument(/* w ww . j ava 2s . c o m*/ data.length == sheetNames.length && sheetNames.length == headers.length && headers.length == columns.length, "data,sheetNames,headers and columns'length should be the same." + "(data:" + data.length + ",sheetNames:" + sheetNames.length + ",headers:" + headers.length + ",columns:" + columns.length + ")"); Preconditions.checkArgument(cellWidth >= 0, "cellWidth can not be less than 0"); Workbook wb; if (VERSION_2003.equals(version)) { wb = new HSSFWorkbook(); if (data.length > 1) { for (int i = 0; i < data.length; i++) { List<?> item = data[i]; Preconditions.checkArgument(item.size() < MAX_ROWS, "Data [" + i + "] is invalid:invalid data size (" + item.size() + ") outside allowable range (0..65535)"); } } else if (data.length == 1 && data[0].size() > MAX_ROWS) { data = dice(data[0], MAX_ROWS).toArray(new List<?>[] {}); String sheetName = sheetNames[0]; sheetNames = new String[data.length]; for (int i = 0; i < data.length; i++) { sheetNames[i] = sheetName + (i == 0 ? "" : (i + 1)); } String[] header = headers[0]; headers = new String[data.length][]; for (int i = 0; i < data.length; i++) { headers[i] = header; } String[] column = columns[0]; columns = new String[data.length][]; for (int i = 0; i < data.length; i++) { columns[i] = column; } } } else { wb = new XSSFWorkbook(); } if (data.length == 0) { return wb; } for (int i = 0; i < data.length; i++) { Sheet sheet = wb.createSheet(sheetNames[i]); Row row; Cell cell; if (headers[i].length > 0) { row = sheet.createRow(0); if (headerRow <= 0) { headerRow = HEADER_ROW; } headerRow = Math.min(headerRow, MAX_ROWS); for (int h = 0, lenH = headers[i].length; h < lenH; h++) { if (cellWidth > 0) { sheet.setColumnWidth(h, cellWidth); } cell = row.createCell(h); cell.setCellValue(headers[i][h]); } } for (int j = 0, len = data[i].size(); j < len; j++) { row = sheet.createRow(j + headerRow); Object obj = data[i].get(j); if (obj == null) { continue; } if (obj instanceof Map) { processAsMap(columns[i], row, obj); } else if (obj instanceof Model) { processAsModel(columns[i], row, obj); } else if (obj instanceof Record) { processAsRecord(columns[i], row, obj); } else { throw new RuntimeException("Not support type[" + obj.getClass() + "]"); } } } return wb; }
From source file:com.demo.common.extreme.view.XlsView.java
License:Apache License
public void beforeBody(TableModel model) { logger.debug("XlsView.init()"); moneyFormat = model.getPreferences().getPreference(PreferencesConstants.TABLE_EXPORTABLE + "format.money"); if (StringUtils.isEmpty(moneyFormat)) { moneyFormat = DEFAULT_MONEY_FORMAT; }// w w w .j av a 2 s.co m percentFormat = model.getPreferences() .getPreference(PreferencesConstants.TABLE_EXPORTABLE + "format.percent"); if (StringUtils.isEmpty(percentFormat)) { percentFormat = DEFAULT_PERCENT_FORMAT; } encoding = model.getExportHandler().getCurrentExport().getEncoding(); wb = new HSSFWorkbook(); sheet = wb.createSheet(); if (encoding.equalsIgnoreCase("UTF")) { wb.setSheetName(0, "Export Workbook"); } else if (encoding.equalsIgnoreCase("UNICODE")) { wb.setSheetName(0, "Export Workbook"); } styles = initStyles(wb); ps = sheet.getPrintSetup(); sheet.setAutobreaks(true); ps.setFitHeight((short) 1); ps.setFitWidth((short) 1); createHeader(model); }
From source file:com.develog.utils.report.engine.export.JRXlsExporter.java
License:Open Source License
/** * *//*from ww w .j a v a 2 s . c o m*/ protected void exportReportToStream(OutputStream os) throws JRException { workbook = new HSSFWorkbook(); emptyCellStyle = workbook.createCellStyle(); emptyCellStyle.setFillForegroundColor((new HSSFColor.WHITE()).getIndex()); emptyCellStyle.setFillPattern(backgroundMode); try { List pages = jasperPrint.getPages(); if (pages != null && pages.size() > 0) { JRPrintPage page = null; if (isOnePagePerSheet) { pageHeight = jasperPrint.getPageHeight(); for (int i = startPageIndex; i <= endPageIndex; i++) { if (Thread.currentThread().isInterrupted()) { throw new JRException("Current thread interrupted."); } page = (JRPrintPage) pages.get(i); sheet = workbook.createSheet("Page " + (i + 1)); /* */ exportPage(page, page); } } else { pageHeight = jasperPrint.getPageHeight() * pages.size(); JRPrintPage alterYAllPages = new JRBasePrintPage(); JRPrintPage allPages = new JRBasePrintPage(); Collection elements = null; JRPrintElement alterYElement = null; JRPrintElement element = null; for (int i = startPageIndex; i <= endPageIndex; i++) { if (Thread.currentThread().isInterrupted()) { throw new JRException("Current thread interrupted."); } page = (JRPrintPage) pages.get(i); elements = page.getElements(); if (elements != null && elements.size() > 0) { for (Iterator it = elements.iterator(); it.hasNext();) { element = (JRPrintElement) it.next(); allPages.addElement(element); alterYElement = new JRBasePrintElement(); alterYElement.setY(element.getY() + jasperPrint.getPageHeight() * i); alterYAllPages.addElement(alterYElement); } } } sheet = workbook.createSheet("Sheet1"); /* */ exportPage(alterYAllPages, allPages); } } workbook.write(os); } catch (IOException e) { throw new JRException("Error generating XLS report : " + jasperPrint.getName(), e); } }
From source file:com.dituiba.excel.BaseExcelService.java
License:Apache License
/** * * @return */ public static Workbook createWorkbook() { return new HSSFWorkbook(); }
From source file:com.dituiba.excel.TestExcel.java
License:Apache License
@Before public void setUp() { workbook = new HSSFWorkbook(); }
From source file:com.dufeng.core.BusinessPlan.java
License:Apache License
public static void main(String[] args) throws Exception { Workbook wb;/* w w w. j ava 2 s .c om*/ if (args.length > 0 && args[0].equals("-xls")) wb = new HSSFWorkbook(); else wb = new XSSFWorkbook(); Map<String, CellStyle> styles = createStyles(wb); Sheet sheet = wb.createSheet("Business Plan"); //turn off gridlines sheet.setDisplayGridlines(false); sheet.setPrintGridlines(false); sheet.setFitToPage(true); sheet.setHorizontallyCenter(true); PrintSetup printSetup = sheet.getPrintSetup(); printSetup.setLandscape(true); //the following three statements are required only for HSSF sheet.setAutobreaks(true); printSetup.setFitHeight((short) 1); printSetup.setFitWidth((short) 1); //the header row: centered text in 48pt font Row headerRow = sheet.createRow(0); headerRow.setHeightInPoints(12.75f); for (int i = 0; i < titles.length; i++) { Cell cell = headerRow.createCell(i); cell.setCellValue(titles[i]); cell.setCellStyle(styles.get("header")); } //columns for 11 weeks starting from 9-Jul Calendar calendar = Calendar.getInstance(); int year = calendar.get(Calendar.YEAR); //calendar.setTime(fmt.parse("9-Jul")); calendar.setTime(new Date()); calendar.set(Calendar.YEAR, year); for (int i = 0; i < 11; i++) { Cell cell = headerRow.createCell(titles.length + i); cell.setCellValue(calendar); cell.setCellStyle(styles.get("header_date")); calendar.roll(Calendar.WEEK_OF_YEAR, true); } //freeze the first row sheet.createFreezePane(0, 1); Row row; Cell cell; int rownum = 1; for (int i = 0; i < data.length; i++, rownum++) { row = sheet.createRow(rownum); if (data[i] == null) continue; for (int j = 0; j < data[i].length; j++) { cell = row.createCell(j); String styleName; boolean isHeader = i == 0 || data[i - 1] == null; switch (j) { case 0: if (isHeader) { styleName = "cell_b"; cell.setCellValue(Double.parseDouble(data[i][j])); } else { styleName = "cell_normal"; cell.setCellValue(data[i][j]); } break; case 1: if (isHeader) { styleName = i == 0 ? "cell_h" : "cell_bb"; } else { styleName = "cell_indented"; } cell.setCellValue(data[i][j]); break; case 2: styleName = isHeader ? "cell_b" : "cell_normal"; cell.setCellValue(data[i][j]); break; case 3: styleName = isHeader ? "cell_b_centered" : "cell_normal_centered"; cell.setCellValue(Integer.parseInt(data[i][j])); break; case 4: { //calendar.setTime(fmt.parse(data[i][j])); calendar.setTime(new Date()); calendar.set(Calendar.YEAR, year); cell.setCellValue(calendar); styleName = isHeader ? "cell_b_date" : "cell_normal_date"; break; } case 5: { int r = rownum + 1; String fmla = "IF(AND(D" + r + ",E" + r + "),E" + r + "+D" + r + ",\"\")"; cell.setCellFormula(fmla); styleName = isHeader ? "cell_bg" : "cell_g"; break; } default: styleName = data[i][j] != null ? "cell_blue" : "cell_normal"; } cell.setCellStyle(styles.get(styleName)); } } //group rows for each phase, row numbers are 0-based sheet.groupRow(4, 6); sheet.groupRow(9, 13); sheet.groupRow(16, 18); //set column widths, the width is measured in units of 1/256th of a character width sheet.setColumnWidth(0, 256 * 6); sheet.setColumnWidth(1, 256 * 33); sheet.setColumnWidth(2, 256 * 20); sheet.setZoom(3, 4); // Write the output to a file String file = "E:/businessplan.xls"; if (wb instanceof XSSFWorkbook) file += "x"; FileOutputStream out = new FileOutputStream(file); wb.write(out); out.close(); }