private void insertItemToSheet(String table, HSSFSheet sheet, ArrayList<String> columns) { HSSFPatriarch patriarch = sheet.createDrawingPatriarch(); Cursor cursor = database.rawQuery("select * from " + table, null); cursor.moveToFirst(); int n = 1; while (!cursor.isAfterLast()) { HSSFRow rowA = sheet.createRow(n); for (int j = 0; j < columns.size(); j++) { HSSFCell cellA = rowA.createCell(j); if (cursor.getType(j) == Cursor.FIELD_TYPE_BLOB) { HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 0, 0, (short) j, n, (short) (j + 1), n + 1); anchor.setAnchorType(3); patriarch.createPicture(anchor, workbook.addPicture(cursor.getBlob(j), HSSFWorkbook.PICTURE_TYPE_JPEG)); } else { cellA.setCellValue(new HSSFRichTextString(cursor.getString(j))); } } n++; cursor.moveToNext(); } cursor.close(); }
/** * 获取Excel2003图片 * * @param sheet * 当前sheet对象 * @param workbook * 工作簿对象 * @return Map key:图片单元格索引(1_1)String,value:图片流PictureData */ public static Map<String, PictureData> getSheetPictrues03(HSSFSheet sheet, HSSFWorkbook workbook) { Map<String, PictureData> sheetIndexPicMap = new HashMap<String, PictureData>(); List<HSSFPictureData> pictures = workbook.getAllPictures(); if (!pictures.isEmpty()) { for (HSSFShape shape : sheet.getDrawingPatriarch().getChildren()) { HSSFClientAnchor anchor = (HSSFClientAnchor) shape.getAnchor(); if (shape instanceof HSSFPicture) { HSSFPicture pic = (HSSFPicture) shape; int pictureIndex = pic.getPictureIndex() - 1; HSSFPictureData picData = pictures.get(pictureIndex); String picIndex = String.valueOf(anchor.getRow1()) + "_" + String.valueOf(anchor.getCol1()); sheetIndexPicMap.put(picIndex, picData); } } return sheetIndexPicMap; } else { return sheetIndexPicMap; } }
/** * 获取Excel2003图片 * * @param sheet * 当前sheet对象 * @param workbook * 工作簿对象 * @return Map key:图片单元格索引(1_1)String,value:图片流PictureData */ public static Map<String, PictureData> getSheetPictrues03(HSSFSheet sheet, HSSFWorkbook workbook) { Map<String, PictureData> sheetIndexPicMap = new HashMap<String, PictureData>(); List<HSSFPictureData> pictures = workbook.getAllPictures(); if (!pictures.isEmpty()) { for (HSSFShape shape : sheet.getDrawingPatriarch().getChildren()) { HSSFClientAnchor anchor = (HSSFClientAnchor) shape.getAnchor(); if (shape instanceof HSSFPicture) { HSSFPicture pic = (HSSFPicture) shape; int pictureIndex = pic.getPictureIndex() - 1; HSSFPictureData picData = pictures.get(pictureIndex); String picIndex = String.valueOf(anchor.getRow1()) + "_" + String.valueOf(anchor.getCol1()); sheetIndexPicMap.put(picIndex, picData); } } return sheetIndexPicMap; } else { return null; } }
private static Table<Integer, Integer, ImageData> readAllCellImages(HSSFPatriarch patriarch, Sheet sheet) { val images = HashBasedTable.<Integer, Integer, ImageData>create(); val allPictures = sheet.getWorkbook().getAllPictures(); for (val shape : patriarch.getChildren()) { if (!(shape instanceof HSSFPicture && shape.getAnchor() instanceof HSSFClientAnchor)) continue; val picture = (HSSFPicture) shape; val imageData = createImageData(allPictures.get(picture.getPictureIndex() - 1)); val axisRow = computeAxisRowIndex(sheet, picture); val axisCol = computeAxisColIndex(sheet, picture); images.put(axisRow, axisCol, imageData); } return images; }
/** * Output Datums in Characterization Results for work sheet. * * @param rowIndex * @param filePath * @param wb * @param sheet */ public static int createImage(int rowIndex, short colIndex, String filePath, HSSFWorkbook wb, HSSFSheet sheet, HSSFPatriarch patriarch) throws IOException { short topLeftCell = colIndex; short bottomRightCell = (short) (colIndex + 7); int topLeftRow = rowIndex + 1; int bottomRightRow = rowIndex + 22; HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 0, 255, topLeftCell, topLeftRow, bottomRightCell, bottomRightRow); anchor.setAnchorType(2); // 2 = Move but don't size with cells patriarch.createPicture(anchor, loadPicture(filePath, wb)); rowIndex = bottomRightRow + 3; return rowIndex; }
public static List<HSSFClientAnchor> getAllAnchors(EscherAggregate escherAggregate) { List<HSSFClientAnchor> pictures = new ArrayList<HSSFClientAnchor>(); if (escherAggregate == null) return Collections.emptyList(); List<EscherRecord> escherRecords = escherAggregate.getEscherRecords(); searchForAnchors(escherRecords, pictures); return pictures; }
@Override protected void writeCurrentRow(Map<String, Object> currentRow, Map<String, Object> repeatedValues) throws JRException { row = sheet.createRow(sheet.getPhysicalNumberOfRows()); setRowHeight(row); for(int i = 0; i< columnNames.size(); i++) { String columnName = columnNames.get(i); CellSettings cellSettings = (CellSettings)currentRow.get(columnName) == null ? (repeatedValues.get(columnName) != null ? (CellSettings)repeatedValues.get(columnName) : null) : (CellSettings)currentRow.get(columnName); cell = row.createCell(i); if(cellSettings != null) { CellType type = cellSettings.getCellType(); cell.setCellType(type); Object cellValue = cellSettings.getCellValue(); if(cellValue != null) { if(cellValue instanceof RichTextString) { cell.setCellValue((RichTextString)cellSettings.getCellValue()); } else if (cellValue instanceof Number) { cell.setCellValue(((Number)cellSettings.getCellValue()).doubleValue()); } else if (cellValue instanceof Date) { cell.setCellValue((Date)cellSettings.getCellValue()); } else if(cellValue instanceof Boolean) { cell.setCellValue((Boolean)cellSettings.getCellValue()); }else if(cellValue instanceof ImageSettings){ ImageSettings imageSettings = (ImageSettings)cellValue; try { HSSFClientAnchor anchor = new HSSFClientAnchor( 0, 0, 0, 0, (short)(i), rowIndex, (short)(i + 1), rowIndex+1 ); anchor.setAnchorType(imageSettings.getAnchorType()); patriarch.createPicture(anchor, imageSettings.getIndex()); } catch (Exception ex) { throw new JRException( EXCEPTION_MESSAGE_KEY_CANNOT_ADD_CELL, null, ex); } catch (Error err) { throw new JRException( EXCEPTION_MESSAGE_KEY_CANNOT_ADD_CELL, null, err); } } } if(cellSettings.getCellStyle() != null) { cell.setCellStyle(cellSettings.getCellStyle()); } if(cellSettings.getFormula() != null) { // the formula text will be stored in formulaCellsMap in order to be applied only after // all defined names are created and available in the workbook (see #closeWorkbook()) formulaCellsMap.put(cell, cellSettings.getFormula()); } if(cellSettings.getLink() != null) { cell.setHyperlink(cellSettings.getLink()); } } } ++rowIndex; }
private void setImage(final HSSFWorkbook workbook, final HSSFSheet sheet, final CellLocation cellLocation, int width, int height) { POIUtils.setCellValue(sheet, cellLocation, ""); if (imageBuffer != null) { final HSSFPatriarch patriarch = sheet.createDrawingPatriarch(); final HSSFPicture picture = patriarch.createPicture(new HSSFClientAnchor(), pictureIndex); final Dimension dimension = picture.getImageDimension(); final float rate = (float) dimension.width / (float) dimension.height; final float specifiedRate = (float) width / (float) height; if (width == -1 || height == -1) { width = dimension.width; height = dimension.height; } else { if (rate > specifiedRate) { if (dimension.width > width) { height = (int) (width / rate); } else { width = dimension.width; height = dimension.height; } } else { if (dimension.height > height) { width = (int) (height * rate); } else { width = dimension.width; height = dimension.height; } } } final HSSFClientAnchor preferredSize = getPreferredSize(sheet, new HSSFClientAnchor(0, 0, 0, 0, (short) cellLocation.c, cellLocation.r, (short) 0, 0), width, height); picture.setAnchor(preferredSize); } }
private void setImage(HSSFWorkbook workbook, HSSFSheet sheet, CellLocation cellLocation, int width, int height) { POIUtils.setCellValue(sheet, cellLocation, ""); if (this.imageBuffer != null) { HSSFPatriarch patriarch = sheet.createDrawingPatriarch(); HSSFPicture picture = patriarch.createPicture( new HSSFClientAnchor(), this.pictureIndex); Dimension dimension = picture.getImageDimension(); float rate = (float) dimension.width / (float) dimension.height; float specifiedRate = (float) width / (float) height; if (width == -1 || height == -1) { width = dimension.width; height = dimension.height; } else { if (rate > specifiedRate) { if (dimension.width > width) { height = (int) (width / rate); } else { width = dimension.width; height = dimension.height; } } else { if (dimension.height > height) { width = (int) (height * rate); } else { width = dimension.width; height = dimension.height; } } } HSSFClientAnchor preferredSize = this.getPreferredSize(sheet, new HSSFClientAnchor(0, 0, 0, 0, (short) cellLocation.c, cellLocation.r, (short) 0, 0), width, height); picture.setAnchor(preferredSize); } }
public cfData execute( cfSession _session, List<cfData> parameters ) throws cfmRunTimeException { cfSpreadSheetData spreadsheet = (cfSpreadSheetData)parameters.get(2); String imgPath = parameters.get(1).getString(); String[] anchor = parameters.get(0).getString().split(","); // Check the anchor if ( anchor.length != 4 && anchor.length != 8 ) throwException(_session, "Invalid Anchor parameter. Must be 4 or 8 comma separated numbers" ); // Determine the file type String fileExt = imgPath.substring( imgPath.lastIndexOf(".")+1 ).toLowerCase(); int imgTypeIndex = 0; if ( fileExt.equals("dib") ){ imgTypeIndex = Workbook.PICTURE_TYPE_DIB; }else if ( fileExt.equals("jpg") ){ imgTypeIndex = Workbook.PICTURE_TYPE_JPEG; }else if ( fileExt.equals("emf") ){ imgTypeIndex = Workbook.PICTURE_TYPE_EMF; }else if ( fileExt.equals("pict") ){ imgTypeIndex = Workbook.PICTURE_TYPE_PICT; }else if ( fileExt.equals("png") ){ imgTypeIndex = Workbook.PICTURE_TYPE_PNG; }else if ( fileExt.equals("wmf") ){ imgTypeIndex = Workbook.PICTURE_TYPE_WMF; }else throwException(_session, "Unknown file type: " + imgPath ); // Read the file FileInputStream fs = null; byte[] fileBuffer = null; try{ fs = new FileInputStream( imgPath ); fileBuffer = org.apache.poi.util.IOUtils.toByteArray(fs); }catch(Exception fe){ throwException(_session, "Unable to read file: " + imgPath ); }finally{ try {fs.close();} catch (IOException e) {} } // Add the picture int imageIndex = spreadsheet.getWorkBook().addPicture(fileBuffer, imgTypeIndex); HSSFClientAnchor clientAnchor = new HSSFClientAnchor(); if ( anchor.length == 4 ){ clientAnchor.setRow1( Integer.valueOf( anchor[0] ) - 1 ); clientAnchor.setCol1( Integer.valueOf( anchor[1] ) - 1 ); clientAnchor.setRow2( Integer.valueOf( anchor[2] ) - 1 ); clientAnchor.setCol2( Integer.valueOf( anchor[3] ) - 1 ); }else{ clientAnchor.setDx1( Integer.valueOf( anchor[0] ) - 1 ); clientAnchor.setDy1( Integer.valueOf( anchor[1] ) - 1 ); clientAnchor.setDx2( Integer.valueOf( anchor[2] ) - 1 ); clientAnchor.setDy2( Integer.valueOf( anchor[3] ) - 1 ); clientAnchor.setRow1( Integer.valueOf( anchor[4] ) - 1 ); clientAnchor.setCol1( Integer.valueOf( anchor[5] ) - 1 ); clientAnchor.setRow2( Integer.valueOf( anchor[6] ) - 1 ); clientAnchor.setCol2( Integer.valueOf( anchor[7] ) - 1 ); } // finalise the image to the sheet spreadsheet.getActiveSheet().createDrawingPatriarch().createPicture(clientAnchor, imageIndex); return cfBooleanData.TRUE; }
public cfData execute( cfSession _session, List<cfData> parameters ) throws cfmRunTimeException { if ( parameters.get(2).getDataType() != cfData.CFSTRUCTDATA ) throwException(_session, "parameter must be of type structure"); cfSpreadSheetData spreadsheet = null; cfStructData commentS = null; int rowNo, columnNo; /* * Collect up the parameters */ spreadsheet = (cfSpreadSheetData)parameters.get(3); commentS = (cfStructData)parameters.get(2); rowNo = parameters.get(1).getInt() - 1; columnNo = parameters.get(0).getInt() - 1; if ( rowNo < 0 ) throwException(_session, "row must be 1 or greater (" + rowNo + ")"); if ( columnNo < 0 ) throwException(_session, "column must be 1 or greater (" + columnNo + ")"); /* * Perform the insertion */ Sheet sheet = spreadsheet.getActiveSheet(); Row row = sheet.getRow( rowNo ); if ( row == null ) row = sheet.createRow( rowNo ); Cell cell = row.getCell( columnNo ); if ( cell == null ) cell = row.createCell( columnNo ); // Create the anchor HSSFClientAnchor clientAnchor = new HSSFClientAnchor(); if ( commentS.containsKey("anchor") ){ String[] anchor = commentS.getData("anchor").getString().split(","); if ( anchor.length != 4 ) throwException(_session,"Invalid 'anchor' attribute, should be 4 numbers"); clientAnchor.setRow1( Integer.valueOf( anchor[0] ) - 1 ); clientAnchor.setCol1( Integer.valueOf( anchor[1] ) - 1 ); clientAnchor.setRow2( Integer.valueOf( anchor[2] ) - 1 ); clientAnchor.setCol2( Integer.valueOf( anchor[3] ) - 1 ); }else{ clientAnchor.setRow1( rowNo ); clientAnchor.setCol1( columnNo ); clientAnchor.setRow2( rowNo + 2 ); clientAnchor.setCol2( columnNo + 2 ); } // Create the comment Comment comment = spreadsheet.getActiveSheet().createDrawingPatriarch().createCellComment(clientAnchor); if ( commentS.containsKey("author") ){ comment.setAuthor( commentS.getData("author").getString() ); } if ( commentS.containsKey("visible") ){ comment.setVisible( commentS.getData("visible").getBoolean() ); } if ( commentS.containsKey("comment") ){ HSSFRichTextString richText = new HSSFRichTextString( commentS.getData("comment").getString() ); try { richText.applyFont( SpreadSheetFormatOptions.createCommentFont(spreadsheet.getWorkBook(), commentS) ); } catch (Exception e) { throwException( _session, e.getMessage() ); } comment.setString( richText ); } cell.setCellComment( comment ); return cfBooleanData.TRUE; }
@SuppressWarnings("unused") private String toString(HSSFClientAnchor anchor) { return "("+anchor.getDx1()+","+anchor.getDx2()+","+anchor.getDy1()+","+anchor.getDy2()+")"+ "("+anchor.getRow1()+","+anchor.getRow2()+","+anchor.getCol1()+","+anchor.getCol2()+")"; }
private void table(final HSSFSheet sheet) { if (sheet == null) { return; } if (sheet.getDrawingPatriarch() != null) { final List<HSSFShape> shapes = sheet.getDrawingPatriarch() .getChildren(); for (int i = 0; i < shapes.size(); ++i) { if (shapes.get(i) instanceof HSSFPicture) { try { // Gain access to private field anchor. final HSSFShape pic = shapes.get(i); final Field f = HSSFShape.class .getDeclaredField("anchor"); f.setAccessible(true); final HSSFClientAnchor anchor = (HSSFClientAnchor) f .get(pic); // Store picture cell row, column and picture data. if (!pix.containsKey(anchor.getRow1())) { pix.put(anchor.getRow1(), new HashMap<Short, List<HSSFPictureData>>()); } if (!pix.get(anchor.getRow1()).containsKey( anchor.getCol1())) { pix.get(anchor.getRow1()).put(anchor.getCol1(), new ArrayList<HSSFPictureData>()); } pix.get(anchor.getRow1()) .get(anchor.getCol1()) .add(book.getAllPictures().get( ((HSSFPicture) pic).getPictureIndex())); } catch (final Exception e) { throw new RuntimeException(e); } } } } out.append("<table cellspacing='0' style='border-spacing:0; border-collapse:collapse;'>\n"); for (rowIndex = 0; rowIndex < sheet.getPhysicalNumberOfRows(); ++rowIndex) { tr(sheet.getRow(rowIndex)); } out.append("</table>\n"); }
private void table(final HSSFSheet sheet, int startIndex, int endIndex) { if (sheet == null) { return; } if (sheet.getDrawingPatriarch() != null) { final List<HSSFShape> shapes = sheet.getDrawingPatriarch() .getChildren(); for (int i = 0; i < shapes.size(); ++i) { if (shapes.get(i) instanceof HSSFPicture) { try { // Gain access to private field anchor. final HSSFShape pic = shapes.get(i); final Field f = HSSFShape.class .getDeclaredField("anchor"); f.setAccessible(true); final HSSFClientAnchor anchor = (HSSFClientAnchor) f .get(pic); // Store picture cell row, column and picture data. if (!pix.containsKey(anchor.getRow1())) { pix.put(anchor.getRow1(), new HashMap<Short, List<HSSFPictureData>>()); } if (!pix.get(anchor.getRow1()).containsKey( anchor.getCol1())) { pix.get(anchor.getRow1()).put(anchor.getCol1(), new ArrayList<HSSFPictureData>()); } pix.get(anchor.getRow1()) .get(anchor.getCol1()) .add(book.getAllPictures().get( ((HSSFPicture) pic).getPictureIndex())); } catch (final Exception e) { throw new RuntimeException(e); } } } } out.append("<table id=\"" + "rvs_table" + "\" width=\"900\" class=\"mt\">\n"); tr(sheet.getRow(0)); StringBuffer buf = new StringBuffer(); tr(sheet.getRow(0), buf); String t = buf.toString(); resolvedValueSetList.add(t); for (int i=startIndex; i<=endIndex; i++) { tr(sheet.getRow(i)); buf = new StringBuffer(); tr(sheet.getRow(i), buf); t = buf.toString(); resolvedValueSetList.add(t); } out.append("</table>\n"); resolvedValueSetIterator = resolvedValueSetList.listIterator(); }
private void buildHeader(HSSFWorkbook workbook, HSSFSheet sheet, HSSFCellStyle style, HSSFCellStyle style2,HSSFPatriarch patriarch,String title,String[] headers){ // 设置这些样式 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); // 生成并设置另一个样式 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); // 定义注释的大小和位置,详见文档 HSSFComment comment = patriarch.createComment(new HSSFClientAnchor(0, 0, 0, 0, (short) 4, 2, (short) 6, 5)); // 设置注释内容 comment.setString(new HSSFRichTextString("可以在POI中添加注释!")); // 设置注释作者,当鼠标移动到单元格上是可以在状态栏中看到该内容. comment.setAuthor("leno"); // 产生表格标题行 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(headers[i]); } }