List of usage examples for org.apache.poi.hssf.usermodel HSSFWorkbook writeProtectWorkbook
public void writeProtectWorkbook(String password, String username)
From source file:org.egov.infra.web.displaytag.export.EGovExcelReadOnlyView.java
License:Open Source License
/** * @see org.displaytag.export.BinaryExportView#doExport(OutputStream) *///ww w .j av a2s .c o m @Override public void doExport(final OutputStream out) throws JspException { try { final HSSFWorkbook wb = new HSSFWorkbook(); wb.writeProtectWorkbook("egov", "egov");// To make the workbook read-only this.sheet = wb.createSheet("-"); int rowNum = 0; int colNum = 0; if (this.header) { // Create an header row final HSSFRow xlsRow = this.sheet.createRow(rowNum++); final HSSFCellStyle headerStyle = wb.createCellStyle(); headerStyle.setFillPattern(HSSFCellStyle.FINE_DOTS); headerStyle.setFillBackgroundColor(HSSFColor.BLUE_GREY.index); final HSSFFont bold = wb.createFont(); bold.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); bold.setColor(HSSFColor.WHITE.index); headerStyle.setFont(bold); final Iterator iterator = this.model.getHeaderCellList().iterator(); while (iterator.hasNext()) { final HeaderCell headerCell = (HeaderCell) iterator.next(); String columnHeader = headerCell.getTitle(); if (columnHeader == null) { columnHeader = StringUtils.capitalize(headerCell.getBeanPropertyName()); } final HSSFCell cell = xlsRow.createCell(colNum++); cell.setCellValue(escapeColumnValue(columnHeader)); cell.setCellStyle(headerStyle); } } // get the correct iterator (full or partial list according to the exportFull field) final RowIterator rowIterator = this.model.getRowIterator(this.exportFull); // iterator on rows while (rowIterator.hasNext()) { final Row row = rowIterator.next(); final HSSFRow xlsRow = this.sheet.createRow(rowNum++); colNum = 0; // iterator on columns final ColumnIterator columnIterator = row.getColumnIterator(this.model.getHeaderCellList()); while (columnIterator.hasNext()) { final Column column = columnIterator.nextColumn(); // Get the value to be displayed for the column final Object value = column.getValue(this.decorated); final HSSFCell cell = xlsRow.createCell(colNum++); if (value instanceof Number) { final Number num = (Number) value; cell.setCellValue(num.doubleValue()); } else if (value instanceof Date) { cell.setCellValue((Date) value); } else if (value instanceof Calendar) { cell.setCellValue((Calendar) value); } else { cell.setCellValue(escapeColumnValue(value)); } } } for (short i = 0; i < colNum; i++) { this.sheet.autoSizeColumn(i, true); } wb.write(out); } catch (final Exception e) { throw new ExcelGenerationException(e); } }