List of usage examples for org.apache.poi.hssf.usermodel HSSFSheet createRow
@Override public HSSFRow createRow(int rownum)
From source file:com.krawler.esp.servlets.exportExcel.java
License:Open Source License
public void exportexcel(HttpServletResponse response, JSONObject jobj, java.util.Hashtable ht, String sheetTitle, String fileName, JSONArray hdr, JSONArray xlshdr, String heading, String[] xtypeArr, com.krawler.spring.exportFunctionality.exportDAOImpl exportDao) throws ServletException, IOException { try {//from w w w . j av a2s . c om response.setContentType("application/vnd.ms-excel"); if (!StringUtil.isNullOrEmpty(heading)) { fileName = heading + fileName; } response.setHeader("Content-Disposition", "attachement; filename=" + fileName + ".xls"); HSSFSheet sheet = wb.createSheet(sheetTitle); CellStyle cs = wb.createCellStyle(); cs.setWrapText(true); HSSFHeader hh = sheet.getHeader(); int j = 1; int width = 0; int maxrowno = 0; HSSFRow row1 = sheet.createRow((short) maxrowno); HashMap hm = extractData(jobj); JSONArray jarr = (JSONArray) hm.get("data"); JSONObject tempObj; for (int k = 0; k < jarr.length(); k++) { tempObj = jarr.getJSONObject(k); HSSFRow row = sheet.createRow((short) j); int cellcount = 0; for (int i = 0; i < hdr.length(); i++) { Object str = tempObj.optString(hdr.getString(i), ""); try { if (xtypeArr.length > 0) { str = convertValue(tempObj.optString(hdr.getString(i), ""), xtypeArr[i]); } } catch (Exception e) { } if (ht.containsValue(hdr.getString(i))) { if (j == maxrowno + 1) { HSSFCell cell1 = row1.createCell(cellcount); cell1.setCellStyle(cs); width = xlshdr.getString(i).length() * 325; if (width > sheet.getColumnWidth(cellcount)) { sheet.setColumnWidth(cellcount, width); } HSSFFont font = wb.createFont(); font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); HSSFRichTextString hst = new HSSFRichTextString(xlshdr.getString(i)); hst.applyFont(font); cell1.setCellValue(hst); } HSSFCell cell = row.createCell(cellcount); cell.setCellStyle(cs); if (str instanceof Date) { cal.setTime((Date) str); cell.setCellValue(cal); cell.setCellStyle(this.dateCellStyle); width = 4500; } else if (str instanceof Number) { cell.setCellValue(((Number) str).doubleValue()); width = 4500; } else { String colvalue = str.toString(); cell.setCellValue(new HSSFRichTextString(colvalue)); width = colvalue.length() * 325; } width = Math.min(width, MAX_CELL_WIDTH); if (width > sheet.getColumnWidth(cellcount)) { sheet.setColumnWidth(cellcount, width); } cellcount++; } } j++; } ConfigReader cr = ConfigReader.getinstance(); String dirpath = cr.get("store"); String path = dirpath + "baitheader.png"; // this.addimage(path,HSSFWorkbook.PICTURE_TYPE_PNG, wb, sheet,0,0,0,0,0,0,12,4); if (true) { OutputStream out = response.getOutputStream(); wb.write(out); out.close(); } } catch (JSONException ex) { Logger.getLogger(exportExcel.class.getName()).log(Level.SEVERE, null, ex); } catch (Exception ex) { Logger.getLogger(exportExcel.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:com.krawler.esp.servlets.exportExcel.java
License:Open Source License
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("application/vnd.ms-excel"); HSSFWorkbook wb = new HSSFWorkbook(); HSSFSheet sheet = wb.createSheet("new sheet"); // Create a row and put some cells in it. Rows are 0 based. HSSFRow row = sheet.createRow(0); HSSFCell cell = row.createCell(0);/*from w w w. ja va 2s . co m*/ cell.setCellValue(1); // Or do it on one line. row.createCell(1).setCellValue(1.2); row.createCell(3).setCellValue(true); // Write the output OutputStream out = response.getOutputStream(); wb.write(out); out.close(); }
From source file:com.learn.core.utils.HSSFReadWrite.java
License:Apache License
/** * given a filename this outputs a sample sheet with just a set of * rows/cells.//from w w w . j a v a 2 s .co m */ private static void testCreateSampleSheet(String outputFilename) throws IOException { try (HSSFWorkbook wb = new HSSFWorkbook()) { HSSFSheet s = wb.createSheet(); HSSFCellStyle cs = wb.createCellStyle(); HSSFCellStyle cs2 = wb.createCellStyle(); HSSFCellStyle cs3 = wb.createCellStyle(); HSSFFont f = wb.createFont(); HSSFFont f2 = wb.createFont(); f.setFontHeightInPoints((short) 12); f.setColor((short) 0xA); f.setBold(true); f2.setFontHeightInPoints((short) 10); f2.setColor((short) 0xf); f2.setBold(true); cs.setFont(f); cs.setDataFormat(HSSFDataFormat.getBuiltinFormat("($#,##0_);[Red]($#,##0)")); cs2.setBorderBottom(BorderStyle.THIN); cs2.setFillPattern(FillPatternType.SOLID_FOREGROUND); cs2.setFillForegroundColor((short) 0xA); cs2.setFont(f2); wb.setSheetName(0, "HSSF Test"); int rownum; for (rownum = 0; rownum < 300; rownum++) { HSSFRow r = s.createRow(rownum); if ((rownum % 2) == 0) { r.setHeight((short) 0x249); } for (int cellnum = 0; cellnum < 50; cellnum += 2) { HSSFCell c = r.createCell(cellnum); c.setCellValue( rownum * 10000 + cellnum + (((double) rownum / 1000) + ((double) cellnum / 10000))); if ((rownum % 2) == 0) { c.setCellStyle(cs); } c = r.createCell(cellnum + 1); c.setCellValue(new HSSFRichTextString("TEST")); // 50 characters divided by 1/20th of a point s.setColumnWidth(cellnum + 1, (int) (50 * 8 / 0.05)); if ((rownum % 2) == 0) { c.setCellStyle(cs2); } } } // draw a thick black border on the row at the bottom using BLANKS rownum++; rownum++; HSSFRow r = s.createRow(rownum); cs3.setBorderBottom(BorderStyle.THICK); for (int cellnum = 0; cellnum < 50; cellnum++) { HSSFCell c = r.createCell(cellnum); c.setCellStyle(cs3); } s.addMergedRegion(new CellRangeAddress(0, 3, 0, 3)); s.addMergedRegion(new CellRangeAddress(100, 110, 100, 110)); // end draw thick black border // create a sheet, set its title then delete it wb.createSheet(); wb.setSheetName(1, "DeletedSheet"); wb.removeSheetAt(1); // end deleted sheet try (FileOutputStream out = new FileOutputStream(outputFilename)) { wb.write(out); } } }
From source file:com.leosys.core.utils.ExcelUtil.java
public void exportExcel(List<?> dataList, OutputStream out) throws Exception { HSSFWorkbook workbook = null;/*from ww w .j a v a 2s . co m*/ HSSFSheet sheet = null; HSSFRow row = null; HSSFCell cell = null; HSSFCellStyle titleStyle = null; int rowIndex = 0; try { workbook = new HSSFWorkbook();// sheet = workbook.createSheet("?");// ? sheet.setDefaultColumnWidth((short) 30);// 15 titleStyle = workbook.createCellStyle();//? titleStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); titleStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); titleStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN); titleStyle.setBorderRight(HSSFCellStyle.BORDER_THIN); titleStyle.setBorderTop(HSSFCellStyle.BORDER_THIN); titleStyle.setFillForegroundColor(HSSFColor.WHITE.index); titleStyle.setFillBackgroundColor(HSSFColor.WHITE.index); titleStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); titleStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); HSSFFont font = workbook.createFont(); font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); font.setFontHeightInPoints((short) 17); titleStyle.setFont(font); row = sheet.createRow(rowIndex++); row.setHeight((short) 600); for (short i = 0; i < headArr.length; i++) { cell = row.createCell(i); //? if (i == 0) cell.setCellValue(new HSSFRichTextString(title)); cell.setCellStyle(titleStyle); } // ??? sheet.addMergedRegion(new Region(0, (short) 0, 0, (short) (headArr.length - 1))); titleStyle = workbook.createCellStyle(); titleStyle.setFillForegroundColor(HSSFColor.LIGHT_YELLOW.index); titleStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); titleStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); titleStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN); titleStyle.setBorderRight(HSSFCellStyle.BORDER_THIN); titleStyle.setBorderTop(HSSFCellStyle.BORDER_THIN); titleStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); titleStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); font = workbook.createFont(); font.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL); font.setFontHeightInPoints((short) 13); titleStyle.setFont(font);// ?? row = sheet.createRow(rowIndex++);// for (short i = 0; i < headArr.length; i++) { cell = row.createCell(i); cell.setCellStyle(titleStyle); cell.setCellValue(new HSSFRichTextString(headArr[i])); } //? titleStyle = workbook.createCellStyle(); titleStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); titleStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN); titleStyle.setBorderRight(HSSFCellStyle.BORDER_THIN); titleStyle.setBorderTop(HSSFCellStyle.BORDER_THIN); if (dataList == null || dataList.isEmpty()) return; short dataType = 0;// if (dataList.get(0) instanceof Map<?, ?>) dataType = 1; else if (dataList.get(0) instanceof List<?>) dataType = 2; if (dataType == 0) { String[] dataArr = null; for (Object data : dataList) { dataArr = (String[]) data; if (dataArr == null) continue; row = sheet.createRow(rowIndex++); for (short i = 0; i < headArr.length; i++) { if (i < dataArr.length) { Object val = dataArr[i]; if (rendererArr != null && rendererArr[i] != null) val = rendererArr[i].renderer(dataArr[i], i, dataArr); fillCell(row, titleStyle, font, i, val); } } } } else if (dataType == 1) { Map<?, ?> map = null; for (Object data : dataList) { map = (Map<?, ?>) data; if (map == null) continue; Object[] dataArr = map.values().toArray(); if (dataArr == null) continue; row = sheet.createRow(rowIndex++); for (short i = 0; i < headArr.length; i++) { if (i < dataArr.length) { Object val = dataArr[i]; if (rendererArr != null && rendererArr[i] != null) val = rendererArr[i].renderer(dataArr[i], i, dataArr); fillCell(row, titleStyle, font, i, val); } } } } else if (dataType == 2) { List<?> list = null; for (Object data : dataList) { list = (List<?>) data; if (list == null || list.isEmpty()) continue; row = sheet.createRow(rowIndex++); for (short i = 0; i < headArr.length; i++) { if (i < list.size()) { Object val = list.get(i); if (rendererArr != null && rendererArr[i] != null) val = rendererArr[i].renderer(list.get(i), i, list); fillCell(row, titleStyle, font, i, val); } } } } else throw new Exception("excel???"); workbook.write(out); } catch (Exception e) { throw new Exception("excel" + e.getMessage()); } finally { // if(out != null){ // try { // out.close(); // } catch (IOException e) {} // } } }
From source file:com.liferay.portlet.documentlibrary.action.EditEntryAction.java
License:Open Source License
public static void exportDocumentData(ResourceRequest resourceRequest, ResourceResponse resourceResponse) throws NumberFormatException, PortalException, SystemException { long file_id = 0; String fileEntryIds = ParamUtil.getString(resourceRequest, "fileEntryIds"); String[] fileentires = fileEntryIds.split(","); List<Long> tempResults = new ArrayList<Long>(); if (!fileEntryIds.isEmpty()) { if (fileentires[0].equals("true")) { for (int i = 1; i < fileentires.length; i++) { DLFileEntry FileEntry = DLFileEntryLocalServiceUtil .getDLFileEntry(Long.parseLong(fileentires[i])); file_id = FileEntry.getFileEntryId(); tempResults.add(file_id); }// w w w .j a v a 2s .c om } else { for (int i = 0; i < fileentires.length; i++) { DLFileEntry FileEntry = DLFileEntryLocalServiceUtil .getDLFileEntry(Long.parseLong(fileentires[i])); file_id = FileEntry.getFileEntryId(); tempResults.add(file_id); } } } try { HSSFWorkbook hwb = new HSSFWorkbook(); HSSFSheet sheet = hwb.createSheet("Site Information"); org.apache.poi.ss.usermodel.Font font = hwb.createFont(); font.setFontHeightInPoints((short) 11); font.setFontName("Arial"); font.setItalic(false); font.setStrikeout(false); font.setBoldweight(org.apache.poi.ss.usermodel.Font.BOLDWEIGHT_BOLD); CellStyle style = hwb.createCellStyle(); style.setBorderBottom(CellStyle.BORDER_THIN); style.setBorderLeft(CellStyle.BORDER_THIN); style.setBorderTop(CellStyle.BORDER_THIN); style.setBorderRight(CellStyle.BORDER_THIN); style.setBorderBottom(CellStyle.BORDER_THIN); style.setBottomBorderColor(HSSFColor.BLACK.index); style.setFont(font); CellStyle filterStyle = hwb.createCellStyle(); org.apache.poi.ss.usermodel.Font filterfont = hwb.createFont(); filterfont.setFontHeightInPoints((short) 9); filterfont.setFontName("Courier New"); filterfont.setItalic(false); filterfont.setStrikeout(false); filterfont.setBoldweight(org.apache.poi.ss.usermodel.Font.BOLDWEIGHT_BOLD); filterStyle.setFont(filterfont); filterStyle.setBorderBottom(CellStyle.BORDER_THIN); filterStyle.setBorderLeft(CellStyle.BORDER_THIN); filterStyle.setBorderTop(CellStyle.BORDER_THIN); filterStyle.setBorderRight(CellStyle.BORDER_THIN); HSSFRow rowhead = sheet.createRow((short) 2); rowhead.createCell((short) 0).setCellValue("S.No."); rowhead.createCell((short) 1).setCellValue("Title "); rowhead.createCell((short) 2).setCellValue("Site Name "); rowhead.createCell((short) 3).setCellValue("Category Type"); rowhead.createCell((short) 4).setCellValue("Com"); rowhead.createCell((short) 5).setCellValue("Upload Date"); rowhead.createCell((short) 6).setCellValue("File Type"); int index = 3; int sno = 0; for (int i = 0; i < tempResults.size(); i++) { sno++; DLFileEntry objdlfileentry = DLFileEntryLocalServiceUtil.getDLFileEntry(tempResults.get(i)); docs_customData objdocs_customData = null; try { objdocs_customData = docs_customDataLocalServiceUtil.getfileEntry(tempResults.get(i)); } catch (Exception e) { // TODO Auto-generated catch block //e.printStackTrace(); } String com = ""; if (objdocs_customData != null) { com = objdocs_customData.getCom(); } String siteName = ""; if (objdocs_customData != null) { siteName = objdocs_customData.getSite(); } String doccategory = ""; if (objdocs_customData != null) { doccategory = objdocs_customData.getCategory(); } HSSFRow row = sheet.createRow((short) index); row.createCell((short) 0).setCellValue(sno); row.createCell((short) 1).setCellValue(objdlfileentry.getTitle()); row.createCell((short) 2).setCellValue(siteName); row.createCell((short) 3).setCellValue(doccategory); row.createCell((short) 4).setCellValue(com); row.createCell((short) 5).setCellValue(objdlfileentry.getModifiedDate()); row.createCell((short) 6).setCellValue(objdlfileentry.getMimeType()); index++; } resourceResponse.setContentType("application/vnd.ms-excel"); resourceResponse.addProperty("Content-Disposition", "attachment;filename=Document_Listing.xls"); OutputStream fileOut = resourceResponse.getPortletOutputStream(); hwb.write(fileOut); fileOut.close(); } catch (Exception ex) { ex.printStackTrace(); } }
From source file:com.lingxiang2014.ExcelView.java
License:Open Source License
public void buildExcelDocument(Map<String, Object> model, HSSFWorkbook workbook, HttpServletRequest request, HttpServletResponse response) throws Exception { Assert.notEmpty(properties);/*from ww w .ja va 2 s . c o m*/ HSSFSheet sheet; if (StringUtils.isNotEmpty(sheetName)) { sheet = workbook.createSheet(sheetName); } else { sheet = workbook.createSheet(); } int rowNumber = 0; if (titles != null && titles.length > 0) { HSSFRow header = sheet.createRow(rowNumber); header.setHeight((short) 400); for (int i = 0; i < properties.length; i++) { HSSFCell cell = header.createCell(i); HSSFCellStyle cellStyle = workbook.createCellStyle(); cellStyle.setFillForegroundColor(HSSFColor.LIGHT_CORNFLOWER_BLUE.index); cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); HSSFFont font = workbook.createFont(); font.setFontHeightInPoints((short) 11); font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); cellStyle.setFont(font); cell.setCellStyle(cellStyle); if (i == 0) { HSSFPatriarch patriarch = sheet.createDrawingPatriarch(); HSSFComment comment = patriarch .createComment(new HSSFClientAnchor(0, 0, 0, 0, (short) 1, 1, (short) 4, 4)); comment.setString(new HSSFRichTextString("P" + "o" + "w" + "e" + "r" + "e" + "d" + " " + "B" + "y" + " " + "S" + "H" + "O" + "P" + "+" + "+")); cell.setCellComment(comment); } if (titles.length > i && titles[i] != null) { cell.setCellValue(titles[i]); } else { cell.setCellValue(properties[i]); } if (widths != null && widths.length > i && widths[i] != null) { sheet.setColumnWidth(i, widths[i]); } else { sheet.autoSizeColumn(i); } } rowNumber++; } if (data != null) { for (Object item : data) { HSSFRow row = sheet.createRow(rowNumber); for (int i = 0; i < properties.length; i++) { HSSFCell cell = row.createCell(i); if (converters != null && converters.length > i && converters[i] != null) { Class<?> clazz = PropertyUtils.getPropertyType(item, properties[i]); ConvertUtils.register(converters[i], clazz); cell.setCellValue(BeanUtils.getProperty(item, properties[i])); ConvertUtils.deregister(clazz); if (clazz.equals(Date.class)) { DateConverter dateConverter = new DateConverter(); dateConverter.setPattern(DEFAULT_DATE_PATTERN); ConvertUtils.register(dateConverter, Date.class); } } else { cell.setCellValue(BeanUtils.getProperty(item, properties[i])); } if (rowNumber == 0 || rowNumber == 1) { if (widths != null && widths.length > i && widths[i] != null) { sheet.setColumnWidth(i, widths[i]); } else { sheet.autoSizeColumn(i); } } } rowNumber++; } } if (contents != null && contents.length > 0) { rowNumber++; for (String content : contents) { HSSFRow row = sheet.createRow(rowNumber); HSSFCell cell = row.createCell(0); HSSFCellStyle cellStyle = workbook.createCellStyle(); HSSFFont font = workbook.createFont(); font.setColor(HSSFColor.GREY_50_PERCENT.index); cellStyle.setFont(font); cell.setCellStyle(cellStyle); cell.setCellValue(content); rowNumber++; } } response.setContentType("application/force-download"); if (StringUtils.isNotEmpty(filename)) { response.setHeader("Content-disposition", "attachment; filename=" + URLEncoder.encode(filename, "UTF-8")); } else { response.setHeader("Content-disposition", "attachment"); } }
From source file:com.lition.service.impl.OwnedServiceImpl.java
/** * POI??//from ww w . j ava 2 s . c o m */ @Override public InputStream getOutExcelDate() { //1.? String headTitle[] = { "id", "?", "??", "", "??" }; //2.Dao?? List<OwnedVehicle> list = dao.queryAll(); //3.?HSSFWorkbook HSSFWorkbook wb = new HSSFWorkbook(); //4.?sheet HSSFSheet sheet = wb.createSheet("?"); //5.? HSSFCellStyle style = wb.createCellStyle(); // ?? style.setAlignment(HSSFCellStyle.ALIGN_CENTER); style.setVerticalAlignment(HSSFCellStyle.ALIGN_CENTER); //6. HSSFRow row0 = sheet.createRow(0); //7.?? sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 4)); HSSFCell row0cell0 = row0.createCell(0); row0cell0.setCellValue("?"); row0cell0.setCellStyle(style); HSSFRow row1 = sheet.createRow(1); for (int i = 0; i < headTitle.length; i++) { HSSFCell row0cell = row1.createCell(i); row0cell.setCellValue(headTitle[i]); row0cell.setCellStyle(style); } for (int i = 0; i < list.size(); i++) { HSSFRow row = sheet.createRow(i + 2); //ID HSSFCell cell0 = row.createCell(0); cell0.setCellValue(list.get(i).getId()); cell0.setCellStyle(style); //? HSSFCell cell1 = row.createCell(1); cell1.setCellValue(list.get(i).getVehicleId()); cell1.setCellStyle(style); //?? HSSFCell cell2 = row.createCell(2); cell2.setCellValue(list.get(i).getDepid()); cell2.setCellStyle(style); // HSSFCell cell3 = row.createCell(3); cell3.setCellValue(list.get(i).getModel()); cell3.setCellStyle(style); //?? HSSFCell cell4 = row.createCell(4); cell4.setCellValue(list.get(i).getVehicleUsageId()); cell4.setCellStyle(style); } //?inputstream; try { OutputStream out = new FileOutputStream("abc.xls"); wb.write(out); out.close(); InputStream in = new FileInputStream("abc.xls"); return in; } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; }
From source file:com.lushapp.common.excel.ExcelUtil.java
License:Apache License
/** * @param excel_name//from w ww .ja v a 2 s .com * ?Excel+?? * @param headList * ExcelHead? * @param fieldList * ExcelField? * @param dataList * Excel? * @throws Exception */ public static void createExcel(String excel_name, String[] headList, String[] fieldList, List<Map<String, Object>> dataList) throws Exception { // Excel HSSFWorkbook workbook = new HSSFWorkbook(); // Excel??? // ???""? // HSSFSheet sheet = workbook.createSheet(""); HSSFSheet sheet = workbook.createSheet(); // 0? HSSFRow row = sheet.createRow(0); // =============================================================== for (int i = 0; i < headList.length; i++) { // 0?? HSSFCell cell = row.createCell(i); // ? cell.setCellType(HSSFCell.CELL_TYPE_STRING); // ? cell.setCellValue(headList[i]); } // =============================================================== for (int n = 0; n < dataList.size(); n++) { // 1? HSSFRow row_value = sheet.createRow(n + 1); Map<String, Object> dataMap = dataList.get(n); // =============================================================== for (int i = 0; i < fieldList.length; i++) { // 0?? HSSFCell cell = row_value.createCell(i); // ? cell.setCellType(HSSFCell.CELL_TYPE_STRING); // ? cell.setCellValue(objToString(dataMap.get(fieldList[i]))); } // =============================================================== } // ? FileOutputStream fOut = new FileOutputStream(excel_name); // Excel workbook.write(fOut); fOut.flush(); // ?? fOut.close(); //System.out.println("[" + excel_name + "]" + "?..."); }
From source file:com.lushapp.common.excel.ExcelUtil.java
License:Apache License
/** * @param excel_name/*from w w w .j a v a2s. co m*/ * ?Excel+?? * @param headList * ExcelHead? * @param fieldList * ExcelField? * @param dataList * Excel? * @throws Exception */ public static void createExcel(String excel_name, List<String> headList, List<String> fieldList, List<Map<String, Object>> dataList) throws Exception { // Excel HSSFWorkbook workbook = new HSSFWorkbook(); // Excel??? // ???""? // HSSFSheet sheet = workbook.createSheet(""); HSSFSheet sheet = workbook.createSheet(); // 0? HSSFRow row = sheet.createRow(0); // =============================================================== for (int i = 0; i < headList.size(); i++) { // 0?? HSSFCell cell = row.createCell(i); // ? cell.setCellType(HSSFCell.CELL_TYPE_STRING); // ? cell.setCellValue(headList.get(i)); } // =============================================================== for (int n = 0; n < dataList.size(); n++) { // 1? HSSFRow row_value = sheet.createRow(n + 1); Map<String, Object> dataMap = dataList.get(n); // =============================================================== for (int i = 0; i < fieldList.size(); i++) { // 0?? HSSFCell cell = row_value.createCell(i); // ? cell.setCellType(HSSFCell.CELL_TYPE_STRING); // ? cell.setCellValue(objToString(dataMap.get(fieldList.get(i)))); } // =============================================================== } // ? FileOutputStream fOut = new FileOutputStream(excel_name); // Excel workbook.write(fOut); fOut.flush(); // ?? fOut.close(); //System.out.println("[" + excel_name + "]" + "?..."); }
From source file:com.lushapp.common.excel.ExcelUtil.java
License:Apache License
/** * @param headList//w ww . j av a 2s .c o m * ExcelHead? * @param fieldList * ExcelField? * @param dataList * Excel? * @throws HSSFWorkbook */ public static HSSFWorkbook createExcel(List<String> headList, List<String> fieldList, List<Map<String, Object>> dataList) throws Exception { // Excel HSSFWorkbook workbook = new HSSFWorkbook(); // Excel??? // ???""? // HSSFSheet sheet = workbook.createSheet(""); HSSFSheet sheet = workbook.createSheet(); // 0? HSSFRow row = sheet.createRow(0); // =============================================================== for (int i = 0; i < headList.size(); i++) { // 0?? HSSFCell cell = row.createCell(i); // ? cell.setCellType(HSSFCell.CELL_TYPE_STRING); // ? cell.setCellValue(headList.get(i)); } // =============================================================== for (int n = 0; n < dataList.size(); n++) { // 1? HSSFRow row_value = sheet.createRow(n + 1); Map<String, Object> dataMap = dataList.get(n); // =============================================================== for (int i = 0; i < fieldList.size(); i++) { // 0?? HSSFCell cell = row_value.createCell(i); // ? cell.setCellType(HSSFCell.CELL_TYPE_STRING); // ? cell.setCellValue(objToString(dataMap.get(fieldList.get(i)))); } // =============================================================== } return workbook; }