List of usage examples for org.apache.poi.hssf.usermodel HSSFRow createCell
@Override public HSSFCell createCell(int column)
From source file:com.asakusafw.testtools.templategen.ExcelBookBuilder.java
License:Apache License
private HSSFCell getCell(HSSFSheet sheet, int rownum, int col) { HSSFRow row = sheet.getRow(rownum); if (row == null) { row = sheet.createRow(rownum);/*from w w w. ja va 2s . c o m*/ } HSSFCell cell = row.getCell(col); if (cell == null) { cell = row.createCell(col); } cell.setCellStyle(commonStyle); return cell; }
From source file:com.asakusafw.testtools.templategen.ExcelBookBuilder.java
License:Apache License
/** * ???//from w w w . j a v a 2 s. co m * @param sheetName ??? * @return ??? * @throws SQLException ???????? */ private HSSFSheet createInputDataSheet(String sheetName) throws SQLException { HSSFSheet sheet = workbook.createSheet(sheetName); // ?? HSSFRow row = sheet.createRow(0); for (int i = 0; i < columnInfos.length; i++) { HSSFCell cell = row.createCell(i); cell.setCellValue(columnInfos[i].getColumnName()); cell.setCellStyle(titleStyle); } // DB? PreparedStatement ps = null; ResultSet rs = null; String sql = "SELECT * FROM " + databaseName + "." + tableName + " limit 0, " + Constants.MAX_ROWS; try { ps = conn.prepareStatement(sql); rs = ps.executeQuery(); while (rs.next()) { row = sheet.createRow(row.getRowNum() + 1); for (int i = 0; i < columnInfos.length; i++) { ColumnInfo info = columnInfos[i]; HSSFCell cell = row.createCell(i); cell.setCellStyle(commonStyle); switch (info.getDataType()) { case CHAR: case VARCHAR: String str = rs.getString(info.getColumnName()); if (!rs.wasNull()) { cell.setCellValue(str); } break; case DATE: Date date = rs.getDate(info.getColumnName()); if (!rs.wasNull()) { cell.setCellValue(new java.util.Date(date.getTime())); cell.setCellStyle(dateStyle); } break; case DATETIME: case TIMESTAMP: Timestamp ts = rs.getTimestamp(info.getColumnName()); if (!rs.wasNull()) { cell.setCellValue(new java.util.Date(ts.getTime())); cell.setCellStyle(dateTimeStyle); } break; case DECIMAL: BigDecimal decimal = rs.getBigDecimal(info.getColumnName()); if (!rs.wasNull()) { cell.setCellValue(decimal.toPlainString()); } break; case TINY_INT: case SMALL_INT: case INT: case LONG: long value = rs.getLong(info.getColumnName()); if (!rs.wasNull()) { cell.setCellValue(Long.toString(value)); } break; default: assert false; break; } } } } finally { if (rs != null) { try { rs.close(); } catch (Exception e) { e.printStackTrace(); } } if (ps != null) { try { ps.close(); } catch (Exception e) { e.printStackTrace(); } } } // ? for (int i = 0; i < columnInfos.length; i++) { sheet.autoSizeColumn(i); } return sheet; }
From source file:com.assentisk.util.FilesService.java
public void generatePositionProfileXls(HttpServletRequest req, String Doc_name, List catList) throws SQLException, IOException { Integer key = 1;//from w ww . j av a2 s . c o m try { HSSFWorkbook workbook = new HSSFWorkbook(); HSSFSheet sheet = workbook.createSheet(Doc_name); Map<String, Object[]> data = new LinkedHashMap<String, Object[]>(); data.put(key.toString(), new Object[] { "Control ID", "Risk", "Control Objective", "Control Description", "Test Steps", "Busines Process Name", "Control Category Name" }); if (catList != null) { for (int i = 0; i < catList.size(); i++) { Map map = (Map) catList.get(i); short colIndex = 0; data.put((++key).toString(), new Object[] { map.get("Control_ID"), map.get("Risk"), map.get("Control_Objective"), map.get("Control_Description"), map.get("Test_Steps"), map.get("businessProcess"), map.get("controlcategory") }); Set<String> keyset = data.keySet(); int rownum = 0; sheet.setColumnHidden((short) 14, true); for (String key2 : keyset) { HSSFRow row = sheet.createRow(rownum++); Object[] objArr = data.get(key2); short cellnum = 0; for (Object obj : objArr) { if (obj instanceof String) { row.createCell(cellnum++).setCellValue((String) obj); } else if (obj instanceof Integer) row.createCell(cellnum++).setCellValue((Integer) obj); else if (obj instanceof String) { row.createCell(cellnum++).setCellValue((String) obj); } else if (obj instanceof String) row.createCell(cellnum++).setCellValue((String) obj); else if (obj instanceof String) row.createCell(cellnum++).setCellValue((String) obj); else if (obj instanceof String) row.createCell(cellnum++).setCellValue((Integer) obj); else if (obj instanceof Integer) row.createCell(cellnum++).setCellValue((String) obj); else if (obj instanceof String) row.createCell(cellnum++).setCellValue((String) obj); else if (obj instanceof String) row.createCell(cellnum++).setCellValue((String) obj); else if (obj instanceof String) row.createCell(cellnum++).setCellValue((String) obj); else if (obj instanceof String) row.createCell(cellnum++).setCellValue((String) obj); else if (obj instanceof String) row.createCell(cellnum++).setCellValue((String) obj); else if (obj instanceof String) row.createCell(cellnum++).setCellValue((String) obj); else if (obj instanceof String) row.createCell(cellnum++).setCellValue((String) obj); else if (obj instanceof String) row.createCell(cellnum++).setCellValue((String) obj); } } } } FileOutputStream out = new FileOutputStream(new File("" + Doc_name + ".xls")); workbook.write(out); out.close(); System.out.println("Excel written successfully.."); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (Exception ex) { ex.printStackTrace(); } }
From source file:com.assentisk.util.FilesService.java
public void generateEmployeDemoXls(HttpServletRequest req, String docName, List<LocationsBean> getLocationList, List<DepartmentBean> getDepartmnentList, List<LegalEntitiesBean> divisionList) throws SQLException, IOException { Integer key = 1;// w ww.j av a 2s. c o m try { HSSFWorkbook workbook = new HSSFWorkbook(); HSSFSheet sheet = workbook.createSheet(docName); String[] str = new String[getLocationList.size()]; if (getLocationList.size() > 0) { for (int i = 0; i < getLocationList.size(); i++) { Map m = (Map) getLocationList.get(i); str[i] = m.get("Name").toString(); } } String[] strDprtmnt = new String[getDepartmnentList.size()]; if (getDepartmnentList.size() > 0) { for (int i = 0; i < getDepartmnentList.size(); i++) { Map m = (Map) getDepartmnentList.get(i); strDprtmnt[i] = m.get("DeptName").toString(); } } String[] strDivision = new String[divisionList.size()]; if (divisionList.size() > 0) { for (int i = 0; i < divisionList.size(); i++) { Map m = (Map) divisionList.get(i); strDivision[i] = m.get("LegalEntityName").toString(); } } /*for(int i=0;i<locationName.size();i++){ str[i]=locationName.get(i).toString(); }*/ // for(int i=1;i<50;i++){ // CellRangeAddressList addressList = new CellRangeAddressList(i, i, 1, 1); // DVConstraint dvConstraint = DVConstraint.createExplicitListConstraint(str); // DataValidation dataValidation = new HSSFDataValidation(addressList, dvConstraint); // dataValidation.setSuppressDropDownArrow(false); // sheet.addValidationData(dataValidation); // // CellRangeAddressList departmnentList = new CellRangeAddressList(i, i, 0, 0); // DVConstraint dvConstraint1 = DVConstraint.createExplicitListConstraint(strDprtmnt); // DataValidation dataValidation1 = new HSSFDataValidation(departmnentList, dvConstraint1); // dataValidation1.setSuppressDropDownArrow(false); // sheet.addValidationData(dataValidation1); // // // CellRangeAddressList devisionList = new CellRangeAddressList(i, i, 9, 9); // DVConstraint dvConstraint2 = DVConstraint.createExplicitListConstraint(strDivision); // DataValidation dataValidation2 = new HSSFDataValidation(devisionList, dvConstraint2); // dataValidation2.setSuppressDropDownArrow(false); // sheet.addValidationData(dataValidation2); // // } // Map<String, String[]> data = new LinkedHashMap<String, String[]>(); String[] stringVal = { "EmpName", "Department", "Location", "location Country", "location State", "location City", "location Contact Name", "location Email", "Divisions", "address", "city", "zip", "phone1", "phone2", "Email1", "Email2" }; // data.put(key.toString(), { "DepartmentID","LocationID","EmpName","Email", "address","city","state","country","zip","phone1","phone2","divisions","email2","isLicense","isActive","abc"}); data.put(key.toString(), stringVal); Set<String> keyset = data.keySet(); int rownum = 0; sheet.setColumnHidden((short) 17, true); for (String key2 : keyset) { HSSFRow row = sheet.createRow(rownum++); String[] objArr = data.get(key2); short cellnum = 0; for (String obj : objArr) { if (obj instanceof String) { row.createCell(cellnum++).setCellValue((String) obj); } else if (obj instanceof String) { row.createCell(cellnum++).setCellValue((String) obj); } else if (obj instanceof String) row.createCell(cellnum++).setCellValue((String) obj); else if (obj instanceof String) row.createCell(cellnum++).setCellValue((String) obj); else if (obj instanceof String) row.createCell(cellnum++).setCellValue((String) obj); else if (obj instanceof String) row.createCell(cellnum++).setCellValue((String) obj); else if (obj instanceof String) row.createCell(cellnum++).setCellValue((String) obj); else if (obj instanceof String) row.createCell(cellnum++).setCellValue((String) obj); else if (obj instanceof String) row.createCell(cellnum++).setCellValue((String) obj); else if (obj instanceof String) row.createCell(cellnum++).setCellValue((String) obj); else if (obj instanceof String) row.createCell(cellnum++).setCellValue((String) obj); else if (obj instanceof String) row.createCell(cellnum++).setCellValue((String) obj); else if (obj instanceof String) row.createCell(cellnum++).setCellValue((String) obj); else if (obj instanceof String) row.createCell(cellnum++).setCellValue((String) obj); else if (obj instanceof String) row.createCell(cellnum++).setCellValue((String) obj); else if (obj instanceof String) row.createCell(cellnum++).setCellValue((String) obj); else if (obj instanceof String) row.createCell(cellnum++).setCellValue((String) obj); } } FileOutputStream out = new FileOutputStream(new File("" + docName + ".xls")); workbook.write(out); out.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (Exception ex) { ex.printStackTrace(); } }
From source file:com.assentisk.util.FilesService.java
public void generateAssestDemoXls(HttpServletRequest req, String docName, List<CategoriesBean> getCategoryList, List<CountryBean> getCountryList, List<ApplicationsBean> appList2, List<BusinessProcessesBean> getBusinessList) throws SQLException, IOException { Integer key = 1;/* w ww .j a va2s . co m*/ try { HSSFWorkbook workbook = new HSSFWorkbook(); HSSFSheet sheet = workbook.createSheet(docName); // HSSFSheet hidden = workbook.createSheet("hidden"); // HSSFSheet hidden1 = workbook.createSheet("hidden1"); // HSSFSheet hidden2 = workbook.createSheet("hidden2"); // HSSFSheet hidden3 = workbook.createSheet("hidden3"); String[] strCategorymnt = new String[getCategoryList.size()]; // if(getCategoryList.size()>0) // { // for (int i = 0; i < getCategoryList.size(); i++) { // Map m = (Map)getCategoryList.get(i); // strCategorymnt[i] = m.get("cname").toString(); // String name = strCategorymnt[i]; // HSSFRow row = hidden3.createRow(i); // HSSFCell cell = row.createCell(0); // cell.setCellValue(name); // } // } // // String [] strCountry = new String[getCountryList.size()]; // if(getCountryList.size()>0) // { // for (int i = 0; i < getCountryList.size(); i++) { // // Map m = (Map)getCountryList.get(i); // strCountry[i] = m.get("CountryName").toString(); // String name = strCountry[i]; // HSSFRow row = hidden.createRow(i); // HSSFCell cell = row.createCell(0); // cell.setCellValue(name); // // } // } // String [] strApllication = new String[appList2.size()]; // if(appList2.size()>0) // { // for (int i = 0; i < appList2.size(); i++) { // // Map m = (Map)appList2.get(i); // strApllication[i] = m.get("ApplicationName").toString(); // String name = strApllication[i]; // HSSFRow row = hidden2.createRow(i); // HSSFCell cell = row.createCell(0); // cell.setCellValue(name); // // } // } // String [] strBusinessList = new String[getBusinessList.size()]; // if(getBusinessList.size()>0) // { // for (int i = 0; i < getBusinessList.size(); i++) { // // Map m = (Map)getBusinessList.get(i); // strBusinessList[i] = m.get("BusinessProcessName").toString(); // String name = strBusinessList[i]; // HSSFRow row = hidden1.createRow(i); // HSSFCell cell = row.createCell(0); // cell.setCellValue(name); // } // } // Name namedCell = workbook.createName(); // namedCell.setNameName("hidden"); // namedCell.setRefersToFormula("hidden!$A$1:$A$" + strCountry.length); // Name namedCell1 = workbook.createName(); // namedCell1.setNameName("hidden1"); // namedCell1.setRefersToFormula("hidden1!$A$1:$A$" + strBusinessList.length); // // Name namedCell2 = workbook.createName(); // namedCell2.setNameName("hidden2"); // namedCell2.setRefersToFormula("hidden2!$A$1:$A$" + strCategorymnt.length); // // Name namedCell3 = workbook.createName(); // namedCell3.setNameName("hidden3"); // namedCell3.setRefersToFormula("hidden3!$A$1:$A$" + strApllication.length); // for(int i=1;i<50;i++){ // DVConstraint constraint = DVConstraint.createFormulaListConstraint("hidden"); // CellRangeAddressList country = new CellRangeAddressList(1, 1, 7, 7); // HSSFDataValidation validation = new HSSFDataValidation(country, constraint); // workbook.setSheetHidden(1, true); // sheet.addValidationData(validation); // DVConstraint constraint1 = DVConstraint.createFormulaListConstraint("hidden1"); // CellRangeAddressList businessList = new CellRangeAddressList(1, 1, 3,3); // HSSFDataValidation validation1 = new HSSFDataValidation(businessList, constraint1); // workbook.setSheetHidden(1, true); // sheet.addValidationData(validation1); // // DVConstraint constraint3 = DVConstraint.createFormulaListConstraint("hidden2"); // CellRangeAddressList appList = new CellRangeAddressList(1, 1, 2, 2); // HSSFDataValidation validation2 = new HSSFDataValidation(appList, constraint3); // workbook.setSheetHidden(1, true); // sheet.addValidationData(validation2); // // // DVConstraint constraint2 = DVConstraint.createFormulaListConstraint("hidden3"); // CellRangeAddressList catList = new CellRangeAddressList(1, 1, 1, 1); // HSSFDataValidation validation3 = new HSSFDataValidation(catList, constraint2); // workbook.setSheetHidden(1, true); // sheet.addValidationData(validation3); // CellRangeAddressList businessList = new CellRangeAddressList(i, i, 3, 3); // DVConstraint dvConstraint1 = DVConstraint.createExplicitListConstraint(strBusinessList); // DataValidation dataValidation = new HSSFDataValidation(businessList, dvConstraint1); // dataValidation.setSuppressDropDownArrow(false); // sheet.addValidationData(dataValidation); // CellRangeAddressList appList = new CellRangeAddressList(i, i, 2, 2); // DVConstraint dvConstraint2 = DVConstraint.createExplicitListConstraint(strApllication); // DataValidation dataValidation2 = new HSSFDataValidation(appList, dvConstraint2); // dataValidation.setSuppressDropDownArrow(false); // sheet.addValidationData(dataValidation2); // // CellRangeAddressList catList = new CellRangeAddressList(i, i, 1,1); // DVConstraint dvConstraint3 = DVConstraint.createExplicitListConstraint(strCategorymnt); // DataValidation dataValidation3 = new HSSFDataValidation(catList, dvConstraint3); // dataValidation.setSuppressDropDownArrow(false); // sheet.addValidationData(dataValidation3); // } Map<String, String[]> data = new LinkedHashMap<String, String[]>(); String[] stringVal = { "AssetName", "Category", "Application", "BusinessProcess", "InitialValue", "Quantity", "Location", "Location Country", "Location State", "Location City", "Location Contact Name", "Location Email" }; data.put(key.toString(), stringVal); Set<String> keyset = data.keySet(); int rownum = 0; sheet.setColumnHidden((short) 12, true); for (String key2 : keyset) { HSSFRow row = sheet.createRow(rownum++); String[] objArr = data.get(key2); short cellnum = 0; for (String obj : objArr) { if (obj instanceof String) { row.createCell(cellnum++).setCellValue((String) obj); } else if (obj instanceof String) { row.createCell(cellnum++).setCellValue((String) obj); } else if (obj instanceof String) { row.createCell(cellnum++).setCellValue((String) obj); } else if (obj instanceof String) { row.createCell(cellnum++).setCellValue((String) obj); } else if (obj instanceof String) { row.createCell(cellnum++).setCellValue((String) obj); } else if (obj instanceof String) { row.createCell(cellnum++).setCellValue((String) obj); } else if (obj instanceof String) { row.createCell(cellnum++).setCellValue((String) obj); } else if (obj instanceof String) { row.createCell(cellnum++).setCellValue((String) obj); } else if (obj instanceof String) { row.createCell(cellnum++).setCellValue((String) obj); } else if (obj instanceof String) { row.createCell(cellnum++).setCellValue((String) obj); } else if (obj instanceof String) { row.createCell(cellnum++).setCellValue((String) obj); } else if (obj instanceof String) { row.createCell(cellnum++).setCellValue((String) obj); } } } FileOutputStream out = new FileOutputStream(new File("" + docName + ".xls")); workbook.write(out); out.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (Exception ex) { ex.printStackTrace(); } }
From source file:com.astrocytes.core.statistics.LayerStatistics.java
License:Open Source License
public boolean saveLayerStatisticsToXls(Map<Integer, List<Point>> layers, List<Point> astrocyteCenters, List<Point> neuronsCenters, File fileToSave) { try {/* w w w. j ava2 s.co m*/ FileOutputStream outputStream = new FileOutputStream(fileToSave); HSSFWorkbook workbook = new HSSFWorkbook(); HSSFSheet sheet = workbook.createSheet(CoreConstants.XLS_SPREADSHEET_TITLE); HSSFRow headerRow = sheet.createRow(0); headerRow.createCell(0).setCellValue(CoreConstants.XLS_LAYERS_TITLE); for (int i = 0; i < layers.size() - 1; i++) { HSSFRow row = sheet.createRow(i + 1); HSSFCell cell = row.createCell(0); cell.setCellType(CellType.STRING); cell.setCellValue(String.valueOf(i < 1 ? i + 1 : (i == 1 ? "2 - 3" : i + 2))); } if (astrocyteCenters != null) { headerRow.createCell(1).setCellValue(CoreConstants.XLS_ASTROCYTES_TITLE); for (Map.Entry<Integer, Integer> count : count(astrocyteCenters, layers).entrySet()) { HSSFRow row = sheet.getRow(count.getKey() + 1); HSSFCell cell = row.createCell(1); cell.setCellType(CellType.NUMERIC); cell.setCellValue(count.getValue()); } } if (neuronsCenters != null) { int colIdx = astrocyteCenters != null ? 2 : 1; headerRow.createCell(colIdx).setCellValue(CoreConstants.XLS_NEURONS_TITLE); for (Map.Entry<Integer, Integer> count : count(neuronsCenters, layers).entrySet()) { HSSFRow row = sheet.getRow(count.getKey() + 1); HSSFCell cell = row.createCell(astrocyteCenters != null ? 2 : 1); cell.setCellType(CellType.NUMERIC); cell.setCellValue(count.getValue()); } } sheet.autoSizeColumn(0); sheet.autoSizeColumn(1); sheet.autoSizeColumn(2); workbook.write(outputStream); outputStream.close(); } catch (IOException e) { e.printStackTrace(); return false; } return true; }
From source file:com.bdx.rainbow.spsy.service.impl.EnterpriseLicenseServiceImpl.java
/** * * @param title ??//from w w w . j a v a 2 s .com * @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);// w ww. j a va 2 s . com 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 {/* w w w. j a v a 2s. c o m*/ 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); //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);//from w w w. ja v a 2s . co m } } 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 } }