changed CRLF to LF in src/examples/.../xssf/...
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@781837 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
3280064978
commit
bf6b4439b3
@ -1,73 +1,72 @@
|
||||
/* ====================================================================
|
||||
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.openxmlformats.schemas.spreadsheetml.x2006.main.STHorizontalAlignment;
|
||||
|
||||
import java.io.FileOutputStream;
|
||||
|
||||
/**
|
||||
* Shows how various alignment options work.
|
||||
*/
|
||||
public class AligningCells {
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
/* ====================================================================
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
@ -1,252 +1,254 @@
|
||||
/* ====================================================================
|
||||
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.CellReference;
|
||||
import org.apache.poi.ss.usermodel.IndexedColors;
|
||||
import org.apache.poi.ss.usermodel.DateUtil;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.zip.ZipFile;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipOutputStream;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* 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);
|
||||
|
||||
for (Enumeration en = zip.entries(); en.hasMoreElements(); ) {
|
||||
ZipEntry ze = (ZipEntry)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 Writer out;
|
||||
private int rownum;
|
||||
|
||||
public SpreadsheetWriter(Writer out){
|
||||
this.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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
/* ====================================================================
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,227 +1,228 @@
|
||||
/* ====================================================================
|
||||
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;
|
||||
}
|
||||
}
|
||||
/* ====================================================================
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
@ -21,7 +21,6 @@ import java.io.FileOutputStream;
|
||||
import java.util.Date;
|
||||
|
||||
import org.apache.poi.ss.usermodel.*;
|
||||
import org.apache.poi.xssf.usermodel.XSSFRichTextString;
|
||||
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
||||
|
||||
/**
|
||||
|
@ -1,44 +1,44 @@
|
||||
/* ====================================================================
|
||||
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());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
/* ====================================================================
|
||||
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());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,50 +1,49 @@
|
||||
/* ====================================================================
|
||||
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 org.apache.poi.hssf.util.Region;
|
||||
|
||||
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();
|
||||
|
||||
}
|
||||
}
|
||||
/* ====================================================================
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
@ -21,14 +21,13 @@ import java.io.FileOutputStream;
|
||||
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);
|
||||
|
@ -1,50 +1,50 @@
|
||||
/* ====================================================================
|
||||
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();
|
||||
|
||||
}
|
||||
}
|
||||
/* ====================================================================
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
@ -1,58 +1,58 @@
|
||||
/* ====================================================================
|
||||
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();
|
||||
|
||||
}
|
||||
}
|
||||
/* ====================================================================
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
@ -1,100 +1,101 @@
|
||||
/* ====================================================================
|
||||
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();
|
||||
}
|
||||
}
|
||||
/* ====================================================================
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
@ -1,70 +1,69 @@
|
||||
/* ====================================================================
|
||||
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 org.apache.poi.hssf.usermodel.HSSFWorkbook;
|
||||
|
||||
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();
|
||||
|
||||
}
|
||||
}
|
||||
/* ====================================================================
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
@ -1,61 +1,61 @@
|
||||
/* ====================================================================
|
||||
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();
|
||||
}
|
||||
|
||||
}
|
||||
/* ====================================================================
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user