]> source.dussan.org Git - poi.git/commitdiff
example demonstrating how to generate large workbooks and avoid OutOfMemory exception
authorYegor Kozlov <yegor@apache.org>
Sun, 7 Dec 2008 14:10:23 +0000 (14:10 +0000)
committerYegor Kozlov <yegor@apache.org>
Sun, 7 Dec 2008 14:10:23 +0000 (14:10 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@724135 13f79535-47bb-0310-9956-ffa450edef68

src/examples/src/org/apache/poi/xssf/usermodel/examples/BigGridDemo.java [new file with mode: 0755]

diff --git a/src/examples/src/org/apache/poi/xssf/usermodel/examples/BigGridDemo.java b/src/examples/src/org/apache/poi/xssf/usermodel/examples/BigGridDemo.java
new file mode 100755 (executable)
index 0000000..6495ddb
--- /dev/null
@@ -0,0 +1,252 @@
+/* ====================================================================\r
+   Licensed to the Apache Software Foundation (ASF) under one or more\r
+   contributor license agreements.  See the NOTICE file distributed with\r
+   this work for additional information regarding copyright ownership.\r
+   The ASF licenses this file to You under the Apache License, Version 2.0\r
+   (the "License"); you may not use this file except in compliance with\r
+   the License.  You may obtain a copy of the License at\r
+\r
+       http://www.apache.org/licenses/LICENSE-2.0\r
+\r
+   Unless required by applicable law or agreed to in writing, software\r
+   distributed under the License is distributed on an "AS IS" BASIS,\r
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+   See the License for the specific language governing permissions and\r
+   limitations under the License.\r
+==================================================================== */\r
+package org.apache.poi.xssf.usermodel.examples;\r
+\r
+import org.apache.poi.xssf.usermodel.*;\r
+import org.apache.poi.ss.util.CellReference;\r
+import org.apache.poi.ss.usermodel.IndexedColors;\r
+import org.apache.poi.ss.usermodel.DateUtil;\r
+\r
+import java.io.*;\r
+import java.util.zip.ZipFile;\r
+import java.util.zip.ZipEntry;\r
+import java.util.zip.ZipOutputStream;\r
+import java.util.*;\r
+\r
+/**\r
+ * Demonstrates a workaround you can use to generate large workbooks and avoid OutOfMemory exception.\r
+ *\r
+ * The trick is as follows:\r
+ * 1. create a template workbook, create sheets and global objects such as cell styles, number formats, etc.\r
+ * 2. create an application that streams data in a text file\r
+ * 3. Substitute the sheet in the template with the generated data\r
+ *\r
+ * @author Yegor Kozlov\r
+ */\r
+public class BigGridDemo {\r
+    public static void main(String[] args) throws Exception {\r
+\r
+        // Step 1. Create a template file. Setup sheets and workbook-level objects such as\r
+        // cell styles, number formats, etc.\r
+\r
+        XSSFWorkbook wb = new XSSFWorkbook();\r
+        XSSFSheet sheet = wb.createSheet("Big Grid");\r
+\r
+        Map<String, XSSFCellStyle> styles = createStyles(wb);\r
+        //name of the zip entry holding sheet data, e.g. /xl/worksheets/sheet1.xml\r
+        String sheetRef = sheet.getPackagePart().getPartName().getName();\r
+\r
+        //save the template\r
+        FileOutputStream os = new FileOutputStream("template.xlsx");\r
+        wb.write(os);\r
+        os.close();\r
+\r
+        //Step 2. Generate XML file.\r
+        File tmp = File.createTempFile("sheet", ".xml");\r
+        Writer fw = new FileWriter(tmp);\r
+        generate(fw, styles);\r
+        fw.close();\r
+\r
+        //Step 3. Substitute the template entry with the generated data\r
+        FileOutputStream out = new FileOutputStream("big-grid.xlsx");\r
+        substitute(new File("template.xlsx"), tmp, sheetRef.substring(1), out);\r
+        out.close();\r
+    }\r
+\r
+    /**\r
+     * Create a library of cell styles.\r
+     */\r
+    private static Map<String, XSSFCellStyle> createStyles(XSSFWorkbook wb){\r
+        Map<String, XSSFCellStyle> styles = new HashMap<String, XSSFCellStyle>();\r
+        XSSFDataFormat fmt = wb.createDataFormat();\r
+\r
+        XSSFCellStyle style1 = wb.createCellStyle();\r
+        style1.setAlignment(XSSFCellStyle.ALIGN_RIGHT);\r
+        style1.setDataFormat(fmt.getFormat("0.0%"));\r
+        styles.put("percent", style1);\r
+\r
+        XSSFCellStyle style2 = wb.createCellStyle();\r
+        style2.setAlignment(XSSFCellStyle.ALIGN_CENTER);\r
+        style2.setDataFormat(fmt.getFormat("0.0X"));\r
+        styles.put("coeff", style2);\r
+\r
+        XSSFCellStyle style3 = wb.createCellStyle();\r
+        style3.setAlignment(XSSFCellStyle.ALIGN_RIGHT);\r
+        style3.setDataFormat(fmt.getFormat("$#,##0.00"));\r
+        styles.put("currency", style3);\r
+\r
+        XSSFCellStyle style4 = wb.createCellStyle();\r
+        style4.setAlignment(XSSFCellStyle.ALIGN_RIGHT);\r
+        style4.setDataFormat(fmt.getFormat("mmm dd"));\r
+        styles.put("date", style4);\r
+\r
+        XSSFCellStyle style5 = wb.createCellStyle();\r
+        XSSFFont headerFont = wb.createFont();\r
+        headerFont.setBold(true);\r
+        style5.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());\r
+        style5.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND);\r
+        style5.setFont(headerFont);\r
+        styles.put("header", style5);\r
+\r
+        return styles;\r
+    }\r
+\r
+    private static void generate(Writer out, Map<String, XSSFCellStyle> styles) throws Exception {\r
+\r
+        Random rnd = new Random();\r
+        Calendar calendar = Calendar.getInstance();\r
+\r
+        SpreadsheetWriter sw = new SpreadsheetWriter(out);\r
+        sw.beginSheet();\r
+\r
+        //insert header row\r
+        sw.insertRow(0);\r
+        int styleIndex = styles.get("header").getIndex();\r
+        sw.createCell(0, "Title", styleIndex);\r
+        sw.createCell(1, "% Change", styleIndex);\r
+        sw.createCell(2, "Ratio", styleIndex);\r
+        sw.createCell(3, "Expenses", styleIndex);\r
+        sw.createCell(4, "Date", styleIndex);\r
+\r
+        sw.endRow();\r
+\r
+        //write data rows\r
+        for (int rownum = 1; rownum < 100000; rownum++) {\r
+            sw.insertRow(rownum);\r
+\r
+            sw.createCell(0, "Hello, " + rownum + "!");\r
+            sw.createCell(1, (double)rnd.nextInt(100)/100, styles.get("percent").getIndex());\r
+            sw.createCell(2, (double)rnd.nextInt(10)/10, styles.get("coeff").getIndex());\r
+            sw.createCell(3, rnd.nextInt(10000), styles.get("currency").getIndex());\r
+            sw.createCell(4, calendar, styles.get("date").getIndex());\r
+\r
+            sw.endRow();\r
+\r
+            calendar.roll(Calendar.DAY_OF_YEAR, 1);\r
+        }\r
+        sw.endSheet();\r
+    }\r
+\r
+    /**\r
+     *\r
+     * @param zipfile the template file\r
+     * @param tmpfile the XML file with the sheet data\r
+     * @param entry the name of the sheet entry to substitute, e.g. xl/worksheets/sheet1.xml\r
+     * @param out the stream to write the result to\r
+     */\r
+    private static void substitute(File zipfile, File tmpfile, String entry, OutputStream out) throws IOException {\r
+        ZipFile zip = new ZipFile(zipfile);\r
+\r
+        ZipOutputStream zos = new ZipOutputStream(out);\r
+\r
+        for (Enumeration en = zip.entries(); en.hasMoreElements(); ) {\r
+            ZipEntry ze = (ZipEntry)en.nextElement();\r
+            if(!ze.getName().equals(entry)){\r
+                zos.putNextEntry(new ZipEntry(ze.getName()));\r
+                InputStream is = zip.getInputStream(ze);\r
+                copyStream(is, zos);\r
+                is.close();\r
+            }\r
+        }\r
+        zos.putNextEntry(new ZipEntry(entry));\r
+        InputStream is = new FileInputStream(tmpfile);\r
+        copyStream(is, zos);\r
+        is.close();\r
+\r
+        zos.close();\r
+    }\r
+\r
+    private static void copyStream(InputStream in, OutputStream out) throws IOException {\r
+        byte[] chunk = new byte[1024];\r
+        int count;\r
+        while ((count = in.read(chunk)) >=0 ) {\r
+          out.write(chunk,0,count);\r
+        }\r
+    }\r
+\r
+    /**\r
+     * Writes spreadsheet data in a Writer.\r
+     * (YK: in future it may evolve in a full-featured API for streaming data in Excel)\r
+     */\r
+    public static class SpreadsheetWriter {\r
+        private Writer out;\r
+        private int rownum;\r
+\r
+        public SpreadsheetWriter(Writer out){\r
+            this.out = out;\r
+        }\r
+\r
+        public void beginSheet() throws IOException {\r
+            out.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +\r
+                    "<worksheet xmlns=\"http://schemas.openxmlformats.org/spreadsheetml/2006/main\">" );\r
+            out.write("<sheetData>\n");\r
+        }\r
+\r
+        public void endSheet() throws IOException {\r
+            out.write("</sheetData>");\r
+            out.write("</worksheet>");\r
+        }\r
+\r
+        /**\r
+         * Insert a new row\r
+         *\r
+         * @param rownum 0-based row number\r
+         */\r
+        public void insertRow(int rownum) throws IOException {\r
+            out.write("<row r=\""+(rownum+1)+"\">\n");\r
+            this.rownum = rownum;\r
+        }\r
+\r
+        /**\r
+         * Insert row end marker\r
+         */\r
+        public void endRow() throws IOException {\r
+            out.write("</row>\n");\r
+        }\r
+\r
+        public void createCell(int columnIndex, String value, int styleIndex) throws IOException {\r
+            String ref = new CellReference(rownum, columnIndex).formatAsString();\r
+            out.write("<c r=\""+ref+"\" t=\"inlineStr\"");\r
+            if(styleIndex != -1) out.write(" s=\""+styleIndex+"\"");\r
+            out.write(">");\r
+            out.write("<is><t>"+value+"</t></is>");\r
+            out.write("</c>");\r
+        }\r
+\r
+        public void createCell(int columnIndex, String value) throws IOException {\r
+            createCell(columnIndex, value, -1);\r
+        }\r
+\r
+        public void createCell(int columnIndex, double value, int styleIndex) throws IOException {\r
+            String ref = new CellReference(rownum, columnIndex).formatAsString();\r
+            out.write("<c r=\""+ref+"\" t=\"n\"");\r
+            if(styleIndex != -1) out.write(" s=\""+styleIndex+"\"");\r
+            out.write(">");\r
+            out.write("<v>"+value+"</v>");\r
+            out.write("</c>");\r
+        }\r
+\r
+        public void createCell(int columnIndex, double value) throws IOException {\r
+            createCell(columnIndex, value, -1);\r
+        }\r
+\r
+        public void createCell(int columnIndex, Calendar value, int styleIndex) throws IOException {\r
+            createCell(columnIndex, DateUtil.getExcelDate(value, false), styleIndex);\r
+        }\r
+    }\r
+\r
+}\r