private static void drawLinesToCenter( HSSFPatriarch patriarch ) { // Draw some lines from and to the corners { HSSFClientAnchor a1 = new HSSFClientAnchor(); a1.setAnchor( (short)2, 2, 0, 0, (short) 2, 2, 512, 128); HSSFSimpleShape shape1 = patriarch.createSimpleShape(a1); shape1.setShapeType(HSSFSimpleShape.OBJECT_TYPE_LINE); } { HSSFClientAnchor a1 = new HSSFClientAnchor(); a1.setAnchor( (short)2, 2, 512, 128, (short) 2, 2, 1024, 0); HSSFSimpleShape shape1 = patriarch.createSimpleShape(a1); shape1.setShapeType(HSSFSimpleShape.OBJECT_TYPE_LINE); } { HSSFClientAnchor a1 = new HSSFClientAnchor(); a1.setAnchor( (short)1, 1, 0, 0, (short) 1, 1, 512, 100); HSSFSimpleShape shape1 = patriarch.createSimpleShape(a1); shape1.setShapeType(HSSFSimpleShape.OBJECT_TYPE_LINE); } { HSSFClientAnchor a1 = new HSSFClientAnchor(); a1.setAnchor( (short)1, 1, 512, 100, (short) 1, 1, 1024, 0); HSSFSimpleShape shape1 = patriarch.createSimpleShape(a1); shape1.setShapeType(HSSFSimpleShape.OBJECT_TYPE_LINE); } } }
a2.setAnchor((short) 2, 2, (int) ( x1 * xRatio ), (int) ( y1 * yRatio ), (short) 2, 2, (int) ( x2 * xRatio ), (int) ( y2 * yRatio )); HSSFSimpleShape shape2 = patriarch.createSimpleShape(a2); shape2.setShapeType(HSSFSimpleShape.OBJECT_TYPE_LINE); a2.setAnchor((short) 2, 2, (int) ( x1 * xRatio ), (int) ( y1 * yRatio ), (short) 2, 2, (int) ( x2 * xRatio ), (int) ( y2 * yRatio )); HSSFSimpleShape shape2 = patriarch.createSimpleShape(a2); shape2.setShapeType(HSSFSimpleShape.OBJECT_TYPE_LINE);
Workbook wb = new HSSFWorkbook(); Sheet sheet = wb.createSheet(); HSSFPatriarch patriarch = (HSSFPatriarch) sheet.createDrawingPatriarch(); /* Here is the thing: the line will go from top left in cell (0,0) to down left of cell (0,1) */ HSSFClientAnchor anchor = new HSSFClientAnchor( 0, 0, 0, 255, (short) 0, 0,(short) 1, 0); HSSFSimpleShape shape = patriarch.createSimpleShape(anchor); shape.setShapeType(HSSFSimpleShape.OBJECT_TYPE_LINE); shape.setLineStyleColor(10, 10, 10); shape.setFillColor(90, 10, 200); shape.setLineWidth(HSSFShape.LINEWIDTH_ONE_PT); shape.setLineStyle(HSSFShape.LINESTYLE_SOLID); // you don't even need the cell, but if you also want to write anything... Row row = sheet.createRow(0); Cell cell = row.createCell(0); cell.setCellValue("Test");
private static void drawManyLines( HSSFPatriarch patriarch ) { // Draw bunch of lines int x1 = 100; int y1 = 100; int x2 = 800; int y2 = 200; int color = 0; for (int i = 0; i < 10; i++) { HSSFClientAnchor a2 = new HSSFClientAnchor(); a2.setAnchor((short) 2, 2, x1, y1, (short) 2, 2, x2, y2); HSSFSimpleShape shape2 = patriarch.createSimpleShape(a2); shape2.setShapeType(HSSFSimpleShape.OBJECT_TYPE_LINE); shape2.setLineStyleColor(color); y1 -= 10; y2 -= 10; color += 30; } }
private static void drawOval( HSSFPatriarch patriarch ) { // Create an oval and style to taste. HSSFClientAnchor a = new HSSFClientAnchor(); a.setAnchor((short)2, 2, 20, 20, (short) 2, 2, 190, 80); HSSFSimpleShape s = patriarch.createSimpleShape(a); s.setShapeType(HSSFSimpleShape.OBJECT_TYPE_OVAL); s.setLineStyleColor(10,10,10); s.setFillColor(90,10,200); s.setLineWidth(HSSFShape.LINEWIDTH_ONE_PT * 3); s.setLineStyle(HSSFShape.LINESTYLE_DOTSYS); }
private static void drawSheet1( HSSFSheet sheet1 ) { // Create a row and size one of the cells reasonably large. HSSFRow row = sheet1.createRow(2); row.setHeight((short) 2800); row.createCell(1); sheet1.setColumnWidth(2, 9000); // Create the drawing patriarch. This is the top level container for // all shapes. HSSFPatriarch patriarch = sheet1.createDrawingPatriarch(); // Draw some lines and an oval. drawLinesToCenter( patriarch ); drawManyLines( patriarch ); drawOval( patriarch ); drawPolygon( patriarch ); // Draw a rectangle. HSSFSimpleShape rect = patriarch.createSimpleShape( new HSSFClientAnchor(100, 100, 900, 200, (short)0, 0, (short)0, 0) ); rect.setShapeType(HSSFSimpleShape.OBJECT_TYPE_RECTANGLE); }