1 2 17 18 package org.apache.poi.hssf.usermodel.examples; 19 20 import org.apache.poi.hssf.usermodel.*; 21 22 import java.io.IOException ; 23 import java.io.FileOutputStream ; 24 import java.io.FileNotFoundException ; 25 26 31 public class OfficeDrawing 32 { 33 public static void main(String [] args) 34 throws IOException 35 { 36 HSSFWorkbook wb = new HSSFWorkbook(); 38 HSSFSheet sheet1 = wb.createSheet("new sheet"); 39 HSSFSheet sheet2 = wb.createSheet("second sheet"); 40 HSSFSheet sheet3 = wb.createSheet("third sheet"); 41 HSSFSheet sheet4 = wb.createSheet("fourth sheet"); 42 43 drawSheet1( sheet1 ); 45 drawSheet2( sheet2 ); 46 drawSheet3( sheet3 ); 47 drawSheet4( sheet4, wb ); 48 49 FileOutputStream fileOut = new FileOutputStream ("workbook.xls"); 51 wb.write(fileOut); 52 fileOut.close(); 53 } 54 55 private static void drawSheet1( HSSFSheet sheet1 ) 56 { 57 HSSFRow row = sheet1.createRow(2); 59 row.setHeight((short) 2800); 60 sheet1.setColumnWidth((short) 2, (short) 9000); 61 62 HSSFPatriarch patriarch = sheet1.createDrawingPatriarch(); 65 66 drawLinesToCenter( patriarch ); 68 drawManyLines( patriarch ); 69 drawOval( patriarch ); 70 drawPolygon( patriarch ); 71 72 HSSFSimpleShape rect = patriarch.createSimpleShape( new HSSFClientAnchor(100, 100, 900, 200, (short)0, 0, (short)0, 0) ); 74 rect.setShapeType(HSSFSimpleShape.OBJECT_TYPE_RECTANGLE); 75 } 76 77 private static void drawSheet2( HSSFSheet sheet2 ) 78 { 79 HSSFRow row = sheet2.createRow(2); 81 row.setHeightInPoints(240); 82 sheet2.setColumnWidth((short) 2, (short) 9000); 83 84 HSSFPatriarch patriarch = sheet2.createDrawingPatriarch(); 87 88 drawGrid( patriarch ); 90 } 91 92 private static void drawSheet3( HSSFSheet sheet3 ) 93 { 94 HSSFRow row = sheet3.createRow(2); 96 row.setHeightInPoints(140); 97 sheet3.setColumnWidth((short) 2, (short) 9000); 98 99 HSSFPatriarch patriarch = sheet3.createDrawingPatriarch(); 102 103 HSSFShapeGroup group = patriarch.createGroup( 105 new HSSFClientAnchor(0,0,900,200,(short)2,2,(short)2,2)); 106 107 HSSFSimpleShape shape1 = group.createShape(new HSSFChildAnchor(3,3,500,500)); 109 shape1.setShapeType(HSSFSimpleShape.OBJECT_TYPE_LINE); 110 ( (HSSFChildAnchor) shape1.getAnchor() ).setAnchor((short)3,3,500,500); 111 HSSFSimpleShape shape2 = group.createShape(new HSSFChildAnchor((short)1,200,400,600)); 112 shape2.setShapeType(HSSFSimpleShape.OBJECT_TYPE_LINE); 113 114 } 115 116 private static void drawSheet4( HSSFSheet sheet4, HSSFWorkbook wb ) 117 { 118 HSSFPatriarch patriarch = sheet4.createDrawingPatriarch(); 121 122 HSSFTextbox textbox1 = patriarch.createTextbox( 124 new HSSFClientAnchor(0,0,0,0,(short)1,1,(short)2,2)); 125 textbox1.setString(new HSSFRichTextString("This is a test") ); 126 HSSFTextbox textbox2 = patriarch.createTextbox( 127 new HSSFClientAnchor(0,0,900,100,(short)3,3,(short)3,4)); 128 textbox2.setString(new HSSFRichTextString("Woo") ); 129 textbox2.setFillColor(200,0,0); 130 textbox2.setLineStyle(HSSFSimpleShape.LINESTYLE_DOTGEL); 131 132 HSSFTextbox textbox3 = patriarch.createTextbox( 134 new HSSFClientAnchor(0,0,900,100,(short)4,4,(short)5,4+1)); 135 HSSFFont font = wb.createFont(); 136 font.setItalic(true); 137 font.setUnderline(HSSFFont.U_DOUBLE); 138 HSSFRichTextString string = new HSSFRichTextString("Woo!!!"); 139 string.applyFont(2,5,font); 140 textbox3.setString(string ); 141 textbox3.setFillColor(0x08000030); 142 textbox3.setLineStyle(HSSFSimpleShape.LINESTYLE_NONE); textbox3.setNoFill(true); } 145 146 private static void drawOval( HSSFPatriarch patriarch ) 147 { 148 HSSFClientAnchor a = new HSSFClientAnchor(); 150 a.setAnchor((short)2, 2, 20, 20, (short) 2, 2, 190, 80); 151 HSSFSimpleShape s = patriarch.createSimpleShape(a); 152 s.setShapeType(HSSFSimpleShape.OBJECT_TYPE_OVAL); 153 s.setLineStyleColor(10,10,10); 154 s.setFillColor(90,10,200); 155 s.setLineWidth(HSSFShape.LINEWIDTH_ONE_PT * 3); 156 s.setLineStyle(HSSFShape.LINESTYLE_DOTSYS); 157 } 158 159 private static void drawPolygon( HSSFPatriarch patriarch ) 160 { 161 166 167 HSSFClientAnchor a = new HSSFClientAnchor(); 168 a.setAnchor( (short) 2, 2, 0, 0, (short) 3, 3, 1023, 255 ); 169 HSSFShapeGroup g = patriarch.createGroup( a ); 170 g.setCoordinates(0,0,200,200); 171 HSSFPolygon p1 = g.createPolygon( new HSSFChildAnchor( 0, 0, 200, 200 ) ); 172 p1.setPolygonDrawArea( 100, 100 ); 173 p1.setPoints( new int[]{0, 90, 50}, new int[]{5, 5, 44} ); 174 p1.setFillColor( 0, 255, 0 ); 175 HSSFPolygon p2 = g.createPolygon( new HSSFChildAnchor( 20, 20, 200, 200 ) ); 176 p2.setPolygonDrawArea( 200, 200 ); 177 p2.setPoints( new int[]{120, 20, 150}, new int[]{105, 30, 195} ); 178 p2.setFillColor( 255, 0, 0 ); 179 } 180 181 private static void drawManyLines( HSSFPatriarch patriarch ) 182 { 183 int x1 = 100; 185 int y1 = 100; 186 int x2 = 800; 187 int y2 = 200; 188 int color = 0; 189 for (int i = 0; i < 10; i++) 190 { 191 HSSFClientAnchor a2 = new HSSFClientAnchor(); 192 a2.setAnchor((short) 2, 2, x1, y1, (short) 2, 2, x2, y2); 193 HSSFSimpleShape shape2 = patriarch.createSimpleShape(a2); 194 shape2.setShapeType(HSSFSimpleShape.OBJECT_TYPE_LINE); 195 shape2.setLineStyleColor(color); 196 y1 -= 10; 197 y2 -= 10; 198 color += 30; 199 } 200 } 201 202 private static void drawGrid( HSSFPatriarch patriarch ) 203 { 204 207 double xRatio = 3.22; 208 double yRatio = 0.6711; 209 210 int x1 = 000; 211 int y1 = 000; 212 int x2 = 000; 213 int y2 = 200; 214 for (int i = 0; i < 20; i++) 215 { 216 HSSFClientAnchor a2 = new HSSFClientAnchor(); 217 a2.setAnchor((short) 2, 2, (int) ( x1 * xRatio ), (int) ( y1 * yRatio ), 218 (short) 2, 2, (int) ( x2 * xRatio ), (int) ( y2 * yRatio )); 219 HSSFSimpleShape shape2 = patriarch.createSimpleShape(a2); 220 shape2.setShapeType(HSSFSimpleShape.OBJECT_TYPE_LINE); 221 222 x1 += 10; 223 x2 += 10; 224 } 225 226 x1 = 000; 227 y1 = 000; 228 x2 = 200; 229 y2 = 000; 230 for (int i = 0; i < 20; i++) 231 { 232 HSSFClientAnchor a2 = new HSSFClientAnchor(); 233 a2.setAnchor((short) 2, 2, (int) ( x1 * xRatio ), (int) ( y1 * yRatio ), 234 (short) 2, 2, (int) ( x2 * xRatio ), (int) ( y2 * yRatio )); 235 HSSFSimpleShape shape2 = patriarch.createSimpleShape(a2); 236 shape2.setShapeType(HSSFSimpleShape.OBJECT_TYPE_LINE); 237 238 y1 += 10; 239 y2 += 10; 240 } 241 } 242 243 private static void drawLinesToCenter( HSSFPatriarch patriarch ) 244 { 245 { 247 HSSFClientAnchor a1 = new HSSFClientAnchor(); 248 a1.setAnchor( (short)2, 2, 0, 0, (short) 2, 2, 512, 128); 249 HSSFSimpleShape shape1 = patriarch.createSimpleShape(a1); 250 shape1.setShapeType(HSSFSimpleShape.OBJECT_TYPE_LINE); 251 } 252 { 253 HSSFClientAnchor a1 = new HSSFClientAnchor(); 254 a1.setAnchor( (short)2, 2, 512, 128, (short) 2, 2, 1024, 0); 255 HSSFSimpleShape shape1 = patriarch.createSimpleShape(a1); 256 shape1.setShapeType(HSSFSimpleShape.OBJECT_TYPE_LINE); 257 } 258 { 259 HSSFClientAnchor a1 = new HSSFClientAnchor(); 260 a1.setAnchor( (short)1, 1, 0, 0, (short) 1, 1, 512, 100); 261 HSSFSimpleShape shape1 = patriarch.createSimpleShape(a1); 262 shape1.setShapeType(HSSFSimpleShape.OBJECT_TYPE_LINE); 263 } 264 { 265 HSSFClientAnchor a1 = new HSSFClientAnchor(); 266 a1.setAnchor( (short)1, 1, 512, 100, (short) 1, 1, 1024, 0); 267 HSSFSimpleShape shape1 = patriarch.createSimpleShape(a1); 268 shape1.setShapeType(HSSFSimpleShape.OBJECT_TYPE_LINE); 269 } 270 271 } 272 } 273 | Popular Tags |