-/* ====================================================================\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.usermodel.*;\r
-import org.openxmlformats.schemas.spreadsheetml.x2006.main.STHorizontalAlignment;\r
-\r
-import java.io.FileOutputStream;\r
-\r
-/**\r
- * Shows how various alignment options work.\r
- */\r
-public class AligningCells {\r
-\r
- public static void main(String[] args) throws Exception {\r
- Workbook wb = new XSSFWorkbook(); //or new HSSFWorkbook();\r
-\r
- Sheet sheet = wb.createSheet();\r
- Row row = sheet.createRow((short) 2);\r
- row.setHeightInPoints(30);\r
- for (int i = 0; i < 8; i++) {\r
- //column width is set in units of 1/256th of a character width\r
- sheet.setColumnWidth(i, 256*15);\r
- }\r
-\r
- createCell(wb, row, (short) 0, XSSFCellStyle.ALIGN_CENTER, XSSFCellStyle.VERTICAL_BOTTOM);\r
- createCell(wb, row, (short) 1, XSSFCellStyle.ALIGN_CENTER_SELECTION, XSSFCellStyle.VERTICAL_BOTTOM);\r
- createCell(wb, row, (short) 2, XSSFCellStyle.ALIGN_FILL, XSSFCellStyle.VERTICAL_CENTER);\r
- createCell(wb, row, (short) 3, XSSFCellStyle.ALIGN_GENERAL, XSSFCellStyle.VERTICAL_CENTER);\r
- createCell(wb, row, (short) 4, XSSFCellStyle.ALIGN_JUSTIFY, XSSFCellStyle.VERTICAL_JUSTIFY);\r
- createCell(wb, row, (short) 5, XSSFCellStyle.ALIGN_LEFT, XSSFCellStyle.VERTICAL_TOP);\r
- createCell(wb, row, (short) 6, XSSFCellStyle.ALIGN_RIGHT, XSSFCellStyle.VERTICAL_TOP);\r
-\r
- // Write the output to a file\r
- FileOutputStream fileOut = new FileOutputStream("xssf-align.xlsx");\r
- wb.write(fileOut);\r
- fileOut.close();\r
-\r
- }\r
-\r
- /**\r
- * Creates a cell and aligns it a certain way.\r
- *\r
- * @param wb the workbook\r
- * @param row the row to create the cell in\r
- * @param column the column number to create the cell in\r
- * @param halign the horizontal alignment for the cell.\r
- */\r
- private static void createCell(Workbook wb, Row row, short column, short halign, short valign) {\r
- Cell cell = row.createCell(column);\r
- cell.setCellValue(new XSSFRichTextString("Align It"));\r
- CellStyle cellStyle = wb.createCellStyle();\r
- cellStyle.setAlignment(halign);\r
- cellStyle.setVerticalAlignment(valign);\r
- cell.setCellStyle(cellStyle);\r
- }\r
-\r
-}\r
+/* ====================================================================
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+==================================================================== */
+
+package org.apache.poi.xssf.usermodel.examples;
+
+import java.io.FileOutputStream;
+import java.io.IOException;
+
+import org.apache.poi.ss.usermodel.*;
+import org.apache.poi.xssf.usermodel.*;
+
+/**
+ * Shows how various alignment options work.
+ */
+public class AligningCells {
+
+ public static void main(String[] args) throws IOException {
+ Workbook wb = new XSSFWorkbook(); //or new HSSFWorkbook();
+
+ Sheet sheet = wb.createSheet();
+ Row row = sheet.createRow((short) 2);
+ row.setHeightInPoints(30);
+ for (int i = 0; i < 8; i++) {
+ //column width is set in units of 1/256th of a character width
+ sheet.setColumnWidth(i, 256*15);
+ }
+
+ createCell(wb, row, (short) 0, XSSFCellStyle.ALIGN_CENTER, XSSFCellStyle.VERTICAL_BOTTOM);
+ createCell(wb, row, (short) 1, XSSFCellStyle.ALIGN_CENTER_SELECTION, XSSFCellStyle.VERTICAL_BOTTOM);
+ createCell(wb, row, (short) 2, XSSFCellStyle.ALIGN_FILL, XSSFCellStyle.VERTICAL_CENTER);
+ createCell(wb, row, (short) 3, XSSFCellStyle.ALIGN_GENERAL, XSSFCellStyle.VERTICAL_CENTER);
+ createCell(wb, row, (short) 4, XSSFCellStyle.ALIGN_JUSTIFY, XSSFCellStyle.VERTICAL_JUSTIFY);
+ createCell(wb, row, (short) 5, XSSFCellStyle.ALIGN_LEFT, XSSFCellStyle.VERTICAL_TOP);
+ createCell(wb, row, (short) 6, XSSFCellStyle.ALIGN_RIGHT, XSSFCellStyle.VERTICAL_TOP);
+
+ // Write the output to a file
+ FileOutputStream fileOut = new FileOutputStream("xssf-align.xlsx");
+ wb.write(fileOut);
+ fileOut.close();
+ }
+
+ /**
+ * Creates a cell and aligns it a certain way.
+ *
+ * @param wb the workbook
+ * @param row the row to create the cell in
+ * @param column the column number to create the cell in
+ * @param halign the horizontal alignment for the cell.
+ */
+ private static void createCell(Workbook wb, Row row, short column, short halign, short valign) {
+ Cell cell = row.createCell(column);
+ cell.setCellValue(new XSSFRichTextString("Align It"));
+ CellStyle cellStyle = wb.createCellStyle();
+ cellStyle.setAlignment(halign);
+ cellStyle.setVerticalAlignment(valign);
+ cell.setCellStyle(cellStyle);
+ }
+}
-/* ====================================================================\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
+/* ====================================================================
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+==================================================================== */
+
+package org.apache.poi.xssf.usermodel.examples;
+
+import java.io.*;
+import java.util.*;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipFile;
+import java.util.zip.ZipOutputStream;
+
+import org.apache.poi.ss.usermodel.DateUtil;
+import org.apache.poi.ss.usermodel.IndexedColors;
+import org.apache.poi.ss.util.CellReference;
+import org.apache.poi.xssf.usermodel.*;
+
+/**
+ * Demonstrates a workaround you can use to generate large workbooks and avoid OutOfMemory exception.
+ *
+ * The trick is as follows:
+ * 1. create a template workbook, create sheets and global objects such as cell styles, number formats, etc.
+ * 2. create an application that streams data in a text file
+ * 3. Substitute the sheet in the template with the generated data
+ *
+ * @author Yegor Kozlov
+ */
+public class BigGridDemo {
+ public static void main(String[] args) throws Exception {
+
+ // Step 1. Create a template file. Setup sheets and workbook-level objects such as
+ // cell styles, number formats, etc.
+
+ XSSFWorkbook wb = new XSSFWorkbook();
+ XSSFSheet sheet = wb.createSheet("Big Grid");
+
+ Map<String, XSSFCellStyle> styles = createStyles(wb);
+ //name of the zip entry holding sheet data, e.g. /xl/worksheets/sheet1.xml
+ String sheetRef = sheet.getPackagePart().getPartName().getName();
+
+ //save the template
+ FileOutputStream os = new FileOutputStream("template.xlsx");
+ wb.write(os);
+ os.close();
+
+ //Step 2. Generate XML file.
+ File tmp = File.createTempFile("sheet", ".xml");
+ Writer fw = new FileWriter(tmp);
+ generate(fw, styles);
+ fw.close();
+
+ //Step 3. Substitute the template entry with the generated data
+ FileOutputStream out = new FileOutputStream("big-grid.xlsx");
+ substitute(new File("template.xlsx"), tmp, sheetRef.substring(1), out);
+ out.close();
+ }
+
+ /**
+ * Create a library of cell styles.
+ */
+ private static Map<String, XSSFCellStyle> createStyles(XSSFWorkbook wb){
+ Map<String, XSSFCellStyle> styles = new HashMap<String, XSSFCellStyle>();
+ XSSFDataFormat fmt = wb.createDataFormat();
+
+ XSSFCellStyle style1 = wb.createCellStyle();
+ style1.setAlignment(XSSFCellStyle.ALIGN_RIGHT);
+ style1.setDataFormat(fmt.getFormat("0.0%"));
+ styles.put("percent", style1);
+
+ XSSFCellStyle style2 = wb.createCellStyle();
+ style2.setAlignment(XSSFCellStyle.ALIGN_CENTER);
+ style2.setDataFormat(fmt.getFormat("0.0X"));
+ styles.put("coeff", style2);
+
+ XSSFCellStyle style3 = wb.createCellStyle();
+ style3.setAlignment(XSSFCellStyle.ALIGN_RIGHT);
+ style3.setDataFormat(fmt.getFormat("$#,##0.00"));
+ styles.put("currency", style3);
+
+ XSSFCellStyle style4 = wb.createCellStyle();
+ style4.setAlignment(XSSFCellStyle.ALIGN_RIGHT);
+ style4.setDataFormat(fmt.getFormat("mmm dd"));
+ styles.put("date", style4);
+
+ XSSFCellStyle style5 = wb.createCellStyle();
+ XSSFFont headerFont = wb.createFont();
+ headerFont.setBold(true);
+ style5.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
+ style5.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND);
+ style5.setFont(headerFont);
+ styles.put("header", style5);
+
+ return styles;
+ }
+
+ private static void generate(Writer out, Map<String, XSSFCellStyle> styles) throws Exception {
+
+ Random rnd = new Random();
+ Calendar calendar = Calendar.getInstance();
+
+ SpreadsheetWriter sw = new SpreadsheetWriter(out);
+ sw.beginSheet();
+
+ //insert header row
+ sw.insertRow(0);
+ int styleIndex = styles.get("header").getIndex();
+ sw.createCell(0, "Title", styleIndex);
+ sw.createCell(1, "% Change", styleIndex);
+ sw.createCell(2, "Ratio", styleIndex);
+ sw.createCell(3, "Expenses", styleIndex);
+ sw.createCell(4, "Date", styleIndex);
+
+ sw.endRow();
+
+ //write data rows
+ for (int rownum = 1; rownum < 100000; rownum++) {
+ sw.insertRow(rownum);
+
+ sw.createCell(0, "Hello, " + rownum + "!");
+ sw.createCell(1, (double)rnd.nextInt(100)/100, styles.get("percent").getIndex());
+ sw.createCell(2, (double)rnd.nextInt(10)/10, styles.get("coeff").getIndex());
+ sw.createCell(3, rnd.nextInt(10000), styles.get("currency").getIndex());
+ sw.createCell(4, calendar, styles.get("date").getIndex());
+
+ sw.endRow();
+
+ calendar.roll(Calendar.DAY_OF_YEAR, 1);
+ }
+ sw.endSheet();
+ }
+
+ /**
+ *
+ * @param zipfile the template file
+ * @param tmpfile the XML file with the sheet data
+ * @param entry the name of the sheet entry to substitute, e.g. xl/worksheets/sheet1.xml
+ * @param out the stream to write the result to
+ */
+ private static void substitute(File zipfile, File tmpfile, String entry, OutputStream out) throws IOException {
+ ZipFile zip = new ZipFile(zipfile);
+
+ ZipOutputStream zos = new ZipOutputStream(out);
+
+ @SuppressWarnings("unchecked")
+ Enumeration<ZipEntry> en = (Enumeration<ZipEntry>) zip.entries();
+ while (en.hasMoreElements()) {
+ ZipEntry ze = en.nextElement();
+ if(!ze.getName().equals(entry)){
+ zos.putNextEntry(new ZipEntry(ze.getName()));
+ InputStream is = zip.getInputStream(ze);
+ copyStream(is, zos);
+ is.close();
+ }
+ }
+ zos.putNextEntry(new ZipEntry(entry));
+ InputStream is = new FileInputStream(tmpfile);
+ copyStream(is, zos);
+ is.close();
+
+ zos.close();
+ }
+
+ private static void copyStream(InputStream in, OutputStream out) throws IOException {
+ byte[] chunk = new byte[1024];
+ int count;
+ while ((count = in.read(chunk)) >=0 ) {
+ out.write(chunk,0,count);
+ }
+ }
+
+ /**
+ * Writes spreadsheet data in a Writer.
+ * (YK: in future it may evolve in a full-featured API for streaming data in Excel)
+ */
+ public static class SpreadsheetWriter {
+ private final Writer _out;
+ private int _rownum;
+
+ public SpreadsheetWriter(Writer out){
+ _out = out;
+ }
+
+ public void beginSheet() throws IOException {
+ _out.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
+ "<worksheet xmlns=\"http://schemas.openxmlformats.org/spreadsheetml/2006/main\">" );
+ _out.write("<sheetData>\n");
+ }
+
+ public void endSheet() throws IOException {
+ _out.write("</sheetData>");
+ _out.write("</worksheet>");
+ }
+
+ /**
+ * Insert a new row
+ *
+ * @param rownum 0-based row number
+ */
+ public void insertRow(int rownum) throws IOException {
+ _out.write("<row r=\""+(rownum+1)+"\">\n");
+ this._rownum = rownum;
+ }
+
+ /**
+ * Insert row end marker
+ */
+ public void endRow() throws IOException {
+ _out.write("</row>\n");
+ }
+
+ public void createCell(int columnIndex, String value, int styleIndex) throws IOException {
+ String ref = new CellReference(_rownum, columnIndex).formatAsString();
+ _out.write("<c r=\""+ref+"\" t=\"inlineStr\"");
+ if(styleIndex != -1) _out.write(" s=\""+styleIndex+"\"");
+ _out.write(">");
+ _out.write("<is><t>"+value+"</t></is>");
+ _out.write("</c>");
+ }
+
+ public void createCell(int columnIndex, String value) throws IOException {
+ createCell(columnIndex, value, -1);
+ }
+
+ public void createCell(int columnIndex, double value, int styleIndex) throws IOException {
+ String ref = new CellReference(_rownum, columnIndex).formatAsString();
+ _out.write("<c r=\""+ref+"\" t=\"n\"");
+ if(styleIndex != -1) _out.write(" s=\""+styleIndex+"\"");
+ _out.write(">");
+ _out.write("<v>"+value+"</v>");
+ _out.write("</c>");
+ }
+
+ public void createCell(int columnIndex, double value) throws IOException {
+ createCell(columnIndex, value, -1);
+ }
+
+ public void createCell(int columnIndex, Calendar value, int styleIndex) throws IOException {
+ createCell(columnIndex, DateUtil.getExcelDate(value, false), styleIndex);
+ }
+ }
+}
-/* ====================================================================\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.CellRangeAddress;\r
-import org.apache.poi.ss.usermodel.*;\r
-\r
-import java.io.FileOutputStream;\r
-import java.util.Calendar;\r
-import java.util.Map;\r
-import java.util.HashMap;\r
-\r
-/**\r
- * A monthly calendar created using Apache POI. Each month is on a separate sheet.\r
- * This is a version of org.apache.poi.ss.examples.CalendarDemo that demonstrates\r
- * some XSSF features not avaiable when using common HSSF-XSSF interfaces.\r
- *\r
- * <pre>\r
- * Usage:\r
- * CalendarDemo <year>\r
- * </pre>\r
- *\r
- * @author Yegor Kozlov\r
- */\r
-public class CalendarDemo {\r
-\r
- private static final String[] days = {\r
- "Sunday", "Monday", "Tuesday",\r
- "Wednesday", "Thursday", "Friday", "Saturday"};\r
-\r
- private static final String[] months = {\r
- "January", "February", "March","April", "May", "June","July", "August",\r
- "September","October", "November", "December"};\r
-\r
- public static void main(String[] args) throws Exception {\r
-\r
- Calendar calendar = Calendar.getInstance();\r
- if(args.length > 0) calendar.set(Calendar.YEAR, Integer.parseInt(args[0]));\r
-\r
- int year = calendar.get(Calendar.YEAR);\r
-\r
- XSSFWorkbook wb = new XSSFWorkbook();\r
- Map<String, XSSFCellStyle> styles = createStyles(wb);\r
-\r
- for (int month = 0; month < 12; month++) {\r
- calendar.set(Calendar.MONTH, month);\r
- calendar.set(Calendar.DAY_OF_MONTH, 1);\r
- //create a sheet for each month\r
- XSSFSheet sheet = wb.createSheet(months[month]);\r
-\r
- //turn off gridlines\r
- sheet.setDisplayGridlines(false);\r
- sheet.setPrintGridlines(false);\r
- XSSFPrintSetup printSetup = sheet.getPrintSetup();\r
- printSetup.setOrientation(PrintOrientation.LANDSCAPE);\r
- sheet.setFitToPage(true);\r
- sheet.setHorizontallyCenter(true);\r
-\r
- //the header row: centered text in 48pt font\r
- XSSFRow headerRow = sheet.createRow(0);\r
- headerRow.setHeightInPoints(80);\r
- XSSFCell titleCell = headerRow.createCell(0);\r
- titleCell.setCellValue(months[month] + " " + year);\r
- titleCell.setCellStyle(styles.get("title"));\r
- sheet.addMergedRegion(CellRangeAddress.valueOf("$A$1:$N$1"));\r
-\r
- //header with month titles\r
- XSSFRow monthRow = sheet.createRow(1);\r
- for (int i = 0; i < days.length; i++) {\r
- //for compatibility with HSSF we have to set column width in units of 1/256th of a character width\r
- sheet.setColumnWidth(i*2, 5*256); //the column is 5 characters wide\r
- sheet.setColumnWidth(i*2 + 1, 13*256); //the column is 13 characters wide\r
- sheet.addMergedRegion(new CellRangeAddress(1, 1, i*2, i*2+1));\r
- XSSFCell monthCell = monthRow.createCell(i*2);\r
- monthCell.setCellValue(days[i]);\r
- monthCell.setCellStyle(styles.get("month"));\r
- }\r
-\r
- int cnt = 1, day=1;\r
- int rownum = 2;\r
- for (int j = 0; j < 6; j++) {\r
- XSSFRow row = sheet.createRow(rownum++);\r
- row.setHeightInPoints(100);\r
- for (int i = 0; i < days.length; i++) {\r
- XSSFCell dayCell_1 = row.createCell(i*2);\r
- XSSFCell dayCell_2 = row.createCell(i*2 + 1);\r
-\r
- int day_of_week = calendar.get(Calendar.DAY_OF_WEEK);\r
- if(cnt >= day_of_week && calendar.get(Calendar.MONTH) == month) {\r
- dayCell_1.setCellValue(day);\r
- calendar.set(Calendar.DAY_OF_MONTH, ++day);\r
-\r
- if(i == 0 || i == days.length-1) {\r
- dayCell_1.setCellStyle(styles.get("weekend_left"));\r
- dayCell_2.setCellStyle(styles.get("weekend_right"));\r
- } else {\r
- dayCell_1.setCellStyle(styles.get("workday_left"));\r
- dayCell_2.setCellStyle(styles.get("workday_right"));\r
- }\r
- } else {\r
- dayCell_1.setCellStyle(styles.get("grey_left"));\r
- dayCell_2.setCellStyle(styles.get("grey_right"));\r
- }\r
- cnt++;\r
- }\r
- if(calendar.get(Calendar.MONTH) > month) break;\r
- }\r
- }\r
-\r
- // Write the output to a file\r
- FileOutputStream out = new FileOutputStream("calendar-"+year+".xlsx");\r
- wb.write(out);\r
- out.close();\r
- }\r
-\r
- /**\r
- * cell styles used for formatting calendar sheets\r
- */\r
- private static Map<String, XSSFCellStyle> createStyles(XSSFWorkbook wb){\r
- Map<String, XSSFCellStyle> styles = new HashMap<String, XSSFCellStyle>();\r
-\r
- XSSFCellStyle style;\r
- XSSFFont titleFont = wb.createFont();\r
- titleFont.setFontHeightInPoints((short)48);\r
- titleFont.setColor(new XSSFColor(new java.awt.Color(39, 51, 89)));\r
- style = wb.createCellStyle();\r
- style.setAlignment(HorizontalAlignment.CENTER);\r
- style.setVerticalAlignment(VerticalAlignment.CENTER);\r
- style.setFont(titleFont);\r
- styles.put("title", style);\r
-\r
- XSSFFont monthFont = wb.createFont();\r
- monthFont.setFontHeightInPoints((short)12);\r
- monthFont.setColor(new XSSFColor(new java.awt.Color(255, 255, 255)));\r
- monthFont.setBold(true);\r
- style = wb.createCellStyle();\r
- style.setAlignment(HorizontalAlignment.CENTER);\r
- style.setVerticalAlignment(VerticalAlignment.CENTER);\r
- style.setFillForegroundColor(new XSSFColor(new java.awt.Color(39, 51, 89)));\r
- style.setFillPattern(FillPatternType.SOLID_FOREGROUND);\r
- style.setFont(monthFont);\r
- styles.put("month", style);\r
-\r
- XSSFFont dayFont = wb.createFont();\r
- dayFont.setFontHeightInPoints((short)14);\r
- dayFont.setBold(true);\r
- style = wb.createCellStyle();\r
- style.setAlignment(HorizontalAlignment.LEFT);\r
- style.setVerticalAlignment(VerticalAlignment.TOP);\r
- style.setFillForegroundColor(new XSSFColor(new java.awt.Color(228, 232, 243)));\r
- style.setFillPattern(FillPatternType.SOLID_FOREGROUND);\r
- style.setBorderLeft(BorderStyle.THIN);\r
- style.setLeftBorderColor(new XSSFColor(new java.awt.Color(39, 51, 89)));\r
- style.setBorderBottom(BorderStyle.THIN);\r
- style.setBottomBorderColor(new XSSFColor(new java.awt.Color(39, 51, 89)));\r
- style.setFont(dayFont);\r
- styles.put("weekend_left", style);\r
-\r
- style = wb.createCellStyle();\r
- style.setAlignment(HorizontalAlignment.CENTER);\r
- style.setVerticalAlignment(VerticalAlignment.TOP);\r
- style.setFillForegroundColor(new XSSFColor(new java.awt.Color(228, 232, 243)));\r
- style.setFillPattern(FillPatternType.SOLID_FOREGROUND);\r
- style.setBorderRight(BorderStyle.THIN);\r
- style.setRightBorderColor(new XSSFColor(new java.awt.Color(39, 51, 89)));\r
- style.setBorderBottom(BorderStyle.THIN);\r
- style.setBottomBorderColor(new XSSFColor(new java.awt.Color(39, 51, 89)));\r
- styles.put("weekend_right", style);\r
-\r
- style = wb.createCellStyle();\r
- style.setAlignment(HorizontalAlignment.LEFT);\r
- style.setVerticalAlignment(VerticalAlignment.TOP);\r
- style.setBorderLeft(BorderStyle.THIN);\r
- style.setFillForegroundColor(new XSSFColor(new java.awt.Color(255, 255, 255)));\r
- style.setFillPattern(FillPatternType.SOLID_FOREGROUND);\r
- style.setLeftBorderColor(new XSSFColor(new java.awt.Color(39, 51, 89)));\r
- style.setBorderBottom(BorderStyle.THIN);\r
- style.setBottomBorderColor(new XSSFColor(new java.awt.Color(39, 51, 89)));\r
- style.setFont(dayFont);\r
- styles.put("workday_left", style);\r
-\r
- style = wb.createCellStyle();\r
- style.setAlignment(HorizontalAlignment.CENTER);\r
- style.setVerticalAlignment(VerticalAlignment.TOP);\r
- style.setFillForegroundColor(new XSSFColor(new java.awt.Color(255, 255, 255)));\r
- style.setFillPattern(FillPatternType.SOLID_FOREGROUND);\r
- style.setBorderRight(BorderStyle.THIN);\r
- style.setRightBorderColor(new XSSFColor(new java.awt.Color(39, 51, 89)));\r
- style.setBorderBottom(BorderStyle.THIN);\r
- style.setBottomBorderColor(new XSSFColor(new java.awt.Color(39, 51, 89)));\r
- styles.put("workday_right", style);\r
-\r
- style = wb.createCellStyle();\r
- style.setBorderLeft(BorderStyle.THIN);\r
- style.setFillForegroundColor(new XSSFColor(new java.awt.Color(234, 234, 234)));\r
- style.setFillPattern(FillPatternType.SOLID_FOREGROUND);\r
- style.setBorderBottom(BorderStyle.THIN);\r
- style.setBottomBorderColor(new XSSFColor(new java.awt.Color(39, 51, 89)));\r
- styles.put("grey_left", style);\r
-\r
- style = wb.createCellStyle();\r
- style.setFillForegroundColor(new XSSFColor(new java.awt.Color(234, 234, 234)));\r
- style.setFillPattern(FillPatternType.SOLID_FOREGROUND);\r
- style.setBorderRight(BorderStyle.THIN);\r
- style.setRightBorderColor(new XSSFColor(new java.awt.Color(39, 51, 89)));\r
- style.setBorderBottom(BorderStyle.THIN);\r
- style.setBottomBorderColor(new XSSFColor(new java.awt.Color(39, 51, 89)));\r
- styles.put("grey_right", style);\r
-\r
- return styles;\r
- }\r
-}\r
+/* ====================================================================
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+==================================================================== */
+
+package org.apache.poi.xssf.usermodel.examples;
+
+import org.apache.poi.xssf.usermodel.*;
+import org.apache.poi.ss.util.CellRangeAddress;
+import org.apache.poi.ss.usermodel.*;
+
+import java.io.FileOutputStream;
+import java.util.Calendar;
+import java.util.Map;
+import java.util.HashMap;
+
+/**
+ * A monthly calendar created using Apache POI. Each month is on a separate sheet.
+ * This is a version of org.apache.poi.ss.examples.CalendarDemo that demonstrates
+ * some XSSF features not avaiable when using common HSSF-XSSF interfaces.
+ *
+ * <pre>
+ * Usage:
+ * CalendarDemo <year>
+ * </pre>
+ *
+ * @author Yegor Kozlov
+ */
+public class CalendarDemo {
+
+ private static final String[] days = {
+ "Sunday", "Monday", "Tuesday",
+ "Wednesday", "Thursday", "Friday", "Saturday"};
+
+ private static final String[] months = {
+ "January", "February", "March","April", "May", "June","July", "August",
+ "September","October", "November", "December"};
+
+ public static void main(String[] args) throws Exception {
+
+ Calendar calendar = Calendar.getInstance();
+ if(args.length > 0) calendar.set(Calendar.YEAR, Integer.parseInt(args[0]));
+
+ int year = calendar.get(Calendar.YEAR);
+
+ XSSFWorkbook wb = new XSSFWorkbook();
+ Map<String, XSSFCellStyle> styles = createStyles(wb);
+
+ for (int month = 0; month < 12; month++) {
+ calendar.set(Calendar.MONTH, month);
+ calendar.set(Calendar.DAY_OF_MONTH, 1);
+ //create a sheet for each month
+ XSSFSheet sheet = wb.createSheet(months[month]);
+
+ //turn off gridlines
+ sheet.setDisplayGridlines(false);
+ sheet.setPrintGridlines(false);
+ XSSFPrintSetup printSetup = sheet.getPrintSetup();
+ printSetup.setOrientation(PrintOrientation.LANDSCAPE);
+ sheet.setFitToPage(true);
+ sheet.setHorizontallyCenter(true);
+
+ //the header row: centered text in 48pt font
+ XSSFRow headerRow = sheet.createRow(0);
+ headerRow.setHeightInPoints(80);
+ XSSFCell titleCell = headerRow.createCell(0);
+ titleCell.setCellValue(months[month] + " " + year);
+ titleCell.setCellStyle(styles.get("title"));
+ sheet.addMergedRegion(CellRangeAddress.valueOf("$A$1:$N$1"));
+
+ //header with month titles
+ XSSFRow monthRow = sheet.createRow(1);
+ for (int i = 0; i < days.length; i++) {
+ //for compatibility with HSSF we have to set column width in units of 1/256th of a character width
+ sheet.setColumnWidth(i*2, 5*256); //the column is 5 characters wide
+ sheet.setColumnWidth(i*2 + 1, 13*256); //the column is 13 characters wide
+ sheet.addMergedRegion(new CellRangeAddress(1, 1, i*2, i*2+1));
+ XSSFCell monthCell = monthRow.createCell(i*2);
+ monthCell.setCellValue(days[i]);
+ monthCell.setCellStyle(styles.get("month"));
+ }
+
+ int cnt = 1, day=1;
+ int rownum = 2;
+ for (int j = 0; j < 6; j++) {
+ XSSFRow row = sheet.createRow(rownum++);
+ row.setHeightInPoints(100);
+ for (int i = 0; i < days.length; i++) {
+ XSSFCell dayCell_1 = row.createCell(i*2);
+ XSSFCell dayCell_2 = row.createCell(i*2 + 1);
+
+ int day_of_week = calendar.get(Calendar.DAY_OF_WEEK);
+ if(cnt >= day_of_week && calendar.get(Calendar.MONTH) == month) {
+ dayCell_1.setCellValue(day);
+ calendar.set(Calendar.DAY_OF_MONTH, ++day);
+
+ if(i == 0 || i == days.length-1) {
+ dayCell_1.setCellStyle(styles.get("weekend_left"));
+ dayCell_2.setCellStyle(styles.get("weekend_right"));
+ } else {
+ dayCell_1.setCellStyle(styles.get("workday_left"));
+ dayCell_2.setCellStyle(styles.get("workday_right"));
+ }
+ } else {
+ dayCell_1.setCellStyle(styles.get("grey_left"));
+ dayCell_2.setCellStyle(styles.get("grey_right"));
+ }
+ cnt++;
+ }
+ if(calendar.get(Calendar.MONTH) > month) break;
+ }
+ }
+
+ // Write the output to a file
+ FileOutputStream out = new FileOutputStream("calendar-"+year+".xlsx");
+ wb.write(out);
+ out.close();
+ }
+
+ /**
+ * cell styles used for formatting calendar sheets
+ */
+ private static Map<String, XSSFCellStyle> createStyles(XSSFWorkbook wb){
+ Map<String, XSSFCellStyle> styles = new HashMap<String, XSSFCellStyle>();
+
+ XSSFCellStyle style;
+ XSSFFont titleFont = wb.createFont();
+ titleFont.setFontHeightInPoints((short)48);
+ titleFont.setColor(new XSSFColor(new java.awt.Color(39, 51, 89)));
+ style = wb.createCellStyle();
+ style.setAlignment(HorizontalAlignment.CENTER);
+ style.setVerticalAlignment(VerticalAlignment.CENTER);
+ style.setFont(titleFont);
+ styles.put("title", style);
+
+ XSSFFont monthFont = wb.createFont();
+ monthFont.setFontHeightInPoints((short)12);
+ monthFont.setColor(new XSSFColor(new java.awt.Color(255, 255, 255)));
+ monthFont.setBold(true);
+ style = wb.createCellStyle();
+ style.setAlignment(HorizontalAlignment.CENTER);
+ style.setVerticalAlignment(VerticalAlignment.CENTER);
+ style.setFillForegroundColor(new XSSFColor(new java.awt.Color(39, 51, 89)));
+ style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
+ style.setFont(monthFont);
+ styles.put("month", style);
+
+ XSSFFont dayFont = wb.createFont();
+ dayFont.setFontHeightInPoints((short)14);
+ dayFont.setBold(true);
+ style = wb.createCellStyle();
+ style.setAlignment(HorizontalAlignment.LEFT);
+ style.setVerticalAlignment(VerticalAlignment.TOP);
+ style.setFillForegroundColor(new XSSFColor(new java.awt.Color(228, 232, 243)));
+ style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
+ style.setBorderLeft(BorderStyle.THIN);
+ style.setLeftBorderColor(new XSSFColor(new java.awt.Color(39, 51, 89)));
+ style.setBorderBottom(BorderStyle.THIN);
+ style.setBottomBorderColor(new XSSFColor(new java.awt.Color(39, 51, 89)));
+ style.setFont(dayFont);
+ styles.put("weekend_left", style);
+
+ style = wb.createCellStyle();
+ style.setAlignment(HorizontalAlignment.CENTER);
+ style.setVerticalAlignment(VerticalAlignment.TOP);
+ style.setFillForegroundColor(new XSSFColor(new java.awt.Color(228, 232, 243)));
+ style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
+ style.setBorderRight(BorderStyle.THIN);
+ style.setRightBorderColor(new XSSFColor(new java.awt.Color(39, 51, 89)));
+ style.setBorderBottom(BorderStyle.THIN);
+ style.setBottomBorderColor(new XSSFColor(new java.awt.Color(39, 51, 89)));
+ styles.put("weekend_right", style);
+
+ style = wb.createCellStyle();
+ style.setAlignment(HorizontalAlignment.LEFT);
+ style.setVerticalAlignment(VerticalAlignment.TOP);
+ style.setBorderLeft(BorderStyle.THIN);
+ style.setFillForegroundColor(new XSSFColor(new java.awt.Color(255, 255, 255)));
+ style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
+ style.setLeftBorderColor(new XSSFColor(new java.awt.Color(39, 51, 89)));
+ style.setBorderBottom(BorderStyle.THIN);
+ style.setBottomBorderColor(new XSSFColor(new java.awt.Color(39, 51, 89)));
+ style.setFont(dayFont);
+ styles.put("workday_left", style);
+
+ style = wb.createCellStyle();
+ style.setAlignment(HorizontalAlignment.CENTER);
+ style.setVerticalAlignment(VerticalAlignment.TOP);
+ style.setFillForegroundColor(new XSSFColor(new java.awt.Color(255, 255, 255)));
+ style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
+ style.setBorderRight(BorderStyle.THIN);
+ style.setRightBorderColor(new XSSFColor(new java.awt.Color(39, 51, 89)));
+ style.setBorderBottom(BorderStyle.THIN);
+ style.setBottomBorderColor(new XSSFColor(new java.awt.Color(39, 51, 89)));
+ styles.put("workday_right", style);
+
+ style = wb.createCellStyle();
+ style.setBorderLeft(BorderStyle.THIN);
+ style.setFillForegroundColor(new XSSFColor(new java.awt.Color(234, 234, 234)));
+ style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
+ style.setBorderBottom(BorderStyle.THIN);
+ style.setBottomBorderColor(new XSSFColor(new java.awt.Color(39, 51, 89)));
+ styles.put("grey_left", style);
+
+ style = wb.createCellStyle();
+ style.setFillForegroundColor(new XSSFColor(new java.awt.Color(234, 234, 234)));
+ style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
+ style.setBorderRight(BorderStyle.THIN);
+ style.setRightBorderColor(new XSSFColor(new java.awt.Color(39, 51, 89)));
+ style.setBorderBottom(BorderStyle.THIN);
+ style.setBottomBorderColor(new XSSFColor(new java.awt.Color(39, 51, 89)));
+ styles.put("grey_right", style);
+
+ return styles;
+ }
+}
import java.util.Date;
import org.apache.poi.ss.usermodel.*;
-import org.apache.poi.xssf.usermodel.XSSFRichTextString;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
/**
-/* ====================================================================\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.XSSFWorkbook;\r
-import org.apache.poi.ss.usermodel.Workbook;\r
-import org.apache.poi.ss.usermodel.Sheet;\r
-import org.apache.poi.ss.usermodel.Cell;\r
-import org.apache.poi.ss.usermodel.Row;\r
-\r
-/**\r
- * Iterate over rows and cells\r
- */\r
-public class IterateCells {\r
-\r
- public static void main(String[] args) throws Exception {\r
- Workbook wb = new XSSFWorkbook(args[0]);\r
- for (int i = 0; i < wb.getNumberOfSheets(); i++) {\r
- Sheet sheet = wb.getSheetAt(i);\r
- System.out.println(wb.getSheetName(i));\r
- for (Row row : sheet) {\r
- System.out.println("rownum: " + row.getRowNum());\r
- for (Cell cell : row) {\r
- System.out.println(cell.toString());\r
- }\r
- }\r
- }\r
-\r
- }\r
-}\r
+/* ====================================================================
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+==================================================================== */
+
+package org.apache.poi.xssf.usermodel.examples;
+
+import org.apache.poi.xssf.usermodel.XSSFWorkbook;
+import org.apache.poi.ss.usermodel.Workbook;
+import org.apache.poi.ss.usermodel.Sheet;
+import org.apache.poi.ss.usermodel.Cell;
+import org.apache.poi.ss.usermodel.Row;
+
+/**
+ * Iterate over rows and cells
+ */
+public class IterateCells {
+
+ public static void main(String[] args) throws Exception {
+ Workbook wb = new XSSFWorkbook(args[0]);
+ for (int i = 0; i < wb.getNumberOfSheets(); i++) {
+ Sheet sheet = wb.getSheetAt(i);
+ System.out.println(wb.getSheetName(i));
+ for (Row row : sheet) {
+ System.out.println("rownum: " + row.getRowNum());
+ for (Cell cell : row) {
+ System.out.println(cell.toString());
+ }
+ }
+ }
+ }
+}
-/* ====================================================================\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.ss.usermodel.Workbook;\r
-import org.apache.poi.ss.usermodel.Sheet;\r
-import org.apache.poi.ss.usermodel.Row;\r
-import org.apache.poi.ss.usermodel.Cell;\r
-import org.apache.poi.ss.util.CellRangeAddress;\r
-import org.apache.poi.xssf.usermodel.XSSFWorkbook;\r
-import org.apache.poi.xssf.usermodel.XSSFRichTextString;\r
-import org.apache.poi.hssf.util.Region;\r
-\r
-import java.io.FileOutputStream;\r
-\r
-/**\r
- * An example of how to merge regions of cells.\r
- */\r
-public class MergingCells {\r
- public static void main(String[] args) throws Exception {\r
- Workbook wb = new XSSFWorkbook(); //or new HSSFWorkbook();\r
- Sheet sheet = wb.createSheet("new sheet");\r
-\r
- Row row = sheet.createRow((short) 1);\r
- Cell cell = row.createCell((short) 1);\r
- cell.setCellValue(new XSSFRichTextString("This is a test of merging"));\r
-\r
- sheet.addMergedRegion(new CellRangeAddress(1, 1, 1, 2));\r
-\r
- // Write the output to a file\r
- FileOutputStream fileOut = new FileOutputStream("merging_cells.xlsx");\r
- wb.write(fileOut);\r
- fileOut.close();\r
-\r
- }\r
-}\r
+/* ====================================================================
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+==================================================================== */
+
+package org.apache.poi.xssf.usermodel.examples;
+
+import org.apache.poi.ss.usermodel.Workbook;
+import org.apache.poi.ss.usermodel.Sheet;
+import org.apache.poi.ss.usermodel.Row;
+import org.apache.poi.ss.usermodel.Cell;
+import org.apache.poi.ss.util.CellRangeAddress;
+import org.apache.poi.xssf.usermodel.XSSFWorkbook;
+import org.apache.poi.xssf.usermodel.XSSFRichTextString;
+
+import java.io.FileOutputStream;
+
+/**
+ * An example of how to merge regions of cells.
+ */
+public class MergingCells {
+ public static void main(String[] args) throws Exception {
+ Workbook wb = new XSSFWorkbook(); //or new HSSFWorkbook();
+ Sheet sheet = wb.createSheet("new sheet");
+
+ Row row = sheet.createRow((short) 1);
+ Cell cell = row.createCell((short) 1);
+ cell.setCellValue(new XSSFRichTextString("This is a test of merging"));
+
+ sheet.addMergedRegion(new CellRangeAddress(1, 1, 1, 2));
+
+ // Write the output to a file
+ FileOutputStream fileOut = new FileOutputStream("merging_cells.xlsx");
+ wb.write(fileOut);
+ fileOut.close();
+ }
+}
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
-import org.apache.poi.hssf.usermodel.HSSFWorkbook;
public class SelectedSheet {
public static void main(String[]args) throws Exception {
Workbook wb = new XSSFWorkbook(); //or new HSSFWorkbook();
- Sheet sheet = wb.createSheet("row sheet");
+ Sheet sheet = wb.createSheet("row sheet");
Sheet sheet2 = wb.createSheet("another sheet");
Sheet sheet3 = wb.createSheet(" sheet 3 ");
sheet3.setSelected(true);
-/* ====================================================================\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.ss.usermodel.Workbook;\r
-import org.apache.poi.ss.usermodel.Sheet;\r
-import org.apache.poi.xssf.usermodel.XSSFWorkbook;\r
-\r
-import java.io.FileOutputStream;\r
-\r
-/**\r
- * How to set spklit and freeze panes\r
- */\r
-public class SplitAndFreezePanes {\r
- public static void main(String[]args) throws Exception {\r
- Workbook wb = new XSSFWorkbook();\r
- Sheet sheet1 = wb.createSheet("new sheet");\r
- Sheet sheet2 = wb.createSheet("second sheet");\r
- Sheet sheet3 = wb.createSheet("third sheet");\r
- Sheet sheet4 = wb.createSheet("fourth sheet");\r
-\r
- // Freeze just one row\r
- sheet1.createFreezePane(0, 1, 0, 1);\r
- // Freeze just one column\r
- sheet2.createFreezePane(1, 0, 1, 0);\r
- // Freeze the columns and rows (forget about scrolling position of the lower right quadrant).\r
- sheet3.createFreezePane(2, 2);\r
- // Create a split with the lower left side being the active quadrant\r
- sheet4.createSplitPane(2000, 2000, 0, 0, Sheet.PANE_LOWER_LEFT);\r
-\r
- FileOutputStream fileOut = new FileOutputStream("splitFreezePane.xlsx");\r
- wb.write(fileOut);\r
- fileOut.close();\r
-\r
- }\r
-}\r
+/* ====================================================================
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+==================================================================== */
+
+package org.apache.poi.xssf.usermodel.examples;
+
+import org.apache.poi.ss.usermodel.Workbook;
+import org.apache.poi.ss.usermodel.Sheet;
+import org.apache.poi.xssf.usermodel.XSSFWorkbook;
+
+import java.io.FileOutputStream;
+
+/**
+ * How to set spklit and freeze panes
+ */
+public class SplitAndFreezePanes {
+ public static void main(String[]args) throws Exception {
+ Workbook wb = new XSSFWorkbook();
+ Sheet sheet1 = wb.createSheet("new sheet");
+ Sheet sheet2 = wb.createSheet("second sheet");
+ Sheet sheet3 = wb.createSheet("third sheet");
+ Sheet sheet4 = wb.createSheet("fourth sheet");
+
+ // Freeze just one row
+ sheet1.createFreezePane(0, 1, 0, 1);
+ // Freeze just one column
+ sheet2.createFreezePane(1, 0, 1, 0);
+ // Freeze the columns and rows (forget about scrolling position of the lower right quadrant).
+ sheet3.createFreezePane(2, 2);
+ // Create a split with the lower left side being the active quadrant
+ sheet4.createSplitPane(2000, 2000, 0, 0, Sheet.PANE_LOWER_LEFT);
+
+ FileOutputStream fileOut = new FileOutputStream("splitFreezePane.xlsx");
+ wb.write(fileOut);
+ fileOut.close();
+ }
+}
-/* ====================================================================\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.XSSFWorkbook;\r
-import org.apache.poi.ss.usermodel.IndexedColors;\r
-import org.apache.poi.ss.usermodel.*;\r
-\r
-import java.io.FileOutputStream;\r
-\r
-/**\r
- * Working with borders\r
- */\r
-public class WorkingWithBorders {\r
- public static void main(String[] args) throws Exception {\r
- Workbook wb = new XSSFWorkbook(); //or new HSSFWorkbook();\r
- Sheet sheet = wb.createSheet("borders");\r
-\r
- // Create a row and put some cells in it. Rows are 0 based.\r
- Row row = sheet.createRow((short) 1);\r
-\r
- // Create a cell and put a value in it.\r
- Cell cell = row.createCell((short) 1);\r
- cell.setCellValue(4);\r
-\r
- // Style the cell with borders all around.\r
- CellStyle style = wb.createCellStyle();\r
- style.setBorderBottom(CellStyle.BORDER_THIN);\r
- style.setBottomBorderColor(IndexedColors.BLACK.getIndex());\r
- style.setBorderLeft(CellStyle.BORDER_THIN);\r
- style.setLeftBorderColor(IndexedColors.GREEN.getIndex());\r
- style.setBorderRight(CellStyle.BORDER_THIN);\r
- style.setRightBorderColor(IndexedColors.BLUE.getIndex());\r
- style.setBorderTop(CellStyle.BORDER_MEDIUM_DASHED);\r
- style.setTopBorderColor(IndexedColors.BLACK.getIndex());\r
- cell.setCellStyle(style);\r
-\r
- // Write the output to a file\r
- FileOutputStream fileOut = new FileOutputStream("xssf-borders.xlsx");\r
- wb.write(fileOut);\r
- fileOut.close();\r
-\r
- }\r
-}\r
+/* ====================================================================
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+==================================================================== */
+
+package org.apache.poi.xssf.usermodel.examples;
+
+import org.apache.poi.xssf.usermodel.XSSFWorkbook;
+import org.apache.poi.ss.usermodel.IndexedColors;
+import org.apache.poi.ss.usermodel.*;
+
+import java.io.FileOutputStream;
+
+/**
+ * Working with borders
+ */
+public class WorkingWithBorders {
+ public static void main(String[] args) throws Exception {
+ Workbook wb = new XSSFWorkbook(); //or new HSSFWorkbook();
+ Sheet sheet = wb.createSheet("borders");
+
+ // Create a row and put some cells in it. Rows are 0 based.
+ Row row = sheet.createRow((short) 1);
+
+ // Create a cell and put a value in it.
+ Cell cell = row.createCell((short) 1);
+ cell.setCellValue(4);
+
+ // Style the cell with borders all around.
+ CellStyle style = wb.createCellStyle();
+ style.setBorderBottom(CellStyle.BORDER_THIN);
+ style.setBottomBorderColor(IndexedColors.BLACK.getIndex());
+ style.setBorderLeft(CellStyle.BORDER_THIN);
+ style.setLeftBorderColor(IndexedColors.GREEN.getIndex());
+ style.setBorderRight(CellStyle.BORDER_THIN);
+ style.setRightBorderColor(IndexedColors.BLUE.getIndex());
+ style.setBorderTop(CellStyle.BORDER_MEDIUM_DASHED);
+ style.setTopBorderColor(IndexedColors.BLACK.getIndex());
+ cell.setCellStyle(style);
+
+ // Write the output to a file
+ FileOutputStream fileOut = new FileOutputStream("xssf-borders.xlsx");
+ wb.write(fileOut);
+ fileOut.close();
+ }
+}
-/* ====================================================================\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.ss.usermodel.*;\r
-import org.apache.poi.xssf.usermodel.XSSFWorkbook;\r
-import org.apache.poi.ss.usermodel.IndexedColors;\r
-\r
-import java.io.FileOutputStream;\r
-\r
-/**\r
- * Working with Fonts\r
- */\r
-public class WorkingWithFonts {\r
- public static void main(String[] args) throws Exception {\r
- Workbook wb = new XSSFWorkbook(); //or new HSSFWorkbook();\r
- Sheet sheet = wb.createSheet("Fonts");\r
-\r
- Font font0 = wb.createFont();\r
- font0.setColor(IndexedColors.BROWN.getIndex());\r
- CellStyle style0 = wb.createCellStyle();\r
- style0.setFont(font0);\r
-\r
- Font font1 = wb.createFont();\r
- font1.setFontHeightInPoints((short)14);\r
- font1.setFontName("Courier New");\r
- font1.setColor(IndexedColors.RED.getIndex());\r
- CellStyle style1 = wb.createCellStyle();\r
- style1.setFont(font1);\r
-\r
- Font font2 = wb.createFont();\r
- font2.setFontHeightInPoints((short)16);\r
- font2.setFontName("Arial");\r
- font2.setColor(IndexedColors.GREEN.getIndex());\r
- CellStyle style2 = wb.createCellStyle();\r
- style2.setFont(font2);\r
-\r
- Font font3 = wb.createFont();\r
- font3.setFontHeightInPoints((short)18);\r
- font3.setFontName("Times New Roman");\r
- font3.setColor(IndexedColors.LAVENDER.getIndex());\r
- CellStyle style3 = wb.createCellStyle();\r
- style3.setFont(font3);\r
-\r
- Font font4 = wb.createFont();\r
- font4.setFontHeightInPoints((short)18);\r
- font4.setFontName("Wingdings");\r
- font4.setColor(IndexedColors.GOLD.getIndex());\r
- CellStyle style4 = wb.createCellStyle();\r
- style4.setFont(font4);\r
-\r
- Font font5 = wb.createFont();\r
- font5.setFontName("Symbol");\r
- CellStyle style5 = wb.createCellStyle();\r
- style5.setFont(font5);\r
-\r
- Cell cell0 = sheet.createRow(0).createCell(1);\r
- cell0.setCellValue("Default");\r
- cell0.setCellStyle(style0);\r
-\r
- Cell cell1 = sheet.createRow(1).createCell(1);\r
- cell1.setCellValue("Courier");\r
- cell1.setCellStyle(style1);\r
-\r
- Cell cell2 = sheet.createRow(2).createCell(1);\r
- cell2.setCellValue("Arial");\r
- cell2.setCellStyle(style2);\r
-\r
- Cell cell3 = sheet.createRow(3).createCell(1);\r
- cell3.setCellValue("Times New Roman");\r
- cell3.setCellStyle(style3);\r
-\r
- Cell cell4 = sheet.createRow(4).createCell(1);\r
- cell4.setCellValue("Wingdings");\r
- cell4.setCellStyle(style4);\r
-\r
- Cell cell5 = sheet.createRow(5).createCell(1);\r
- cell5.setCellValue("Symbol");\r
- cell5.setCellStyle(style5);\r
-\r
- // Write the output to a file\r
- FileOutputStream fileOut = new FileOutputStream("xssf-fonts.xlsx");\r
- wb.write(fileOut);\r
- fileOut.close();\r
- }\r
-}\r
+/* ====================================================================
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+==================================================================== */
+
+package org.apache.poi.xssf.usermodel.examples;
+
+import org.apache.poi.ss.usermodel.*;
+import org.apache.poi.xssf.usermodel.XSSFWorkbook;
+import org.apache.poi.ss.usermodel.IndexedColors;
+
+import java.io.FileOutputStream;
+
+/**
+ * Working with Fonts
+ */
+public class WorkingWithFonts {
+ public static void main(String[] args) throws Exception {
+ Workbook wb = new XSSFWorkbook(); //or new HSSFWorkbook();
+ Sheet sheet = wb.createSheet("Fonts");
+
+ Font font0 = wb.createFont();
+ font0.setColor(IndexedColors.BROWN.getIndex());
+ CellStyle style0 = wb.createCellStyle();
+ style0.setFont(font0);
+
+ Font font1 = wb.createFont();
+ font1.setFontHeightInPoints((short)14);
+ font1.setFontName("Courier New");
+ font1.setColor(IndexedColors.RED.getIndex());
+ CellStyle style1 = wb.createCellStyle();
+ style1.setFont(font1);
+
+ Font font2 = wb.createFont();
+ font2.setFontHeightInPoints((short)16);
+ font2.setFontName("Arial");
+ font2.setColor(IndexedColors.GREEN.getIndex());
+ CellStyle style2 = wb.createCellStyle();
+ style2.setFont(font2);
+
+ Font font3 = wb.createFont();
+ font3.setFontHeightInPoints((short)18);
+ font3.setFontName("Times New Roman");
+ font3.setColor(IndexedColors.LAVENDER.getIndex());
+ CellStyle style3 = wb.createCellStyle();
+ style3.setFont(font3);
+
+ Font font4 = wb.createFont();
+ font4.setFontHeightInPoints((short)18);
+ font4.setFontName("Wingdings");
+ font4.setColor(IndexedColors.GOLD.getIndex());
+ CellStyle style4 = wb.createCellStyle();
+ style4.setFont(font4);
+
+ Font font5 = wb.createFont();
+ font5.setFontName("Symbol");
+ CellStyle style5 = wb.createCellStyle();
+ style5.setFont(font5);
+
+ Cell cell0 = sheet.createRow(0).createCell(1);
+ cell0.setCellValue("Default");
+ cell0.setCellStyle(style0);
+
+ Cell cell1 = sheet.createRow(1).createCell(1);
+ cell1.setCellValue("Courier");
+ cell1.setCellStyle(style1);
+
+ Cell cell2 = sheet.createRow(2).createCell(1);
+ cell2.setCellValue("Arial");
+ cell2.setCellStyle(style2);
+
+ Cell cell3 = sheet.createRow(3).createCell(1);
+ cell3.setCellValue("Times New Roman");
+ cell3.setCellStyle(style3);
+
+ Cell cell4 = sheet.createRow(4).createCell(1);
+ cell4.setCellValue("Wingdings");
+ cell4.setCellStyle(style4);
+
+ Cell cell5 = sheet.createRow(5).createCell(1);
+ cell5.setCellValue("Symbol");
+ cell5.setCellStyle(style5);
+
+ // Write the output to a file
+ FileOutputStream fileOut = new FileOutputStream("xssf-fonts.xlsx");
+ wb.write(fileOut);
+ fileOut.close();
+ }
+}
-/* ====================================================================\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.usermodel.*;\r
-import org.apache.poi.util.IOUtils;\r
-import org.apache.poi.hssf.usermodel.HSSFWorkbook;\r
-\r
-import java.io.IOException;\r
-import java.io.InputStream;\r
-import java.io.FileInputStream;\r
-import java.io.FileOutputStream;\r
-\r
-/**\r
- * Demonstrates how to insert pictures in a SpreadsheetML document\r
- *\r
- * @author Yegor Kozlov\r
- */\r
-public class WorkingWithPictures {\r
- public static void main(String[] args) throws IOException {\r
-\r
- //create a new workbook\r
- Workbook wb = new XSSFWorkbook(); //or new HSSFWorkbook();\r
- CreationHelper helper = wb.getCreationHelper();\r
-\r
- //add a picture in this workbook.\r
- InputStream is = new FileInputStream(args[0]);\r
- byte[] bytes = IOUtils.toByteArray(is);\r
- is.close();\r
- int pictureIdx = wb.addPicture(bytes, Workbook.PICTURE_TYPE_JPEG);\r
-\r
- //create sheet\r
- Sheet sheet = wb.createSheet();\r
-\r
- //create drawing\r
- Drawing drawing = sheet.createDrawingPatriarch();\r
-\r
- //add a picture shape\r
- ClientAnchor anchor = helper.createClientAnchor();\r
- anchor.setCol1(1);\r
- anchor.setRow1(1);\r
- Picture pict = drawing.createPicture(anchor, pictureIdx);\r
-\r
- //auto-size picture\r
- pict.resize(2);\r
-\r
- //save workbook\r
- String file = "picture.xls";\r
- if(wb instanceof XSSFWorkbook) file += "x";\r
- FileOutputStream fileOut = new FileOutputStream(file);\r
- wb.write(fileOut);\r
- fileOut.close();\r
-\r
- }\r
-}\r
+/* ====================================================================
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+==================================================================== */
+
+package org.apache.poi.xssf.usermodel.examples;
+
+import org.apache.poi.xssf.usermodel.*;
+import org.apache.poi.ss.usermodel.*;
+import org.apache.poi.util.IOUtils;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+
+/**
+ * Demonstrates how to insert pictures in a SpreadsheetML document
+ *
+ * @author Yegor Kozlov
+ */
+public class WorkingWithPictures {
+ public static void main(String[] args) throws IOException {
+
+ //create a new workbook
+ Workbook wb = new XSSFWorkbook(); //or new HSSFWorkbook();
+ CreationHelper helper = wb.getCreationHelper();
+
+ //add a picture in this workbook.
+ InputStream is = new FileInputStream(args[0]);
+ byte[] bytes = IOUtils.toByteArray(is);
+ is.close();
+ int pictureIdx = wb.addPicture(bytes, Workbook.PICTURE_TYPE_JPEG);
+
+ //create sheet
+ Sheet sheet = wb.createSheet();
+
+ //create drawing
+ Drawing drawing = sheet.createDrawingPatriarch();
+
+ //add a picture shape
+ ClientAnchor anchor = helper.createClientAnchor();
+ anchor.setCol1(1);
+ anchor.setRow1(1);
+ Picture pict = drawing.createPicture(anchor, pictureIdx);
+
+ //auto-size picture
+ pict.resize(2);
+
+ //save workbook
+ String file = "picture.xls";
+ if(wb instanceof XSSFWorkbook) file += "x";
+ FileOutputStream fileOut = new FileOutputStream(file);
+ wb.write(fileOut);
+ fileOut.close();
+ }
+}
-/* ====================================================================\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
-\r
-import java.io.FileOutputStream;\r
-\r
-/**\r
- * Demonstrates how to work with rich text\r
- */\r
-public class WorkingWithRichText {\r
-\r
- public static void main(String[] args) throws Exception {\r
-\r
- XSSFWorkbook wb = new XSSFWorkbook(); //or new HSSFWorkbook();\r
-\r
- XSSFSheet sheet = wb.createSheet();\r
- XSSFRow row = sheet.createRow((short) 2);\r
-\r
- XSSFCell cell = row.createCell(1);\r
- XSSFRichTextString rt = new XSSFRichTextString("The quick brown fox");\r
-\r
- XSSFFont font1 = wb.createFont();\r
- font1.setBold(true);\r
- font1.setColor(new XSSFColor(new java.awt.Color(255, 0, 0)));\r
- rt.applyFont(0, 10, font1);\r
-\r
- XSSFFont font2 = wb.createFont();\r
- font2.setItalic(true);\r
- font2.setUnderline(XSSFFont.U_DOUBLE);\r
- font2.setColor(new XSSFColor(new java.awt.Color(0, 255, 0)));\r
- rt.applyFont(10, 19, font2);\r
-\r
- XSSFFont font3 = wb.createFont();\r
- font3.setColor(new XSSFColor(new java.awt.Color(0, 0, 255)));\r
- rt.append(" Jumped over the lazy dog", font3);\r
-\r
- cell.setCellValue(rt);\r
-\r
- // Write the output to a file\r
- FileOutputStream fileOut = new FileOutputStream("xssf-richtext.xlsx");\r
- wb.write(fileOut);\r
- fileOut.close();\r
- }\r
-\r
-}\r
+/* ====================================================================
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+==================================================================== */
+
+package org.apache.poi.xssf.usermodel.examples;
+
+import org.apache.poi.xssf.usermodel.*;
+
+import java.io.FileOutputStream;
+
+/**
+ * Demonstrates how to work with rich text
+ */
+public class WorkingWithRichText {
+
+ public static void main(String[] args) throws Exception {
+
+ XSSFWorkbook wb = new XSSFWorkbook(); //or new HSSFWorkbook();
+
+ XSSFSheet sheet = wb.createSheet();
+ XSSFRow row = sheet.createRow((short) 2);
+
+ XSSFCell cell = row.createCell(1);
+ XSSFRichTextString rt = new XSSFRichTextString("The quick brown fox");
+
+ XSSFFont font1 = wb.createFont();
+ font1.setBold(true);
+ font1.setColor(new XSSFColor(new java.awt.Color(255, 0, 0)));
+ rt.applyFont(0, 10, font1);
+
+ XSSFFont font2 = wb.createFont();
+ font2.setItalic(true);
+ font2.setUnderline(XSSFFont.U_DOUBLE);
+ font2.setColor(new XSSFColor(new java.awt.Color(0, 255, 0)));
+ rt.applyFont(10, 19, font2);
+
+ XSSFFont font3 = wb.createFont();
+ font3.setColor(new XSSFColor(new java.awt.Color(0, 0, 255)));
+ rt.append(" Jumped over the lazy dog", font3);
+
+ cell.setCellValue(rt);
+
+ // Write the output to a file
+ FileOutputStream fileOut = new FileOutputStream("xssf-richtext.xlsx");
+ wb.write(fileOut);
+ fileOut.close();
+ }
+}