List of usage examples for org.apache.poi.ss.usermodel CreationHelper createClientAnchor
ClientAnchor createClientAnchor();
From source file:de.enerko.reports2.engine.Report.java
License:Apache License
/** * This method adds a new cell to the sheet of a workbook. It could * (together with {@link #fill(Workbook, Cell, String, String, boolean)}) be moved to * the {@link CellDefinition} itself, but that would mean that the {@link CellDefinition} is * tied to a specific Excel API. Having those methods here allows the Report to become * an interface if a second engine (i.e. JXL) should be added in the future. * @param workbook//from w w w . j a va2 s.c om * @param sheet * @param cellDefinition */ private void addCell(final Workbook workbook, final Sheet sheet, final CellDefinition cellDefinition) { final int columnNum = cellDefinition.column, rowNum = cellDefinition.row; Row row = sheet.getRow(rowNum); if (row == null) row = sheet.createRow(rowNum); Cell cell = row.getCell(columnNum); // If the cell already exists and is no blank cell // it will be used including all formating if (cell != null && cell.getCellType() != Cell.CELL_TYPE_BLANK) { cell = fill(workbook, cell, cellDefinition, false); } // Otherwise a new cell will be created, the datatype set and // optionally a format will be created else { cell = fill(workbook, row.createCell(columnNum), cellDefinition, true); final Sheet referenceSheet; if (cellDefinition.getReferenceCell() != null && (referenceSheet = workbook.getSheet(cellDefinition.getReferenceCell().sheetname)) != null) { final Row referenceRow = referenceSheet.getRow(cellDefinition.getReferenceCell().row); final Cell referenceCell = referenceRow == null ? null : referenceRow.getCell(cellDefinition.getReferenceCell().column); if (referenceCell != null && referenceCell.getCellStyle() != null) cell.setCellStyle(referenceCell.getCellStyle()); } } // Add an optional comment if (cellDefinition.hasComment()) { final CreationHelper factory = workbook.getCreationHelper(); final Drawing drawing = sheet.createDrawingPatriarch(); final ClientAnchor commentAnchor = factory.createClientAnchor(); final int col1 = cellDefinition.comment.column == null ? cell.getColumnIndex() + 1 : cellDefinition.comment.column; final int row1 = cellDefinition.comment.row == null ? cell.getRowIndex() : cellDefinition.comment.row; commentAnchor.setCol1(col1); commentAnchor.setRow1(row1); commentAnchor.setCol2(col1 + Math.max(1, cellDefinition.comment.width)); commentAnchor.setRow2(row1 + Math.max(1, cellDefinition.comment.height)); final Comment comment = drawing.createCellComment(commentAnchor); comment.setString(factory.createRichTextString(cellDefinition.comment.text)); comment.setAuthor(cellDefinition.comment.author); comment.setVisible(cellDefinition.comment.visible); cell.setCellComment(comment); } }
From source file:de.iteratec.iteraplan.businesslogic.exchange.elasticExcel.export.template.AbstractIntroSheetGenerator.java
License:Open Source License
private void insertIteraplanLogo(int colIndex, int rowIndex) { CreationHelper helper = getWorkbook().getCreationHelper(); byte[] bytes; try {/*from w w w . j a v a 2 s . c om*/ InputStream is = logoImage.getInputStream(); bytes = IOUtils.toByteArray(is); } catch (IOException e) { LOGGER.error("Could not read the excel template!", e); throw new IteraplanTechnicalException(IteraplanErrorMessages.INVALID_EXCEL_TEMPLATE); } int pictureIdx = getWorkbook().addPicture(bytes, Workbook.PICTURE_TYPE_PNG); Sheet sheet = getIntroductionSheet(); Drawing drawing = sheet.createDrawingPatriarch(); ClientAnchor anchor = helper.createClientAnchor(); //set top-left corner of the picture anchor.setCol1(colIndex); anchor.setRow1(rowIndex); Picture pict = drawing.createPicture(anchor, pictureIdx); pict.resize(); }
From source file:de.iteratec.iteraplan.businesslogic.exchange.legacyExcel.exporter.ExportWorkbook.java
License:Open Source License
/** * Adds headers stored in <code>headers</code> to the current sheet. If required, a special width * for the corresponding column can be set by providing a value in <code>headersWidth</code> using * header as key.<br/>//from w ww . j ava 2 s . c o m * <b>IMPORTANT</b>: Headers are added in the order provided in <code>headers</code>. * * @param headers * headers to be added */ public void addHeaders(int sheetId, List<ExcelSheet.Header> headers) { Sheet sheet = getSheetById(sheetId); Drawing drawing = sheet.createDrawingPatriarch(); CreationHelper factory = sheet.getWorkbook().getCreationHelper(); Row row = sheet.createRow(this.getCurrentRowOfSheet(sheet, 3)); int columnIndex = 0; for (ExcelSheet.Header header : headers) { int currColumnIndex = columnIndex; Cell cell = row.createCell(columnIndex); if (header.getDescription() != null) { ClientAnchor commentAnchor = factory.createClientAnchor(); //Sizing the comment 1x3 cells commentAnchor.setCol1(cell.getColumnIndex()); commentAnchor.setCol2(cell.getColumnIndex() + 1); commentAnchor.setRow1(row.getRowNum()); commentAnchor.setRow2(row.getRowNum() + 3); Comment comment = drawing.createCellComment(commentAnchor); RichTextString str = factory.createRichTextString(header.getDescription()); comment.setString(str); comment.setAuthor(""); cell.setCellComment(comment); } setCellValue(cell, header.getLabel(), getHeaderTableStyle()); Integer width = header.getWidth(); if (width != null) { sheet.setColumnWidth(currColumnIndex, width.intValue()); } columnIndex++; } LOGGER.debug("Added headers."); }
From source file:de.viaboxx.nlstools.formats.MBExcelPersistencer.java
License:Apache License
private Comment comment(HSSFCell cell, String text) { CreationHelper factory = wb.getCreationHelper(); Drawing drawing = sheet.createDrawingPatriarch(); // When the comment box is visible, have it show in a 1x3 space ClientAnchor anchor = factory.createClientAnchor(); anchor.setCol1(cell.getColumnIndex()); anchor.setCol2(cell.getColumnIndex() + 1); anchor.setRow1(cell.getRow().getRowNum()); anchor.setRow2(cell.getRow().getRowNum() + 3); Comment comment = drawing.createCellComment(anchor); RichTextString str = factory.createRichTextString(text); comment.setString(str);//from ww w .j av a 2 s. co m cell.setCellComment(comment); return comment; }
From source file:domain.Excel.java
private void cargarLogo1(Integer fila, Integer columna, Double resize) { InputStream inputStream;// w w w.j av a2 s . c om try { inputStream = getClass().getResourceAsStream("/resources/logo tru-test.png");//Tru-Test.jpg"); byte[] bytes = IOUtils.toByteArray(inputStream); int pictureIdx = wb.addPicture(bytes, wb.PICTURE_TYPE_PNG); inputStream.close(); CreationHelper helper = wb.getCreationHelper(); Drawing drawing = sheet.createDrawingPatriarch(); ClientAnchor anchor = helper.createClientAnchor(); //set top-left corner for the image anchor.setCol1(columna); anchor.setRow1(fila); Picture pict = drawing.createPicture(anchor, pictureIdx); //Reset the image to the original size pict.resize(resize); } catch (FileNotFoundException ex) { Logger.getLogger(Excel.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) { Logger.getLogger(Excel.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:domain.Excel.java
private void cargarLogo2(Integer fila, Integer columna, Double resize) { InputStream inputStream;/*from www.ja va 2 s .c o m*/ try { inputStream = getClass().getResourceAsStream("/resources/LOGO.png");//Tru-Test.jpg"); byte[] bytes = IOUtils.toByteArray(inputStream); int pictureIdx = wb.addPicture(bytes, wb.PICTURE_TYPE_PNG); inputStream.close(); CreationHelper helper = wb.getCreationHelper(); Drawing drawing = sheet.createDrawingPatriarch(); ClientAnchor anchor = helper.createClientAnchor(); //set top-left corner for the image anchor.setCol1(columna); anchor.setRow1(fila); Picture pict = drawing.createPicture(anchor, pictureIdx); //Reset the image to the original size pict.resize(resize); } catch (FileNotFoundException ex) { Logger.getLogger(Excel.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) { Logger.getLogger(Excel.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:excel.CellComments.java
License:Apache License
public static void main(String[] args) throws IOException { try (Workbook wb = new XSSFWorkbook()) { CreationHelper factory = wb.getCreationHelper(); Sheet sheet = wb.createSheet();/*from ww w . j a v a 2s . com*/ Cell cell1 = sheet.createRow(3).createCell(5); cell1.setCellValue("F4"); Drawing<?> drawing = sheet.createDrawingPatriarch(); ClientAnchor anchor = factory.createClientAnchor(); Comment comment1 = drawing.createCellComment(anchor); RichTextString str1 = factory.createRichTextString("Hello, World!"); comment1.setString(str1); comment1.setAuthor("Apache POI"); cell1.setCellComment(comment1); Cell cell2 = sheet.createRow(2).createCell(2); cell2.setCellValue("C3"); Comment comment2 = drawing.createCellComment(anchor); RichTextString str2 = factory.createRichTextString("XSSF can set cell comments"); //apply custom font to the text in the comment Font font = wb.createFont(); font.setFontName("Arial"); font.setFontHeightInPoints((short) 14); font.setBold(true); font.setColor(IndexedColors.RED.getIndex()); str2.applyFont(font); comment2.setString(str2); comment2.setAuthor("Apache POI"); comment2.setAddress(new CellAddress("C3")); try (FileOutputStream out = new FileOutputStream("comments.xlsx")) { wb.write(out); } } }
From source file:functions.excels.Excel.java
License:Apache License
/** * Colle le logo en haut gauche de la page donne. * @param page/*from w ww . ja v a 2s .c o m*/ * @throws IOException */ public void collerLogo(int page) throws IOException { InputStream is = new FileInputStream("public/images/banniere-aer.png"); byte[] bytes = IOUtils.toByteArray(is); int pictureIdx = wb.addPicture(bytes, Workbook.PICTURE_TYPE_PNG); is.close(); CreationHelper helper = wb.getCreationHelper(); Sheet sheet = wb.getSheetAt(0); // Create the drawing patriarch. This is the top level container for all shapes. Drawing drawing = sheet.createDrawingPatriarch(); //add a picture shape ClientAnchor anchor = helper.createClientAnchor(); //set top-left corner of the picture, //subsequent call of Picture#resize() will operate relative to it anchor.setCol1(0); anchor.setRow1(LIGNES * page); Picture pict = drawing.createPicture(anchor, pictureIdx); //auto-size picture relative to its top-left corner pict.resize(); }
From source file:functions.excels.Excel.java
License:Apache License
/** * Insre la carte en paramtre la page donne * @param carte/*from ww w . j a v a 2 s . c om*/ * @param page * @throws IOException */ public void pasteMap(Carte carte, int page) throws IOException { ByteArrayOutputStream os = new ByteArrayOutputStream(); ImageIO.write(carte.getImage(), "png", os); byte[] bytes = os.toByteArray(); int pictureIdx = wb.addPicture(bytes, Workbook.PICTURE_TYPE_PNG); CreationHelper helper = wb.getCreationHelper(); Sheet sheet = wb.getSheetAt(0); // Create the drawing patriarch. This is the top level container for all shapes. Drawing drawing = sheet.createDrawingPatriarch(); //add a picture shape ClientAnchor anchor = helper.createClientAnchor(); //set top-left corner of the picture, //subsequent call of Picture#resize() will operate relative to it anchor.setCol1(4); anchor.setRow1(LIGNES * page + 10); Picture pict = drawing.createPicture(anchor, pictureIdx); //auto-size picture relative to its top-left corner pict.resize(); }
From source file:info.informationsea.tableio.excel.ExcelImageSheetWriter.java
License:Open Source License
public void addImage(ImageType type, byte[] data) { int pictureType; switch (type) { case TYPE_JPEG: pictureType = Workbook.PICTURE_TYPE_JPEG; break;/* w w w . j a va2 s. co m*/ case TYPE_PNG: pictureType = Workbook.PICTURE_TYPE_PNG; break; default: throw new IllegalArgumentException("Image type should be jpeg or png"); } int pictureIndex = sheet.getWorkbook().addPicture(data, pictureType); CreationHelper creationHelper = sheet.getWorkbook().getCreationHelper(); Drawing drawing = sheet.createDrawingPatriarch(); ClientAnchor anchor = creationHelper.createClientAnchor(); anchor.setCol1(1); anchor.setRow1(nextRow); Picture picture = drawing.createPicture(anchor, pictureIndex); picture.resize(); nextRow = picture.getPreferredSize().getRow2() + 1; }