Source Code Cross Referenced for OfficeDrawing.java in  » Collaboration » poi-3.0.2-beta2 » org » apache » poi » hssf » usermodel » examples » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Collaboration » poi 3.0.2 beta2 » org.apache.poi.hssf.usermodel.examples 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /* ====================================================================
002:         Licensed to the Apache Software Foundation (ASF) under one or more
003:         contributor license agreements.  See the NOTICE file distributed with
004:         this work for additional information regarding copyright ownership.
005:         The ASF licenses this file to You under the Apache License, Version 2.0
006:         (the "License"); you may not use this file except in compliance with
007:         the License.  You may obtain a copy of the License at
008:
009:         http://www.apache.org/licenses/LICENSE-2.0
010:
011:         Unless required by applicable law or agreed to in writing, software
012:         distributed under the License is distributed on an "AS IS" BASIS,
013:         WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014:         See the License for the specific language governing permissions and
015:         limitations under the License.
016:         ==================================================================== */
017:
018:        package org.apache.poi.hssf.usermodel.examples;
019:
020:        import org.apache.poi.hssf.usermodel.*;
021:
022:        import java.io.*;
023:
024:        /**
025:         * Demonstrates how to use the office drawing capabilities of POI.
026:         *
027:         * @author Glen Stampoultzis (glens at apache.org)
028:         */
029:        public class OfficeDrawing {
030:            public static void main(String[] args) throws IOException {
031:                // Create the workbook and sheets.
032:                HSSFWorkbook wb = new HSSFWorkbook();
033:                HSSFSheet sheet1 = wb.createSheet("new sheet");
034:                HSSFSheet sheet2 = wb.createSheet("second sheet");
035:                HSSFSheet sheet3 = wb.createSheet("third sheet");
036:                HSSFSheet sheet4 = wb.createSheet("fourth sheet");
037:                HSSFSheet sheet5 = wb.createSheet("fifth sheet");
038:
039:                // Draw stuff in them
040:                drawSheet1(sheet1);
041:                drawSheet2(sheet2);
042:                drawSheet3(sheet3);
043:                drawSheet4(sheet4, wb);
044:                drawSheet5(sheet5, wb);
045:
046:                // Write the file out.
047:                FileOutputStream fileOut = new FileOutputStream("workbook.xls");
048:                wb.write(fileOut);
049:                fileOut.close();
050:            }
051:
052:            private static void drawSheet1(HSSFSheet sheet1) {
053:                // Create a row and size one of the cells reasonably large.
054:                HSSFRow row = sheet1.createRow(2);
055:                row.setHeight((short) 2800);
056:                row.createCell((short) 1);
057:                sheet1.setColumnWidth((short) 2, (short) 9000);
058:
059:                // Create the drawing patriarch.  This is the top level container for
060:                // all shapes.
061:                HSSFPatriarch patriarch = sheet1.createDrawingPatriarch();
062:
063:                // Draw some lines and an oval.
064:                drawLinesToCenter(patriarch);
065:                drawManyLines(patriarch);
066:                drawOval(patriarch);
067:                drawPolygon(patriarch);
068:
069:                // Draw a rectangle.
070:                HSSFSimpleShape rect = patriarch
071:                        .createSimpleShape(new HSSFClientAnchor(100, 100, 900,
072:                                200, (short) 0, 0, (short) 0, 0));
073:                rect.setShapeType(HSSFSimpleShape.OBJECT_TYPE_RECTANGLE);
074:            }
075:
076:            private static void drawSheet2(HSSFSheet sheet2) {
077:                // Create a row and size one of the cells reasonably large.
078:                HSSFRow row = sheet2.createRow(2);
079:                row.createCell((short) 1);
080:                row.setHeightInPoints(240);
081:                sheet2.setColumnWidth((short) 2, (short) 9000);
082:
083:                // Create the drawing patriarch.  This is the top level container for
084:                // all shapes. This will clear out any existing shapes for that sheet.
085:                HSSFPatriarch patriarch = sheet2.createDrawingPatriarch();
086:
087:                // Draw a grid in one of the cells.
088:                drawGrid(patriarch);
089:            }
090:
091:            private static void drawSheet3(HSSFSheet sheet3) {
092:                // Create a row and size one of the cells reasonably large
093:                HSSFRow row = sheet3.createRow(2);
094:                row.setHeightInPoints(140);
095:                row.createCell((short) 1);
096:                sheet3.setColumnWidth((short) 2, (short) 9000);
097:
098:                // Create the drawing patriarch.  This is the top level container for
099:                // all shapes. This will clear out any existing shapes for that sheet.
100:                HSSFPatriarch patriarch = sheet3.createDrawingPatriarch();
101:
102:                // Create a shape group.
103:                HSSFShapeGroup group = patriarch
104:                        .createGroup(new HSSFClientAnchor(0, 0, 900, 200,
105:                                (short) 2, 2, (short) 2, 2));
106:
107:                // Create a couple of lines in the group.
108:                HSSFSimpleShape shape1 = group.createShape(new HSSFChildAnchor(
109:                        3, 3, 500, 500));
110:                shape1.setShapeType(HSSFSimpleShape.OBJECT_TYPE_LINE);
111:                ((HSSFChildAnchor) shape1.getAnchor()).setAnchor((short) 3, 3,
112:                        500, 500);
113:                HSSFSimpleShape shape2 = group.createShape(new HSSFChildAnchor(
114:                        (short) 1, 200, 400, 600));
115:                shape2.setShapeType(HSSFSimpleShape.OBJECT_TYPE_LINE);
116:
117:            }
118:
119:            private static void drawSheet4(HSSFSheet sheet4, HSSFWorkbook wb) {
120:                // Create the drawing patriarch.  This is the top level container for
121:                // all shapes. This will clear out any existing shapes for that sheet.
122:                HSSFPatriarch patriarch = sheet4.createDrawingPatriarch();
123:
124:                // Create a couple of textboxes
125:                HSSFTextbox textbox1 = patriarch
126:                        .createTextbox(new HSSFClientAnchor(0, 0, 0, 0,
127:                                (short) 1, 1, (short) 2, 2));
128:                textbox1.setString(new HSSFRichTextString("This is a test"));
129:                HSSFTextbox textbox2 = patriarch
130:                        .createTextbox(new HSSFClientAnchor(0, 0, 900, 100,
131:                                (short) 3, 3, (short) 3, 4));
132:                textbox2.setString(new HSSFRichTextString("Woo"));
133:                textbox2.setFillColor(200, 0, 0);
134:                textbox2.setLineStyle(HSSFSimpleShape.LINESTYLE_DOTGEL);
135:
136:                // Create third one with some fancy font styling.
137:                HSSFTextbox textbox3 = patriarch
138:                        .createTextbox(new HSSFClientAnchor(0, 0, 900, 100,
139:                                (short) 4, 4, (short) 5, 4 + 1));
140:                HSSFFont font = wb.createFont();
141:                font.setItalic(true);
142:                font.setUnderline(HSSFFont.U_DOUBLE);
143:                HSSFRichTextString string = new HSSFRichTextString("Woo!!!");
144:                string.applyFont(2, 5, font);
145:                textbox3.setString(string);
146:                textbox3.setFillColor(0x08000030);
147:                textbox3.setLineStyle(HSSFSimpleShape.LINESTYLE_NONE); // no line around the textbox.
148:                textbox3.setNoFill(true); // make it transparent
149:            }
150:
151:            private static void drawSheet5(HSSFSheet sheet5, HSSFWorkbook wb)
152:                    throws IOException {
153:
154:                // Create the drawing patriarch.  This is the top level container for
155:                // all shapes. This will clear out any existing shapes for that sheet.
156:                HSSFPatriarch patriarch = sheet5.createDrawingPatriarch();
157:
158:                HSSFClientAnchor anchor;
159:                anchor = new HSSFClientAnchor(0, 0, 0, 255, (short) 2, 2,
160:                        (short) 4, 7);
161:                anchor.setAnchorType(2);
162:                patriarch.createPicture(anchor, loadPicture(
163:                        "src/resources/logos/logoKarmokar4.png", wb));
164:
165:                anchor = new HSSFClientAnchor(0, 0, 0, 255, (short) 4, 2,
166:                        (short) 5, 7);
167:                anchor.setAnchorType(2);
168:                patriarch.createPicture(anchor, loadPicture(
169:                        "src/resources/logos/logoKarmokar4edited.png", wb));
170:
171:                anchor = new HSSFClientAnchor(0, 0, 1023, 255, (short) 6, 2,
172:                        (short) 8, 7);
173:                anchor.setAnchorType(2);
174:                HSSFPicture picture = patriarch.createPicture(anchor,
175:                        loadPicture("src/resources/logos/logoKarmokar4s.png",
176:                                wb));
177:                //Reset the image to the original size.
178:                picture.resize();
179:                picture.setLineStyle(picture.LINESTYLE_DASHDOTGEL);
180:
181:            }
182:
183:            private static int loadPicture(String path, HSSFWorkbook wb)
184:                    throws IOException {
185:                int pictureIndex;
186:                FileInputStream fis = null;
187:                ByteArrayOutputStream bos = null;
188:                try {
189:                    fis = new FileInputStream(path);
190:                    bos = new ByteArrayOutputStream();
191:                    int c;
192:                    while ((c = fis.read()) != -1)
193:                        bos.write(c);
194:                    pictureIndex = wb.addPicture(bos.toByteArray(),
195:                            HSSFWorkbook.PICTURE_TYPE_PNG);
196:                } finally {
197:                    if (fis != null)
198:                        fis.close();
199:                    if (bos != null)
200:                        bos.close();
201:                }
202:                return pictureIndex;
203:            }
204:
205:            private static void drawOval(HSSFPatriarch patriarch) {
206:                // Create an oval and style to taste.
207:                HSSFClientAnchor a = new HSSFClientAnchor();
208:                a.setAnchor((short) 2, 2, 20, 20, (short) 2, 2, 190, 80);
209:                HSSFSimpleShape s = patriarch.createSimpleShape(a);
210:                s.setShapeType(HSSFSimpleShape.OBJECT_TYPE_OVAL);
211:                s.setLineStyleColor(10, 10, 10);
212:                s.setFillColor(90, 10, 200);
213:                s.setLineWidth(HSSFShape.LINEWIDTH_ONE_PT * 3);
214:                s.setLineStyle(HSSFShape.LINESTYLE_DOTSYS);
215:            }
216:
217:            private static void drawPolygon(HSSFPatriarch patriarch) {
218:                //        HSSFClientAnchor a = new HSSFClientAnchor( 0, 0, 1023, 255, (short) 2, 2, (short) 3, 3 );
219:                //        HSSFPolygon p = patriarch.createPolygon(a);
220:                //        p.setPolygonDrawArea(100,100);
221:                //        p.setPoints( new int[]{30, 90, 50}, new int[]{88, 5, 44} );
222:
223:                HSSFClientAnchor a = new HSSFClientAnchor();
224:                a.setAnchor((short) 2, 2, 0, 0, (short) 3, 3, 1023, 255);
225:                HSSFShapeGroup g = patriarch.createGroup(a);
226:                g.setCoordinates(0, 0, 200, 200);
227:                HSSFPolygon p1 = g.createPolygon(new HSSFChildAnchor(0, 0, 200,
228:                        200));
229:                p1.setPolygonDrawArea(100, 100);
230:                p1.setPoints(new int[] { 0, 90, 50 }, new int[] { 5, 5, 44 });
231:                p1.setFillColor(0, 255, 0);
232:                HSSFPolygon p2 = g.createPolygon(new HSSFChildAnchor(20, 20,
233:                        200, 200));
234:                p2.setPolygonDrawArea(200, 200);
235:                p2.setPoints(new int[] { 120, 20, 150 }, new int[] { 105, 30,
236:                        195 });
237:                p2.setFillColor(255, 0, 0);
238:            }
239:
240:            private static void drawManyLines(HSSFPatriarch patriarch) {
241:                // Draw bunch of lines
242:                int x1 = 100;
243:                int y1 = 100;
244:                int x2 = 800;
245:                int y2 = 200;
246:                int color = 0;
247:                for (int i = 0; i < 10; i++) {
248:                    HSSFClientAnchor a2 = new HSSFClientAnchor();
249:                    a2.setAnchor((short) 2, 2, x1, y1, (short) 2, 2, x2, y2);
250:                    HSSFSimpleShape shape2 = patriarch.createSimpleShape(a2);
251:                    shape2.setShapeType(HSSFSimpleShape.OBJECT_TYPE_LINE);
252:                    shape2.setLineStyleColor(color);
253:                    y1 -= 10;
254:                    y2 -= 10;
255:                    color += 30;
256:                }
257:            }
258:
259:            private static void drawGrid(HSSFPatriarch patriarch) {
260:                // This draws a grid of lines.  Since the coordinates space fixed at
261:                // 1024 by 256 we use a ratio to get a reasonably square grids.
262:
263:                double xRatio = 3.22;
264:                double yRatio = 0.6711;
265:
266:                int x1 = 000;
267:                int y1 = 000;
268:                int x2 = 000;
269:                int y2 = 200;
270:                for (int i = 0; i < 20; i++) {
271:                    HSSFClientAnchor a2 = new HSSFClientAnchor();
272:                    a2.setAnchor((short) 2, 2, (int) (x1 * xRatio),
273:                            (int) (y1 * yRatio), (short) 2, 2,
274:                            (int) (x2 * xRatio), (int) (y2 * yRatio));
275:                    HSSFSimpleShape shape2 = patriarch.createSimpleShape(a2);
276:                    shape2.setShapeType(HSSFSimpleShape.OBJECT_TYPE_LINE);
277:
278:                    x1 += 10;
279:                    x2 += 10;
280:                }
281:
282:                x1 = 000;
283:                y1 = 000;
284:                x2 = 200;
285:                y2 = 000;
286:                for (int i = 0; i < 20; i++) {
287:                    HSSFClientAnchor a2 = new HSSFClientAnchor();
288:                    a2.setAnchor((short) 2, 2, (int) (x1 * xRatio),
289:                            (int) (y1 * yRatio), (short) 2, 2,
290:                            (int) (x2 * xRatio), (int) (y2 * yRatio));
291:                    HSSFSimpleShape shape2 = patriarch.createSimpleShape(a2);
292:                    shape2.setShapeType(HSSFSimpleShape.OBJECT_TYPE_LINE);
293:
294:                    y1 += 10;
295:                    y2 += 10;
296:                }
297:            }
298:
299:            private static void drawLinesToCenter(HSSFPatriarch patriarch) {
300:                // Draw some lines from and to the corners
301:                {
302:                    HSSFClientAnchor a1 = new HSSFClientAnchor();
303:                    a1.setAnchor((short) 2, 2, 0, 0, (short) 2, 2, 512, 128);
304:                    HSSFSimpleShape shape1 = patriarch.createSimpleShape(a1);
305:                    shape1.setShapeType(HSSFSimpleShape.OBJECT_TYPE_LINE);
306:                }
307:                {
308:                    HSSFClientAnchor a1 = new HSSFClientAnchor();
309:                    a1.setAnchor((short) 2, 2, 512, 128, (short) 2, 2, 1024, 0);
310:                    HSSFSimpleShape shape1 = patriarch.createSimpleShape(a1);
311:                    shape1.setShapeType(HSSFSimpleShape.OBJECT_TYPE_LINE);
312:                }
313:                {
314:                    HSSFClientAnchor a1 = new HSSFClientAnchor();
315:                    a1.setAnchor((short) 1, 1, 0, 0, (short) 1, 1, 512, 100);
316:                    HSSFSimpleShape shape1 = patriarch.createSimpleShape(a1);
317:                    shape1.setShapeType(HSSFSimpleShape.OBJECT_TYPE_LINE);
318:                }
319:                {
320:                    HSSFClientAnchor a1 = new HSSFClientAnchor();
321:                    a1.setAnchor((short) 1, 1, 512, 100, (short) 1, 1, 1024, 0);
322:                    HSSFSimpleShape shape1 = patriarch.createSimpleShape(a1);
323:                    shape1.setShapeType(HSSFSimpleShape.OBJECT_TYPE_LINE);
324:                }
325:
326:            }
327:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.