List of usage examples for org.apache.poi.hssf.usermodel HSSFCell setCellValue
@SuppressWarnings("fallthrough") public void setCellValue(boolean value)
From source file:com.b510.excel.client.HSSFReadWrite.java
License:Apache License
/** * Method main/*from w ww . jav a 2s . c om*/ * * Given 1 argument takes that as the filename, inputs it and dumps the cell * values/types out to sys.out.<br/> * * given 2 arguments where the second argument is the word "write" and the * first is the filename - writes out a sample (test) spreadsheet see * {@link HSSFReadWrite#testCreateSampleSheet(String)}.<br/> * * given 2 arguments where the first is an input filename and the second an * output filename (not write), attempts to fully read in the spreadsheet * and fully write it out.<br/> * * given 3 arguments where the first is an input filename and the second an * output filename (not write) and the third is "modify1", attempts to read * in the spreadsheet, deletes rows 0-24, 74-99. Changes cell at row 39, col * 3 to "MODIFIED CELL" then writes it out. Hence this is "modify test 1". * If you take the output from the write test, you'll have a valid scenario. */ public static void main(String[] args) { String[] name = new String[2]; name[0] = "HSSFReadWrite.xlsx"; name[1] = "write"; if (name.length < 1) { System.err.println("At least one argument expected"); return; } String fileName = name[0]; try { if (name.length < 2) { HSSFWorkbook wb = HSSFReadWrite.readFile(fileName); System.out.println("Data dump:\n"); for (int k = 0; k < wb.getNumberOfSheets(); k++) { HSSFSheet sheet = wb.getSheetAt(k); int rows = sheet.getPhysicalNumberOfRows(); System.out.println("Sheet " + k + " \"" + wb.getSheetName(k) + "\" has " + rows + " row(s)."); for (int r = 0; r < rows; r++) { HSSFRow row = sheet.getRow(r); if (row == null) { continue; } int cells = row.getPhysicalNumberOfCells(); System.out.println("\nROW " + row.getRowNum() + " has " + cells + " cell(s)."); for (int c = 0; c < cells; c++) { HSSFCell cell = row.getCell(c); String value = null; switch (cell.getCellType()) { case HSSFCell.CELL_TYPE_FORMULA: value = "FORMULA value=" + cell.getCellFormula(); break; case HSSFCell.CELL_TYPE_NUMERIC: value = "NUMERIC value=" + cell.getNumericCellValue(); break; case HSSFCell.CELL_TYPE_STRING: value = "STRING value=" + cell.getStringCellValue(); break; default: } System.out.println("CELL col=" + cell.getColumnIndex() + " VALUE=" + value); } } } } else if (name.length == 2) { if (name[1].toLowerCase().equals("write")) { System.out.println("Write mode"); long time = System.currentTimeMillis(); HSSFReadWrite.testCreateSampleSheet(fileName); System.out.println("" + (System.currentTimeMillis() - time) + " ms generation time"); } else { System.out.println("readwrite test"); HSSFWorkbook wb = HSSFReadWrite.readFile(fileName); FileOutputStream stream = new FileOutputStream(name[1]); wb.write(stream); stream.close(); } } else if (name.length == 3 && name[2].toLowerCase().equals("modify1")) { // delete row 0-24, row 74 - 99 && change cell 3 on row 39 to // string "MODIFIED CELL!!" HSSFWorkbook wb = HSSFReadWrite.readFile(fileName); FileOutputStream stream = new FileOutputStream(name[1]); HSSFSheet sheet = wb.getSheetAt(0); for (int k = 0; k < 25; k++) { HSSFRow row = sheet.getRow(k); sheet.removeRow(row); } for (int k = 74; k < 100; k++) { HSSFRow row = sheet.getRow(k); sheet.removeRow(row); } HSSFRow row = sheet.getRow(39); HSSFCell cell = row.getCell(3); cell.setCellValue("MODIFIED CELL!!!!!"); wb.write(stream); stream.close(); } } catch (Exception e) { e.printStackTrace(); } }
From source file:com.bdx.rainbow.spsy.service.impl.EnterpriseLicenseServiceImpl.java
/** * * @param title ??//from ww w . ja va 2 s .c om * @param headers ?? * @param resultLists ? * @param pattern ?,"yyyy-MM-dd" */ private HSSFWorkbook exportExcel(String title, String[] headers, List<DubboEnterpriseLicense> resultLists, String pattern) { if (resultLists == null) { return null; } if (pattern == null && StringUtils.isEmpty(pattern)) { pattern = "yyyy-MM-dd"; } SimpleDateFormat sdf = new SimpleDateFormat(pattern); // HSSFWorkbook workbook = new HSSFWorkbook(); // ? HSSFSheet sheet = workbook.createSheet(title); // 15 sheet.setDefaultColumnWidth((short) 15); // ?? HSSFCellStyle style = workbook.createCellStyle(); // ? style.setFillForegroundColor(HSSFColor.SKY_BLUE.index); style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); style.setBorderBottom(HSSFCellStyle.BORDER_THIN); style.setBorderLeft(HSSFCellStyle.BORDER_THIN); style.setBorderRight(HSSFCellStyle.BORDER_THIN); style.setBorderTop(HSSFCellStyle.BORDER_THIN); style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // ? HSSFFont font = workbook.createFont(); font.setColor(HSSFColor.VIOLET.index); font.setFontHeightInPoints((short) 12); font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); // ?? style.setFont(font); // ??? HSSFCellStyle style2 = workbook.createCellStyle(); style2.setFillForegroundColor(HSSFColor.LIGHT_YELLOW.index); style2.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); style2.setBorderBottom(HSSFCellStyle.BORDER_THIN); style2.setBorderLeft(HSSFCellStyle.BORDER_THIN); style2.setBorderRight(HSSFCellStyle.BORDER_THIN); style2.setBorderTop(HSSFCellStyle.BORDER_THIN); style2.setAlignment(HSSFCellStyle.ALIGN_CENTER); style2.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); // ?? HSSFFont font2 = workbook.createFont(); font2.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL); // ?? style2.setFont(font2); // HSSFRow row = sheet.createRow(0); for (short i = 0; i < headers.length; i++) { HSSFCell cell = row.createCell(i); cell.setCellStyle(style); HSSFRichTextString text = new HSSFRichTextString(headers[i]); cell.setCellValue(text); } // ????? for (int i = 0; i < resultLists.size(); i++) { row = sheet.createRow(i + 1); int index = 0; // HSSFCell cell = row.createCell(j); // row.createCell(index++).setCellStyle(style2); DubboEnterpriseLicense info = resultLists.get(i); row.createCell(index++).setCellValue(info.getEnterpriseName()); row.createCell(index++).setCellValue(info.getOrganizationCode()); row.createCell(index++).setCellValue(info.getLegalPerson()); row.createCell(index++).setCellValue(info.getLegalPersonPhone()); String type = info.getLicenseType(); if ("001".equals(type)) { row.createCell(index++).setCellValue(""); } else if ("002".equals(type)) { row.createCell(index++).setCellValue("??"); } else if ("003".equals(type)) { row.createCell(index++).setCellValue(""); } else if ("004".equals(type)) { row.createCell(index++).setCellValue("?"); } else { row.createCell(index++).setCellValue(type); } row.createCell(index++).setCellValue(info.getLicenseCode()); if (info.getLicenseTime() != null) { row.createCell(index++).setCellValue(sdf.format(info.getLicenseTime())); } else { row.createCell(index++).setCellValue(""); } if (info.getInvalidDate() != null) { Long time = (System.currentTimeMillis() - info.getInvalidDate().getTime()) / (1000 * 3600 * 24); if (time > 0) { row.createCell(index++).setCellValue(""); } else if (time <= 0) { row.createCell(index++).setCellValue(""); } else { row.createCell(index++).setCellValue("1"); } } else { row.createCell(index++).setCellValue(""); } } return workbook; }
From source file:com.bean.UserBean.java
public void gerarExcel() { HSSFWorkbook workbook = new HSSFWorkbook(); HSSFSheet firstSheet = workbook.createSheet("Aba1"); HSSFRow row = firstSheet.createRow(0); HSSFCell cell = row.createCell(0); cell.setCellValue("Primeiro valor"); cell = row.createCell(1);//from w w w . ja v a2 s . c om cell.setCellValue("Segundo valor"); row = firstSheet.createRow(1); cell = row.createCell(0); cell.setCellValue("Segunda linha"); try { FacesContext facesContext = FacesContext.getCurrentInstance(); ExternalContext externalContext = facesContext.getExternalContext(); externalContext.setResponseContentType("application/vnd.ms-excel"); externalContext.setResponseHeader("Content-Disposition", "attachment; filename=\"my.xls\""); workbook.write(externalContext.getResponseOutputStream()); facesContext.responseComplete(); } catch (Exception e) { System.out.println(e); } }
From source file:com.bharath.dao.SelectCourseDao.java
public String getStudentslisttoExcel(String curl) { ArrayList<String> arr = new ArrayList<String>(); ArrayList<String> arr1 = new ArrayList<String>(); ArrayList<String> arr2 = new ArrayList<String>(); ArrayList<String> arr3 = new ArrayList<String>(); try {//from ww w .j a v a2 s . c om UserAgent userAgent = new UserAgent(); userAgent.openContent(curl); //userAgent.visit(curl); Table table = userAgent.doc.getTable( "<table class=\"datadisplaytable\" summary=\"This table displays a list of students registered for the course; summary information about each student is provided.\" width=\"100%\">"); System.out.println(table.toString()); Elements elements = table.getCol(0); for (Element element : elements) { arr.add(element.innerText()); //System.out.println(element.innerText()); } Elements elements1 = table.getCol(2); for (Element element : elements1) { arr1.add(element.innerText()); //System.out.println(element.innerText()); } Elements elements2 = table.getCol(3); for (Element element : elements2) { arr2.add(element.innerText()); //System.out.println(element.innerText()); } Elements elements3 = table.getCol(4); for (Element element : elements3) { arr3.add(element.innerText()); // System.out.println(element.innerText()); } HSSFWorkbook workBook = new HSSFWorkbook(); HSSFSheet spreadSheet = workBook.createSheet("StudentDetails"); HSSFRow row; HSSFCell cell; //int colnum = 0; int rowcount = 0; //for (int i = 0; i < arr.size(); i++) { for (String str : arr) { row = spreadSheet.createRow((short) rowcount); // System.out.println(arr.get(i)); cell = row.createCell(0); cell.setCellValue(str.trim()); rowcount++; // colcount++; } rowcount = 0; for (String str : arr1) { row = spreadSheet.getRow((short) rowcount); // System.out.println(arr.get(i)); cell = row.createCell(1); cell.setCellValue(str.trim()); rowcount++; // colcount++; } rowcount = 0; for (String str : arr2) { row = spreadSheet.getRow((short) rowcount); // System.out.println(arr.get(i)); cell = row.createCell(2); cell.setCellValue(str.trim()); rowcount++; // colcount++; } rowcount = 0; for (String str : arr3) { row = spreadSheet.getRow((short) rowcount); // System.out.println(arr.get(i)); cell = row.createCell(3); cell.setCellValue(str.trim()); rowcount++; // colcount++; } ByteArrayOutputStream outByteStream = new ByteArrayOutputStream(); workBook.write(outByteStream); outArray = outByteStream.toByteArray(); System.out.println(outArray.length); //fos = new FileOutputStream("student.xls"); System.out.println("Done"); //workBook.write(fos); } catch (Exception e) { System.out.println(e); } return "Done"; }
From source file:com.br.uepb.bsc7.www.UI.ManipulaXLS.java
public /*static*/ void criaXLS1(String arqSaida, String nomePlan) throws IOException, NullPointerException { int numLinhas = 5; int numCelulas = 2; //Cria pasta de trabalho wb = new HSSFWorkbook(); //Cria planilha HSSFSheet s = wb.createSheet(nomePlan); //wb.setSheetName(0, nomePlan); //Alterar para a quantidade de linhas retornada por cada relatrio for (int i = 0; i < numLinhas; i++) { //Cria a linha HSSFRow linha = s.createRow(i);//from w ww . j a v a2s .co m //Alterar para a quantidade de clulas retornada por cada relatrio for (int j = 0; j < numCelulas; j++) { HSSFCell celula = linha.createCell(j); /*O valor passado deve ser de acordo com aquele recebido por cada relatrio, provavelmente String;*/ //celula.setCellValue((String) tabela.getValueAt(i, j)); celula.setCellValue(i + j); } } FileOutputStream fileOutputStream = new FileOutputStream(arqSaida + ".xls"); try { wb.write(fileOutputStream); //Fecha o fileOutputStream //Melhorar este catch } catch (IOException ex) { System.out.println("Teste"); } finally { fileOutputStream.close(); wb.close(); //Fecha a pasta de trabalho } }
From source file:com.br.uepb.bsc7.www.UI.ManipulaXLS.java
public /*static*/ void criaXLS(JTable tabela, String arqSaida, String nomePlan) throws IOException, NullPointerException { int numLinhas = tabela.getRowCount(); int numCelulas = tabela.getColumnCount(); //Cria pasta de trabalho wb = new HSSFWorkbook(); //Cria planilha HSSFSheet s = wb.createSheet(nomePlan); //wb.setSheetName(0, nomePlan); //Alterar para a quantidade de linhas retornada por cada relatrio for (int i = 0; i < numLinhas; i++) { //Cria a linha HSSFRow linha = s.createRow(i);// w ww. j a v a 2 s . c o m //Alterar para a quantidade de clulas retornada por cada relatrio for (int j = 0; j < numCelulas; j++) { HSSFCell celula = linha.createCell(j); /*O valor passado deve ser de acordo com aquele recebido por cada relatrio, provavelmente String;*/ celula.setCellValue((String) tabela.getValueAt(i, j)); } } FileOutputStream fileOutputStream = new FileOutputStream(arqSaida + ".xls"); try { wb.write(fileOutputStream); //Fecha o fileOutputStream //Melhorar este catch } catch (IOException ex) { System.out.println("Teste"); } finally { fileOutputStream.close(); wb.close(); //Fecha a pasta de trabalho } }
From source file:com.brick.customer.util.CustomerInfoExcel.java
@SuppressWarnings("unchecked") public HSSFWorkbook createReport(Map<String, Object> params, Context context) throws Exception { ExcelFileWriter efw = new ExcelFileWriter(); HSSFSheet sheet = efw.createSheet(context.contextMap.get("sheetName") == null ? "summary" : (String) context.contextMap.get("sheetName")); List<HashMap<String, Object>> list = (List<HashMap<String, Object>>) params.get("cusInfo"); sheet.setColumnWidth(0, 5000);//from w w w. j a va 2 s . c o m sheet.setColumnWidth(1, 3000); sheet.setColumnWidth(2, 5300); sheet.setColumnWidth(3, 3600); sheet.setColumnWidth(4, 4600); sheet.setColumnWidth(5, 10000); sheet.setColumnWidth(6, 6000); sheet.setColumnWidth(7, 4000); sheet.setColumnWidth(8, 3500); sheet.setColumnWidth(9, 3500); sheet.setColumnWidth(10, 3500); sheet.setColumnWidth(11, 4200); sheet.setColumnWidth(12, 4200); sheet.setColumnWidth(13, 4200); sheet.setColumnWidth(14, 4800); sheet.setColumnWidth(15, 5000); sheet.setColumnWidth(16, 5000); sheet.setColumnWidth(17, 3000); sheet.setColumnWidth(18, 4500); sheet.setColumnWidth(19, 4500); sheet.setColumnWidth(20, 3000); sheet.setColumnWidth(21, 4300); sheet.setColumnWidth(22, 4000); sheet.setColumnWidth(23, 7000); sheet.setColumnWidth(24, 10000); HSSFFont headFont0 = null; HSSFCellStyle headStyle0 = null; headFont0 = efw.getWorkbook().createFont(); headFont0.setFontHeightInPoints((short) 13); //? headFont0.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); // headStyle0 = efw.getWorkbook().createCellStyle(); //? headStyle0.setAlignment(HSSFCellStyle.ALIGN_CENTER); // ? headStyle0.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); // headStyle0.setWrapText(true); // ? headStyle0.setFillBackgroundColor((short) 59); headStyle0.setFont(headFont0); //?? HSSFCellStyle cellMoney = efw.getWorkbook().createCellStyle(); HSSFDataFormat format = efw.getWorkbook().createDataFormat(); cellMoney.setAlignment(HSSFCellStyle.ALIGN_RIGHT); cellMoney.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); cellMoney.setDataFormat(format.getFormat("#,###,##0")); HSSFRow row0 = sheet.createRow(0); HSSFCell cell0 = row0.createCell(0); cell0.setCellValue("?"); cell0.setCellStyle(headStyle0); HSSFCell cell1 = row0.createCell(1); cell1.setCellValue("??"); cell1.setCellStyle(headStyle0); HSSFCell cell2 = row0.createCell(2); cell2.setCellValue("???"); cell2.setCellStyle(headStyle0); HSSFCell cell3 = row0.createCell(3); cell3.setCellValue("??"); cell3.setCellStyle(headStyle0); HSSFCell cell4 = row0.createCell(4); cell4.setCellValue("?"); cell4.setCellStyle(headStyle0); HSSFCell cell5 = row0.createCell(5); cell5.setCellValue("??"); cell5.setCellStyle(headStyle0); HSSFCell cell6 = row0.createCell(6); cell6.setCellValue("??"); cell6.setCellStyle(headStyle0); HSSFCell cell7 = row0.createCell(7); cell7.setCellValue("?"); cell7.setCellStyle(headStyle0); HSSFCell cell8 = row0.createCell(8); cell8.setCellValue("?"); cell8.setCellStyle(headStyle0); HSSFCell cell9 = row0.createCell(9); cell9.setCellValue(""); cell9.setCellStyle(headStyle0); HSSFCell cell10 = row0.createCell(10); cell10.setCellValue(""); cell10.setCellStyle(headStyle0); HSSFCell cell11 = row0.createCell(11); cell11.setCellValue("??"); cell11.setCellStyle(headStyle0); HSSFCell cell12 = row0.createCell(12); cell12.setCellValue("?"); cell12.setCellStyle(headStyle0); HSSFCell cell13 = row0.createCell(13); cell13.setCellValue("?"); cell13.setCellStyle(headStyle0); HSSFCell cell14 = row0.createCell(14); cell14.setCellValue(""); cell14.setCellStyle(headStyle0); HSSFCell cell15 = row0.createCell(15); cell15.setCellValue("?"); cell15.setCellStyle(headStyle0); HSSFCell cell16 = row0.createCell(16); cell16.setCellValue("??"); cell16.setCellStyle(headStyle0); HSSFCell cell17 = row0.createCell(17); cell17.setCellValue("?"); cell17.setCellStyle(headStyle0); HSSFCell cell18 = row0.createCell(18); cell18.setCellValue("??"); cell18.setCellStyle(headStyle0); HSSFCell cell19 = row0.createCell(19); cell19.setCellValue("?"); cell19.setCellStyle(headStyle0); HSSFCell cell20 = row0.createCell(20); cell20.setCellValue(""); cell20.setCellStyle(headStyle0); HSSFCell cell21 = row0.createCell(21); cell21.setCellValue("???"); cell21.setCellStyle(headStyle0); HSSFCell cell22 = row0.createCell(22); cell22.setCellValue("??"); cell22.setCellStyle(headStyle0); HSSFCell cell23 = row0.createCell(23); cell23.setCellValue("??"); cell23.setCellStyle(headStyle0); HSSFCell cell24 = row0.createCell(24); cell24.setCellValue(""); cell24.setCellStyle(headStyle0); for (int i = 0; i < list.size(); i++) { HSSFRow row1 = sheet.createRow(i + 1); HSSFCell cellr0 = row1.createCell(0); cellr0.setCellValue((String) list.get(i).get("CUST_CODE")); HSSFCell cellr1 = row1.createCell(1); cellr1.setCellValue((String) list.get(i).get("NAME")); HSSFCell cellr2 = row1.createCell(2); cellr2.setCellValue((String) list.get(i).get("CUST_NAME")); HSSFCell cellr3 = row1.createCell(3); cellr3.setCellValue((String) list.get(i).get("CORP_ORAGNIZATION_CODE")); HSSFCell cellr4 = row1.createCell(4); cellr4.setCellValue((String) list.get(i).get("CUST_AREA")); HSSFCell cellr5 = row1.createCell(5); cellr5.setCellValue((String) list.get(i).get("CORP_WORK_ADDRESS")); HSSFCell cellr6 = row1.createCell(6); cellr6.setCellValue((String) list.get(i).get("VIRTUAL_CODE")); HSSFCell cellr7 = row1.createCell(7); String s = null; int type = Integer.parseInt(list.get(i).get("STATETYPE").toString()); switch (type) { case 0: s = ""; break; case 1: s = ""; break; case 2: s = "??"; break; case 3: s = ""; break; case 4: s = ""; break; } cellr7.setCellValue(s); HSSFCell cellr8 = row1.createCell(8); cellr8.setCellValue((String) list.get(i).get("CORP_SETUP_DATE")); HSSFCell cellr9 = row1.createCell(9); double n = list.get(i).get("CORP_REGISTE_CAPITAL") == null ? 0 : (Double) list.get(i).get("CORP_REGISTE_CAPITAL"); cellr9.setCellValue(n); cellr9.setCellStyle(cellMoney); HSSFCell cellr10 = row1.createCell(10); double m = list.get(i).get("CORP_PAICLUP_CAPITAL") == null ? 0 : (Double) list.get(i).get("CORP_PAICLUP_CAPITAL"); cellr10.setCellValue(m); cellr10.setCellStyle(cellMoney); HSSFCell cellr11 = row1.createCell(11); cellr11.setCellValue((String) list.get(i).get("CORP_BUSINESS_LICENSE")); HSSFCell cellr12 = row1.createCell(12); cellr12.setCellValue((String) list.get(i).get("TAX_CODE")); HSSFCell cellr13 = row1.createCell(13); cellr13.setCellValue((String) list.get(i).get("CORP_TAX_CODE")); HSSFCell cellr14 = row1.createCell(14); cellr14.setCellValue((String) list.get(i).get("CORP_PERIOD_VALIDITY")); HSSFCell cellr15 = row1.createCell(15); cellr15.setCellValue((String) list.get(i).get("CORP_WORK_ADDRESS")); HSSFCell cellr16 = row1.createCell(16); cellr16.setCellValue((String) list.get(i).get("CORP_BUSINESS_RANGE")); HSSFCell cellr17 = row1.createCell(17); cellr17.setCellValue((String) list.get(i).get("CORP_COMPANY_ZIP")); HSSFCell cellr18 = row1.createCell(18); cellr18.setCellValue((String) list.get(i).get("CORP_COMPANY_WEBSITE")); HSSFCell cellr19 = row1.createCell(19); cellr19.setCellValue((String) list.get(i).get("CORP_COMPANY_EMAIL")); HSSFCell cellr20 = row1.createCell(20); cellr20.setCellValue((String) list.get(i).get("CORP_HEAD_SIGNATURE")); HSSFCell cellr21 = row1.createCell(21); cellr21.setCellValue((String) list.get(i).get("CORP_HS_IDCARD")); HSSFCell cellr22 = row1.createCell(22); cellr22.setCellValue((String) list.get(i).get("CORP_HS_LINK_MODE")); HSSFCell cellr23 = row1.createCell(23); cellr23.setCellValue((String) list.get(i).get("CORP_HS_HOME_ADDRESS")); HSSFCell cellr24 = row1.createCell(24); cellr24.setCellValue((String) list.get(i).get("REMARK")); } return efw.getWorkbook(); }
From source file:com.byd.test.actions.OrderAction.java
@RequestMapping("xopera") public ModelAndView xopera(HttpServletRequest request, HttpServletResponse response) { try {/* w w w .j a va 2 s . co m*/ OutputStream out = response.getOutputStream(); response.reset(); response.setHeader("content-disposition", "attachment;filename=" + "fileName.xls"); response.setContentType("application/x-octetstream;charset=gb2312"); HSSFWorkbook workbook = new HSSFWorkbook(); HSSFSheet sheet = null; String sheetName = "first sheet"; sheet = workbook.createSheet(sheetName); System.out.println("*-*-----------------------"); HSSFCell cell = sheet.createRow(1).createCell((short) 2); cell.setCellValue(323); workbook.write(out); out.flush(); out.close(); } catch (IOException ex) { Logger.getLogger(OrderAction.class.getName()).log(Level.SEVERE, null, ex); } String s = (String) request.getParameter("plantCode");//String a = request.getQueryString(""); System.out.println("plantCode: " + s); return new ModelAndView("myname"); }
From source file:com.carfinance.module.common.controller.DocumentDownloadController.java
/** * ????Excel//from w w w . j a v a2 s .c o m * @param model * @param request * @param response */ @RequestMapping(value = "/cashierstatement", method = RequestMethod.GET) public void cashierStatement(Model model, HttpServletRequest request, HttpServletResponse response) { String contrace_id_str = request.getParameter("contrace_id"); String contrace_no = ""; String customer_name = ""; String customer_no = ""; String daily_available_km = ""; String contrace_type = ""; VehicleContraceInfo vehicleContraceInfo = this.vehicleServiceManageService .getVehicleContraceInfoById(Long.valueOf(contrace_id_str)); if (vehicleContraceInfo != null) { contrace_no = vehicleContraceInfo.getContrace_no(); customer_name = vehicleContraceInfo.getCustomer_name(); daily_available_km = vehicleContraceInfo.getDaily_available_km() + ""; } else { PropertyContraceInfo propertyContraceInfo = this.vehicleServiceManageService .getPropertyContraceInfoById(Long.valueOf(contrace_id_str)); if (propertyContraceInfo != null) { contrace_no = propertyContraceInfo.getContrace_no(); customer_name = propertyContraceInfo.getCustomer_name(); contrace_type = "?"; } } List<VehicleContraceVehsInfo> vehicleContraceVehsInfoList = this.vehicleServiceManageService .getVehicleContraceVehsListByContraceId(Long.valueOf(contrace_id_str)); //1.ContentType response.setContentType("multipart/form-data"); //2.????(??a.pdf) response.setHeader("Content-Disposition", "attachment;fileName=" + contrace_no + ".xls"); // webbookExcel HSSFWorkbook wb = new HSSFWorkbook(); for (VehicleContraceVehsInfo v : vehicleContraceVehsInfoList) { // webbooksheet,Excelsheet HSSFSheet sheet = wb.createSheet(v.getLicense_plate()); // 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("?"); cell.setCellStyle(style); cell = row.createCell((short) 2); cell.setCellValue(""); cell.setCellStyle(style); cell = row.createCell((short) 3); cell.setCellValue("?"); cell.setCellStyle(style); cell = row.createCell((short) 4); cell.setCellValue(""); cell.setCellStyle(style); row = sheet.createRow(1); row.createCell((short) 0).setCellValue(customer_name); row.createCell((short) 1).setCellValue(customer_no); row.createCell((short) 2).setCellValue(contrace_type); row.createCell((short) 3).setCellValue(""); row.createCell((short) 4).setCellValue(""); row = sheet.createRow(2); HSSFCell cell2 = row.createCell((short) 0); cell2.setCellValue("?"); cell2.setCellStyle(style); cell2 = row.createCell((short) 1); cell2.setCellValue("??"); cell2.setCellStyle(style); cell2 = row.createCell((short) 2); cell2.setCellValue(""); cell2.setCellStyle(style); cell2 = row.createCell((short) 3); cell2.setCellValue(""); cell2.setCellStyle(style); cell2 = row.createCell((short) 4); cell2.setCellValue("??"); cell2.setCellStyle(style); row = sheet.createRow(3); row.createCell((short) 0).setCellValue(v.getKm()); row.createCell((short) 1).setCellValue(v.getOil_percent()); long over_km = (v.getReturn_km() - v.getKm()) > 0 ? (v.getReturn_km() - v.getKm()) : 0; row.createCell((short) 2).setCellValue(over_km); row.createCell((short) 3).setCellValue(""); row.createCell((short) 4).setCellValue(""); row = sheet.createRow(4); HSSFCell cell4 = row.createCell((short) 0); cell4.setCellValue(""); cell4.setCellStyle(style); cell4 = row.createCell((short) 1); cell4.setCellValue("?"); cell4.setCellStyle(style); cell4 = row.createCell((short) 2); cell4.setCellValue(""); cell4.setCellStyle(style); cell4 = row.createCell((short) 3); cell4.setCellValue(""); cell4.setCellStyle(style); cell4 = row.createCell((short) 4); cell4.setCellValue(""); cell4.setCellStyle(style); row = sheet.createRow(5); row.createCell((short) 0).setCellValue(v.getReturn_km()); row.createCell((short) 1).setCellValue(v.getRevert_oil_percent()); long over_oil = (v.getOil_percent() - v.getRevert_oil_percent()) > 0 ? (v.getOil_percent() - v.getRevert_oil_percent()) : 0; row.createCell((short) 2).setCellValue(over_oil); row.createCell((short) 3).setCellValue(""); row.createCell((short) 4).setCellValue(v.getActually_price()); } // ? try { String path = appProps.get("contrace.over.download.path") + contrace_no + ".xls"; FileOutputStream fout = new FileOutputStream(path); wb.write(fout); fout.close(); ServletOutputStream out; //File(?download.pdf) File file = new File(path); try { FileInputStream inputStream = new FileInputStream(file); //3.response?ServletOutputStream(out) out = response.getOutputStream(); int b = 0; byte[] buffer = new byte[512]; while (b != -1) { b = inputStream.read(buffer); //4.?(out) out.write(buffer, 0, b); } inputStream.close(); out.close(); out.flush(); } catch (IOException e) { e.printStackTrace(); } } catch (Exception e) { e.printStackTrace(); } }
From source file:com.cimmyt.reports.impl.ServiceReportCustomQueryImpl.java
License:Apache License
private void createCell(HSSFRow rowData, int sizeColumn, String strValue, HSSFCellStyle style) { HSSFCell dataCell = rowData.createCell(sizeColumn); HSSFRichTextString cellValue = new HSSFRichTextString(strValue); if (style != null) dataCell.setCellStyle(style);// w ww. ja v a 2s . co m dataCell.setCellType(HSSFCell.CELL_TYPE_STRING); dataCell.setCellValue(cellValue); }