List of usage examples for org.apache.poi.hssf.usermodel HSSFSheet setGridsPrinted
public void setGridsPrinted(boolean value)
From source file:com.eryansky.core.excelTools.ExcelUtils.java
License:Apache License
public static void copySheetStyle(HSSFWorkbook destwb, HSSFSheet dest, HSSFWorkbook srcwb, HSSFSheet src) { if (src == null || dest == null) return;//w ww . j a v a2 s. c o m dest.setAlternativeExpression(src.getAlternateExpression()); dest.setAlternativeFormula(src.getAlternateFormula()); dest.setAutobreaks(src.getAutobreaks()); dest.setDialog(src.getDialog()); if (src.getColumnBreaks() != null) { for (int col : src.getColumnBreaks()) { dest.setColumnBreak(col); } } dest.setDefaultColumnWidth(src.getDefaultColumnWidth()); dest.setDefaultRowHeight(src.getDefaultRowHeight()); dest.setDefaultRowHeightInPoints(src.getDefaultRowHeightInPoints()); dest.setDisplayGuts(src.getDisplayGuts()); dest.setFitToPage(src.getFitToPage()); dest.setHorizontallyCenter(src.getHorizontallyCenter()); dest.setDisplayFormulas(src.isDisplayFormulas()); dest.setDisplayGridlines(src.isDisplayGridlines()); dest.setDisplayRowColHeadings(src.isDisplayRowColHeadings()); dest.setGridsPrinted(src.isGridsPrinted()); dest.setPrintGridlines(src.isPrintGridlines()); for (int i = 0; i < src.getNumMergedRegions(); i++) { CellRangeAddress r = src.getMergedRegion(i); dest.addMergedRegion(r); } if (src.getRowBreaks() != null) { for (int row : src.getRowBreaks()) { dest.setRowBreak(row); } } dest.setRowSumsBelow(src.getRowSumsBelow()); dest.setRowSumsRight(src.getRowSumsRight()); int maxcol = 0; for (int i = 0; i <= src.getLastRowNum(); i++) { HSSFRow row = src.getRow(i); if (row != null) { if (maxcol < row.getLastCellNum()) maxcol = row.getLastCellNum(); } } for (int col = 0; col <= maxcol; col++) { if (src.getColumnWidth(col) != src.getDefaultColumnWidth()) dest.setColumnWidth(col, src.getColumnWidth(col)); dest.setColumnHidden(col, src.isColumnHidden(col)); } }
From source file:com.eryansky.core.excelTools.JsGridReportBase.java
License:Apache License
/** * //from www . jav a 2 s . co m * @param wb Excel * @param title Sheet?? * @param styles ? * @param creator * @param tableData ? * @throws Exception */ public HSSFWorkbook writeSheet(HSSFWorkbook wb, String title, HashMap<String, HSSFCellStyle> styles, String creator, TableData tableData) throws Exception { SimpleDateFormat formater = new SimpleDateFormat("yyyyMMdd HHmm"); String create_time = formater.format(new Date()); HSSFSheet sheet = wb.createSheet(title);// Excel sheet.setDisplayGridlines(false);// ? HSSFRow row = sheet.createRow(0);// HSSFCell cell = row.createCell(0);// int rownum = 0; cell.setCellValue(new HSSFRichTextString(title)); HSSFCellStyle style = styles.get("TITLE"); if (style != null) cell.setCellStyle(style); TableHeaderMetaData headerMetaData = tableData.getTableHeader();// HTML sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, headerMetaData.getColumnCount() - 1)); row = sheet.createRow(1); cell = row.createCell(0); cell.setCellValue(new HSSFRichTextString(":")); style = styles.get("SUB_TITLE"); if (style != null) cell.setCellStyle(style); cell = row.createCell(1); cell.setCellValue(new HSSFRichTextString(creator)); style = styles.get("SUB_TITLE2"); if (style != null) cell.setCellStyle(style); cell = row.createCell(2); cell.setCellValue(new HSSFRichTextString(":")); style = styles.get("SUB_TITLE"); if (style != null) cell.setCellStyle(style); cell = row.createCell(3); style = styles.get("SUB_TITLE2"); cell.setCellValue(new HSSFRichTextString(create_time)); if (style != null) cell.setCellStyle(style); rownum = 3;// rownum = 1? HSSFCellStyle headerstyle = styles.get("TABLE_HEADER"); System.out.println(JsonMapper.getInstance().toJson(headerMetaData)); int colnum = 0; for (int i = 0; i < headerMetaData.getOriginColumns().size(); i++) { TableColumn tc = headerMetaData.getOriginColumns().get(i); if (i != 0) { colnum += headerMetaData.getOriginColumns().get(i - 1).getLength(); } generateColumn(sheet, tc, headerMetaData.maxlevel, rownum, colnum, headerstyle); } rownum += headerMetaData.maxlevel; List<TableDataRow> dataRows = tableData.getRows(); HashMap<Integer, Integer> counter = new HashMap<Integer, Integer>(); HashMap<Integer, String> word = new HashMap<Integer, String>(); int index = 0; for (TableDataRow dataRow : dataRows) { row = sheet.createRow(rownum); List<TableDataCell> dataCells = dataRow.getCells(); int size = headerMetaData.getColumns().size(); index = -1; for (int i = 0; i < size; i++) { TableColumn tc = headerMetaData.getColumns().get(i); if (!tc.isVisible()) continue; index++; String value = dataCells.get(i).getValue(); if (tc.isGrouped()) { String w = word.get(index); if (w == null) { word.put(index, value); counter.put(index, 1); createCell(row, tc, dataCells, i, index, styles); } else { if (w.equals(value)) { counter.put(index, counter.get(index) + 1); } else { stopGrouping(sheet, word, counter, index, size, rownum, styles.get("STRING")); word.put(index, value); counter.put(index, 1); createCell(row, tc, dataCells, i, index, styles); } } } else { createCell(row, tc, dataCells, i, index, styles); } } rownum++; } stopGrouping(sheet, word, counter, 0, index, rownum, styles.get("STRING")); // ??? for (int c = 0; c < headerMetaData.getColumns().size(); c++) { sheet.autoSizeColumn((short) c, true); } sheet.setGridsPrinted(true); return wb; }
From source file:org.jasig.portlets.FeedbackPortlet.web.ExcelFeedbackView.java
License:Apache License
@Override protected void buildExcelDocument(Map model, HSSFWorkbook wb, HttpServletRequest request, HttpServletResponse response) throws Exception { // TODO Auto-generated method stub model = (HashMap) model.get("model"); int rowCount = 0; response.setHeader("Content-Disposition", "attachment; filename=\"feedback.xls\""); // this encourages it to default as a 'feedback.xls' file name // set up the sheet HSSFSheet sheet = wb.createSheet("uPortal Feedback"); sheet.setGridsPrinted(true); // create header row HSSFRow header = sheet.createRow(rowCount); header.createCell((short) 0).setCellValue(new HSSFRichTextString("User Id")); header.createCell((short) 1).setCellValue(new HSSFRichTextString("User Name")); header.createCell((short) 2).setCellValue(new HSSFRichTextString("User Email")); header.createCell((short) 3).setCellValue(new HSSFRichTextString("User Role")); header.createCell((short) 4).setCellValue(new HSSFRichTextString("Useragent")); header.createCell((short) 5).setCellValue(new HSSFRichTextString("Submitted")); header.createCell((short) 6).setCellValue(new HSSFRichTextString("Tab Name")); header.createCell((short) 7).setCellValue(new HSSFRichTextString("Liked?")); header.createCell((short) 8).setCellValue(new HSSFRichTextString("Feedback")); // add each feedback item to the sheet List<FeedbackItem> feedback = (List<FeedbackItem>) model.get("feedback"); SimpleDateFormat dateFormatter = new SimpleDateFormat(FeedbackQueryParameters.DATE_FORMAT); for (Iterator<FeedbackItem> iter = feedback.iterator(); iter.hasNext();) { rowCount++;//from w ww. j av a2 s . co m FeedbackItem item = iter.next(); HSSFRow row = sheet.createRow(rowCount); row.createCell((short) 0).setCellValue(new HSSFRichTextString(item.getUserid())); row.createCell((short) 1).setCellValue(new HSSFRichTextString(item.getUsername())); row.createCell((short) 2).setCellValue(new HSSFRichTextString(item.getUseremail())); row.createCell((short) 3).setCellValue(new HSSFRichTextString(item.getUserrole())); row.createCell((short) 4).setCellValue(new HSSFRichTextString(item.getUseragent())); row.createCell((short) 5) .setCellValue(new HSSFRichTextString(dateFormatter.format(item.getSubmissiontime()))); // formats to be readable row.createCell((short) 6).setCellValue(new HSSFRichTextString(item.getTabname())); row.createCell((short) 7).setCellValue(new HSSFRichTextString(item.getFeedbacktype())); row.createCell((short) 8).setCellValue(new HSSFRichTextString(item.getFeedback())); } }