docs cleanup and improvements
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@713108 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
aa8ee7d78c
commit
5a5ab51f2b
@ -31,14 +31,14 @@
|
|||||||
</header>
|
</header>
|
||||||
|
|
||||||
<body>
|
<body>
|
||||||
<section><title>POI 3.5.1 beta 1, and Office Open XML Support (2008-07-18)</title>
|
<section><title>POI 3.5 beta 3, and Office Open XML Support (2008-07-18)</title>
|
||||||
<p>We are currently working to support the new Office Open XML
|
<p>We are currently working to support the new Office Open XML
|
||||||
file formats, such as XLSX and PPTX, which were introduced in
|
file formats, such as XLSX and PPTX, which were introduced in
|
||||||
Office 2007.</p>
|
Office 2007.</p>
|
||||||
<p>Development for this is in a svn branch, but we are please to
|
<p>Development for this is in a svn branch, but we are please to
|
||||||
announce our first preview release containing this support.
|
announce our first preview release containing this support.
|
||||||
Users interested in the OOXML support should download the
|
Users interested in the OOXML support should download the
|
||||||
<link href="http://www.apache.org/dyn/closer.cgi/poi/dev/">POI 3.5.1 beta 1</link>
|
<link href="http://www.apache.org/dyn/closer.cgi/poi/dev/">POI 3.5 beta 3</link>
|
||||||
the source and binaries from their
|
the source and binaries from their
|
||||||
<link href="http://www.apache.org/dyn/closer.cgi/poi/dev/">local mirror</link>.
|
<link href="http://www.apache.org/dyn/closer.cgi/poi/dev/">local mirror</link>.
|
||||||
People interested should also follow the
|
People interested should also follow the
|
||||||
|
@ -121,12 +121,12 @@ HSSFFont f2 = wb.createFont();
|
|||||||
|
|
||||||
// Set font 1 to 12 point type, blue and bold
|
// Set font 1 to 12 point type, blue and bold
|
||||||
f.setFontHeightInPoints((short) 12);
|
f.setFontHeightInPoints((short) 12);
|
||||||
f.setColor( (short)0xc );
|
f.setColor( HSSFColor.RED.index );
|
||||||
f.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
|
f.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
|
||||||
|
|
||||||
// Set font 2 to 10 point type, red and bold
|
// Set font 2 to 10 point type, red and bold
|
||||||
f2.setFontHeightInPoints((short) 10);
|
f2.setFontHeightInPoints((short) 10);
|
||||||
f2.setColor( (short)HSSFFont.COLOR_RED );
|
f2.setColor( HSSFFont.RED.index );
|
||||||
f2.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
|
f2.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
|
||||||
|
|
||||||
// Set cell style and formatting
|
// Set cell style and formatting
|
||||||
@ -183,12 +183,12 @@ for(int i=0; i<wbs.length; i++) {
|
|||||||
|
|
||||||
// Set font 1 to 12 point type, blue and bold
|
// Set font 1 to 12 point type, blue and bold
|
||||||
f.setFontHeightInPoints((short) 12);
|
f.setFontHeightInPoints((short) 12);
|
||||||
f.setColor( (short)0xc );
|
f.setColor( IndexedColors.RED.getIndex() );
|
||||||
f.setBoldweight(Font.BOLDWEIGHT_BOLD);
|
f.setBoldweight(Font.BOLDWEIGHT_BOLD);
|
||||||
|
|
||||||
// Set font 2 to 10 point type, red and bold
|
// Set font 2 to 10 point type, red and bold
|
||||||
f2.setFontHeightInPoints((short) 10);
|
f2.setFontHeightInPoints((short) 10);
|
||||||
f2.setColor( (short)Font.COLOR_RED );
|
f2.setColor( IndexedColors.RED.getIndex() );
|
||||||
f2.setBoldweight(Font.BOLDWEIGHT_BOLD);
|
f2.setBoldweight(Font.BOLDWEIGHT_BOLD);
|
||||||
|
|
||||||
// Set cell style and formatting
|
// Set cell style and formatting
|
||||||
|
@ -597,7 +597,7 @@ public class ExampleEventUserModel {
|
|||||||
// Do now, as characters() may be called more than once
|
// Do now, as characters() may be called more than once
|
||||||
if(nextIsString) {
|
if(nextIsString) {
|
||||||
int idx = Integer.parseInt(lastContents);
|
int idx = Integer.parseInt(lastContents);
|
||||||
lastContents = sst.getSharedStringAt(idx);
|
lastContents = new XSSFRichTextString(sst.getEntryAt(idx)).toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
// v => contents of a cell
|
// v => contents of a cell
|
||||||
|
@ -112,14 +112,14 @@
|
|||||||
// Create a row and put some cells in it. Rows are 0 based.
|
// Create a row and put some cells in it. Rows are 0 based.
|
||||||
Row row = sheet.createRow((short)0);
|
Row row = sheet.createRow((short)0);
|
||||||
// Create a cell and put a value in it.
|
// Create a cell and put a value in it.
|
||||||
Cell cell = row.createCell((short)0);
|
Cell cell = row.createCell(0);
|
||||||
cell.setCellValue(1);
|
cell.setCellValue(1);
|
||||||
|
|
||||||
// Or do it on one line.
|
// Or do it on one line.
|
||||||
row.createCell((short)1).setCellValue(1.2);
|
row.createCell(1).setCellValue(1.2);
|
||||||
row.createCell((short)2).setCellValue(
|
row.createCell(2).setCellValue(
|
||||||
createHelper.createRichTextString("This is a string"));
|
createHelper.createRichTextString("This is a string"));
|
||||||
row.createCell((short)3).setCellValue(true);
|
row.createCell(3).setCellValue(true);
|
||||||
|
|
||||||
// Write the output to a file
|
// Write the output to a file
|
||||||
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
|
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
|
||||||
@ -136,11 +136,11 @@
|
|||||||
Sheet sheet = wb.createSheet("new sheet");
|
Sheet sheet = wb.createSheet("new sheet");
|
||||||
|
|
||||||
// Create a row and put some cells in it. Rows are 0 based.
|
// Create a row and put some cells in it. Rows are 0 based.
|
||||||
Row row = sheet.createRow((short)0);
|
Row row = sheet.createRow(0);
|
||||||
|
|
||||||
// Create a cell and put a date value in it. The first cell is not styled
|
// Create a cell and put a date value in it. The first cell is not styled
|
||||||
// as a date.
|
// as a date.
|
||||||
Cell cell = row.createCell((short)0);
|
Cell cell = row.createCell(0);
|
||||||
cell.setCellValue(new Date());
|
cell.setCellValue(new Date());
|
||||||
|
|
||||||
// we style the second cell as a date (and time). It is important to
|
// we style the second cell as a date (and time). It is important to
|
||||||
@ -149,10 +149,15 @@
|
|||||||
CellStyle cellStyle = wb.createCellStyle();
|
CellStyle cellStyle = wb.createCellStyle();
|
||||||
cellStyle.setDataFormat(
|
cellStyle.setDataFormat(
|
||||||
createHelper.createDataFormat().getFormat("m/d/yy h:mm"));
|
createHelper.createDataFormat().getFormat("m/d/yy h:mm"));
|
||||||
cell = row.createCell((short)1);
|
cell = row.createCell(1);
|
||||||
cell.setCellValue(new Date());
|
cell.setCellValue(new Date());
|
||||||
cell.setCellStyle(cellStyle);
|
cell.setCellStyle(cellStyle);
|
||||||
|
|
||||||
|
//you can also set date as java.util.Calendar
|
||||||
|
cell = row.createCell(2);
|
||||||
|
cell.setCellValue(Calendar.getInstance());
|
||||||
|
cell.setCellStyle(cellStyle);
|
||||||
|
|
||||||
// Write the output to a file
|
// Write the output to a file
|
||||||
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
|
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
|
||||||
wb.write(fileOut);
|
wb.write(fileOut);
|
||||||
@ -162,14 +167,15 @@
|
|||||||
<anchor id="CellTypes"/>
|
<anchor id="CellTypes"/>
|
||||||
<section><title>Working with different types of cells</title>
|
<section><title>Working with different types of cells</title>
|
||||||
<source>
|
<source>
|
||||||
HSSFWorkbook wb = new HSSFWorkbook();
|
Workbook wb = new HSSFWorkbook();
|
||||||
HSSFSheet sheet = wb.createSheet("new sheet");
|
Sheet sheet = wb.createSheet("new sheet");
|
||||||
HSSFRow row = sheet.createRow((short)2);
|
Row row = sheet.createRow((short)2);
|
||||||
row.createCell((short) 0).setCellValue(1.1);
|
row.createCell(0).setCellValue(1.1);
|
||||||
row.createCell((short) 1).setCellValue(new Date());
|
row.createCell(1).setCellValue(new Date());
|
||||||
row.createCell((short) 2).setCellValue("a string");
|
row.createCell(2).setCellValue(Calendar.getInstance());
|
||||||
row.createCell((short) 3).setCellValue(true);
|
row.createCell(3).setCellValue("a string");
|
||||||
row.createCell((short) 4).setCellType(HSSFCell.CELL_TYPE_ERROR);
|
row.createCell(4).setCellValue(true);
|
||||||
|
row.createCell(5).setCellType(HSSFCell.CELL_TYPE_ERROR);
|
||||||
|
|
||||||
// Write the output to a file
|
// Write the output to a file
|
||||||
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
|
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
|
||||||
@ -180,22 +186,23 @@
|
|||||||
<anchor id="Alignment"/>
|
<anchor id="Alignment"/>
|
||||||
<section><title>Demonstrates various alignment options</title>
|
<section><title>Demonstrates various alignment options</title>
|
||||||
<source>
|
<source>
|
||||||
public static void main(String[] args)
|
public static void main(String[] args) throws Exception {
|
||||||
throws IOException
|
Workbook wb = new XSSFWorkbook(); //or new HSSFWorkbook();
|
||||||
{
|
|
||||||
HSSFWorkbook wb = new HSSFWorkbook();
|
Sheet sheet = wb.createSheet();
|
||||||
HSSFSheet sheet = wb.createSheet("new sheet");
|
Row row = sheet.createRow((short) 2);
|
||||||
HSSFRow row = sheet.createRow((short) 2);
|
row.setHeightInPoints(30);
|
||||||
createCell(wb, row, (short) 0, HSSFCellStyle.ALIGN_CENTER);
|
|
||||||
createCell(wb, row, (short) 1, HSSFCellStyle.ALIGN_CENTER_SELECTION);
|
createCell(wb, row, (short) 0, XSSFCellStyle.ALIGN_CENTER, XSSFCellStyle.VERTICAL_BOTTOM);
|
||||||
createCell(wb, row, (short) 2, HSSFCellStyle.ALIGN_FILL);
|
createCell(wb, row, (short) 1, XSSFCellStyle.ALIGN_CENTER_SELECTION, XSSFCellStyle.VERTICAL_BOTTOM);
|
||||||
createCell(wb, row, (short) 3, HSSFCellStyle.ALIGN_GENERAL);
|
createCell(wb, row, (short) 2, XSSFCellStyle.ALIGN_FILL, XSSFCellStyle.VERTICAL_CENTER);
|
||||||
createCell(wb, row, (short) 4, HSSFCellStyle.ALIGN_JUSTIFY);
|
createCell(wb, row, (short) 3, XSSFCellStyle.ALIGN_GENERAL, XSSFCellStyle.VERTICAL_CENTER);
|
||||||
createCell(wb, row, (short) 5, HSSFCellStyle.ALIGN_LEFT);
|
createCell(wb, row, (short) 4, XSSFCellStyle.ALIGN_JUSTIFY, XSSFCellStyle.VERTICAL_JUSTIFY);
|
||||||
createCell(wb, row, (short) 6, HSSFCellStyle.ALIGN_RIGHT);
|
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
|
// Write the output to a file
|
||||||
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
|
FileOutputStream fileOut = new FileOutputStream("xssf-align.xlsx");
|
||||||
wb.write(fileOut);
|
wb.write(fileOut);
|
||||||
fileOut.close();
|
fileOut.close();
|
||||||
|
|
||||||
@ -204,17 +211,17 @@
|
|||||||
/**
|
/**
|
||||||
* Creates a cell and aligns it a certain way.
|
* Creates a cell and aligns it a certain way.
|
||||||
*
|
*
|
||||||
* @param wb the workbook
|
* @param wb the workbook
|
||||||
* @param row the row to create the cell in
|
* @param row the row to create the cell in
|
||||||
* @param column the column number to create the cell in
|
* @param column the column number to create the cell in
|
||||||
* @param align the alignment for the cell.
|
* @param halign the horizontal alignment for the cell.
|
||||||
*/
|
*/
|
||||||
private static void createCell(HSSFWorkbook wb, HSSFRow row, short column, short align)
|
private static void createCell(Workbook wb, Row row, short column, short halign, short valign) {
|
||||||
{
|
Cell cell = row.createCell(column);
|
||||||
HSSFCell cell = row.createCell(column);
|
cell.setCellValue(new XSSFRichTextString("Align It"));
|
||||||
cell.setCellValue("Align It");
|
CellStyle cellStyle = wb.createCellStyle();
|
||||||
HSSFCellStyle cellStyle = wb.createCellStyle();
|
cellStyle.setAlignment(halign);
|
||||||
cellStyle.setAlignment(align);
|
cellStyle.setVerticalAlignment(valign);
|
||||||
cell.setCellStyle(cellStyle);
|
cell.setCellStyle(cellStyle);
|
||||||
}
|
}
|
||||||
</source>
|
</source>
|
||||||
@ -222,26 +229,26 @@
|
|||||||
<anchor id="Borders"/>
|
<anchor id="Borders"/>
|
||||||
<section><title>Working with borders</title>
|
<section><title>Working with borders</title>
|
||||||
<source>
|
<source>
|
||||||
HSSFWorkbook wb = new HSSFWorkbook();
|
Workbook wb = new HSSFWorkbook();
|
||||||
HSSFSheet sheet = wb.createSheet("new sheet");
|
Sheet sheet = wb.createSheet("new sheet");
|
||||||
|
|
||||||
// Create a row and put some cells in it. Rows are 0 based.
|
// Create a row and put some cells in it. Rows are 0 based.
|
||||||
HSSFRow row = sheet.createRow((short) 1);
|
Row row = sheet.createRow(1);
|
||||||
|
|
||||||
// Create a cell and put a value in it.
|
// Create a cell and put a value in it.
|
||||||
HSSFCell cell = row.createCell((short) 1);
|
Cell cell = row.createCell(1);
|
||||||
cell.setCellValue(4);
|
cell.setCellValue(4);
|
||||||
|
|
||||||
// Style the cell with borders all around.
|
// Style the cell with borders all around.
|
||||||
HSSFCellStyle style = wb.createCellStyle();
|
CellStyle style = wb.createCellStyle();
|
||||||
style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
|
style.setBorderBottom(CellStyle.BORDER_THIN);
|
||||||
style.setBottomBorderColor(HSSFColor.BLACK.index);
|
style.setBottomBorderColor(IndexedColors.BLACK.getIndex());
|
||||||
style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
|
style.setBorderLeft(CellStyle.BORDER_THIN);
|
||||||
style.setLeftBorderColor(HSSFColor.GREEN.index);
|
style.setLeftBorderColor(IndexedColors.GREEN.getIndex());
|
||||||
style.setBorderRight(HSSFCellStyle.BORDER_THIN);
|
style.setBorderRight(CellStyle.BORDER_THIN);
|
||||||
style.setRightBorderColor(HSSFColor.BLUE.index);
|
style.setRightBorderColor(IndexedColors.BLUE.getIndex());
|
||||||
style.setBorderTop(HSSFCellStyle.BORDER_MEDIUM_DASHED);
|
style.setBorderTop(CellStyle.BORDER_MEDIUM_DASHED);
|
||||||
style.setTopBorderColor(HSSFColor.BLACK.index);
|
style.setTopBorderColor(IndexedColors.BLACK.getIndex());
|
||||||
cell.setCellStyle(style);
|
cell.setCellStyle(style);
|
||||||
|
|
||||||
// Write the output to a file
|
// Write the output to a file
|
||||||
@ -327,24 +334,24 @@ for (Row row : sheet1) {
|
|||||||
System.out.print(" - ");
|
System.out.print(" - ");
|
||||||
|
|
||||||
switch(cell.getCellType()) {
|
switch(cell.getCellType()) {
|
||||||
case Cell.CELL_TYPE_STRING:
|
case Cell.CELL_TYPE_STRING:
|
||||||
System.out.println(cell.getRichStringCellValue().getString());
|
System.out.println(cell.getRichStringCellValue().getString());
|
||||||
break;
|
break;
|
||||||
case Cell.CELL_TYPE_NUMERIC:
|
case Cell.CELL_TYPE_NUMERIC:
|
||||||
if(DateUtil.isCellDateFormatted(cell)) {
|
if(DateUtil.isCellDateFormatted(cell)) {
|
||||||
System.out.println(cell.getDateCellValue());
|
System.out.println(cell.getDateCellValue());
|
||||||
} else {
|
} else {
|
||||||
System.out.println(cell.getNumericCellValue());
|
System.out.println(cell.getNumericCellValue());
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case Cell.CELL_TYPE_BOOLEAN:
|
case Cell.CELL_TYPE_BOOLEAN:
|
||||||
System.out.println(cell.getBooleanCellValue());
|
System.out.println(cell.getBooleanCellValue());
|
||||||
break;
|
break;
|
||||||
case Cell.CELL_TYPE_FORMULA:
|
case Cell.CELL_TYPE_FORMULA:
|
||||||
System.out.println(cell.getCellFormula());
|
System.out.println(cell.getCellFormula());
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
System.out.println();
|
System.out.println();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -372,24 +379,24 @@ for (Row row : sheet1) {
|
|||||||
<anchor id="FillsAndFrills"/>
|
<anchor id="FillsAndFrills"/>
|
||||||
<section><title>Fills and colors</title>
|
<section><title>Fills and colors</title>
|
||||||
<source>
|
<source>
|
||||||
HSSFWorkbook wb = new HSSFWorkbook();
|
Workbook wb = new XSSFWorkbook();
|
||||||
HSSFSheet sheet = wb.createSheet("new sheet");
|
Sheet sheet = wb.createSheet("new sheet");
|
||||||
|
|
||||||
// Create a row and put some cells in it. Rows are 0 based.
|
// Create a row and put some cells in it. Rows are 0 based.
|
||||||
HSSFRow row = sheet.createRow((short) 1);
|
Row row = sheet.createRow((short) 1);
|
||||||
|
|
||||||
// Aqua background
|
// Aqua background
|
||||||
HSSFCellStyle style = wb.createCellStyle();
|
CellStyle style = wb.createCellStyle();
|
||||||
style.setFillBackgroundColor(HSSFColor.AQUA.index);
|
style.setFillBackgroundColor(IndexedColors.AQUA.getIndex());
|
||||||
style.setFillPattern(HSSFCellStyle.BIG_SPOTS);
|
style.setFillPattern(CellStyle.BIG_SPOTS);
|
||||||
HSSFCell cell = row.createCell((short) 1);
|
Cell cell = row.createCell((short) 1);
|
||||||
cell.setCellValue("X");
|
cell.setCellValue("X");
|
||||||
cell.setCellStyle(style);
|
cell.setCellStyle(style);
|
||||||
|
|
||||||
// Orange "foreground", foreground being the fill foreground not the font color.
|
// Orange "foreground", foreground being the fill foreground not the font color.
|
||||||
style = wb.createCellStyle();
|
style = wb.createCellStyle();
|
||||||
style.setFillForegroundColor(HSSFColor.ORANGE.index);
|
style.setFillForegroundColor(IndexedColors.ORANGE.getIndex());
|
||||||
style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
|
style.setFillPattern(CellStyle.SOLID_FOREGROUND);
|
||||||
cell = row.createCell((short) 2);
|
cell = row.createCell((short) 2);
|
||||||
cell.setCellValue("X");
|
cell.setCellValue("X");
|
||||||
cell.setCellStyle(style);
|
cell.setCellStyle(style);
|
||||||
@ -403,14 +410,19 @@ for (Row row : sheet1) {
|
|||||||
<anchor id="MergedCells"/>
|
<anchor id="MergedCells"/>
|
||||||
<section><title>Merging cells</title>
|
<section><title>Merging cells</title>
|
||||||
<source>
|
<source>
|
||||||
HSSFWorkbook wb = new HSSFWorkbook();
|
Workbook wb = new HSSFWorkbook();
|
||||||
HSSFSheet sheet = wb.createSheet("new sheet");
|
Sheet sheet = wb.createSheet("new sheet");
|
||||||
|
|
||||||
HSSFRow row = sheet.createRow((short) 1);
|
Row row = sheet.createRow((short) 1);
|
||||||
HSSFCell cell = row.createCell((short) 1);
|
Cell cell = row.createCell((short) 1);
|
||||||
cell.setCellValue("This is a test of merging");
|
cell.setCellValue("This is a test of merging");
|
||||||
|
|
||||||
sheet.addMergedRegion(new Region(1,(short)1,1,(short)2));
|
sheet.addMergedRegion(new CellRangeAddress(
|
||||||
|
1, //first row (0-based)
|
||||||
|
1, //last row (0-based)
|
||||||
|
1, //first column (0-based)
|
||||||
|
2 //last column (0-based)
|
||||||
|
));
|
||||||
|
|
||||||
// Write the output to a file
|
// Write the output to a file
|
||||||
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
|
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
|
||||||
@ -421,25 +433,25 @@ for (Row row : sheet1) {
|
|||||||
<anchor id="WorkingWithFonts"/>
|
<anchor id="WorkingWithFonts"/>
|
||||||
<section><title>Working with fonts</title>
|
<section><title>Working with fonts</title>
|
||||||
<source>
|
<source>
|
||||||
HSSFWorkbook wb = new HSSFWorkbook();
|
Workbook wb = new HSSFWorkbook();
|
||||||
HSSFSheet sheet = wb.createSheet("new sheet");
|
Sheet sheet = wb.createSheet("new sheet");
|
||||||
|
|
||||||
// Create a row and put some cells in it. Rows are 0 based.
|
// Create a row and put some cells in it. Rows are 0 based.
|
||||||
HSSFRow row = sheet.createRow((short) 1);
|
Row row = sheet.createRow(1);
|
||||||
|
|
||||||
// Create a new font and alter it.
|
// Create a new font and alter it.
|
||||||
HSSFFont font = wb.createFont();
|
Font font = wb.createFont();
|
||||||
font.setFontHeightInPoints((short)24);
|
font.setFontHeightInPoints((short)24);
|
||||||
font.setFontName("Courier New");
|
font.setFontName("Courier New");
|
||||||
font.setItalic(true);
|
font.setItalic(true);
|
||||||
font.setStrikeout(true);
|
font.setStrikeout(true);
|
||||||
|
|
||||||
// Fonts are set into a style so create a new one to use.
|
// Fonts are set into a style so create a new one to use.
|
||||||
HSSFCellStyle style = wb.createCellStyle();
|
CellStyle style = wb.createCellStyle();
|
||||||
style.setFont(font);
|
style.setFont(font);
|
||||||
|
|
||||||
// Create a cell and put a value in it.
|
// Create a cell and put a value in it.
|
||||||
HSSFCell cell = row.createCell((short) 1);
|
Cell cell = row.createCell(1);
|
||||||
cell.setCellValue("This is a test of fonts");
|
cell.setCellValue("This is a test of fonts");
|
||||||
cell.setCellStyle(style);
|
cell.setCellStyle(style);
|
||||||
|
|
||||||
@ -457,12 +469,12 @@ Examples:
|
|||||||
<p><strong>Wrong:</strong></p>
|
<p><strong>Wrong:</strong></p>
|
||||||
<source>
|
<source>
|
||||||
for (int i = 0; i < 10000; i++) {
|
for (int i = 0; i < 10000; i++) {
|
||||||
HSSFRow row = sheet.createRow(i);
|
Row row = sheet.createRow(i);
|
||||||
HSSFCell cell = row.createCell((short) 0);
|
Cell cell = row.createCell((short) 0);
|
||||||
|
|
||||||
HSSFCellStyle style = workbook.createCellStyle();
|
CellStyle style = workbook.createCellStyle();
|
||||||
HSSFFont font = workbook.createFont();
|
Font font = workbook.createFont();
|
||||||
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
|
font.setBoldweight(Font.BOLDWEIGHT_BOLD);
|
||||||
style.setFont(font);
|
style.setFont(font);
|
||||||
cell.setCellStyle(style);
|
cell.setCellStyle(style);
|
||||||
}
|
}
|
||||||
@ -470,13 +482,13 @@ Examples:
|
|||||||
<p><strong>Correct:</strong></p>
|
<p><strong>Correct:</strong></p>
|
||||||
<source>
|
<source>
|
||||||
|
|
||||||
HSSFCellStyle style = workbook.createCellStyle();
|
CellStyle style = workbook.createCellStyle();
|
||||||
HSSFFont font = workbook.createFont();
|
Font font = workbook.createFont();
|
||||||
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
|
font.setBoldweight(Font.BOLDWEIGHT_BOLD);
|
||||||
style.setFont(font);
|
style.setFont(font);
|
||||||
for (int i = 0; i < 10000; i++) {
|
for (int i = 0; i < 10000; i++) {
|
||||||
HSSFRow row = sheet.createRow(i);
|
Row row = sheet.createRow(i);
|
||||||
HSSFCell cell = row.createCell((short) 0);
|
Cell cell = row.createCell((short) 0);
|
||||||
cell.setCellStyle(style);
|
cell.setCellStyle(style);
|
||||||
}
|
}
|
||||||
</source>
|
</source>
|
||||||
@ -484,6 +496,7 @@ Examples:
|
|||||||
</section>
|
</section>
|
||||||
<anchor id="CustomColors"/>
|
<anchor id="CustomColors"/>
|
||||||
<section><title>Custom colors</title>
|
<section><title>Custom colors</title>
|
||||||
|
<p><strong>HSSF:</strong></p>
|
||||||
<source>
|
<source>
|
||||||
HSSFWorkbook wb = new HSSFWorkbook();
|
HSSFWorkbook wb = new HSSFWorkbook();
|
||||||
HSSFSheet sheet = wb.createSheet();
|
HSSFSheet sheet = wb.createSheet();
|
||||||
@ -535,6 +548,18 @@ Examples:
|
|||||||
wb.write(out);
|
wb.write(out);
|
||||||
out.close();
|
out.close();
|
||||||
</source>
|
</source>
|
||||||
|
<p><strong>XSSF:</strong></p>
|
||||||
|
<source>
|
||||||
|
XSSFWorkbook wb = new XSSFWorkbook();
|
||||||
|
XSSFSheet sheet = wb.createSheet();
|
||||||
|
XSSFRow row = sheet.createRow(0);
|
||||||
|
XSSFCell cell = row.createCell( 0);
|
||||||
|
cell.setCellValue("custom XSSF colors");
|
||||||
|
|
||||||
|
XSSFCellStyle style1 = wb.createCellStyle();
|
||||||
|
style1.setFillForegroundColor(new XSSFColor(new java.awt.Color(128, 0, 128)));
|
||||||
|
style1.setFillPattern(CellStyle.SOLID_FOREGROUND);
|
||||||
|
</source>
|
||||||
</section>
|
</section>
|
||||||
<anchor id="ReadWriteWorkbook"/>
|
<anchor id="ReadWriteWorkbook"/>
|
||||||
<section><title>Reading and Rewriting Workbooks</title>
|
<section><title>Reading and Rewriting Workbooks</title>
|
||||||
@ -545,9 +570,9 @@ Examples:
|
|||||||
Workbook wb = WorkbookFactory.create(inp);
|
Workbook wb = WorkbookFactory.create(inp);
|
||||||
Sheet sheet = wb.getSheetAt(0);
|
Sheet sheet = wb.getSheetAt(0);
|
||||||
Row row = sheet.getRow(2);
|
Row row = sheet.getRow(2);
|
||||||
Cell cell = row.getCell((short)3);
|
Cell cell = row.getCell(3);
|
||||||
if (cell == null)
|
if (cell == null)
|
||||||
cell = row.createCell((short)3);
|
cell = row.createCell(3);
|
||||||
cell.setCellType(Cell.CELL_TYPE_STRING);
|
cell.setCellType(Cell.CELL_TYPE_STRING);
|
||||||
cell.setCellValue("a test");
|
cell.setCellValue("a test");
|
||||||
|
|
||||||
@ -560,41 +585,38 @@ Examples:
|
|||||||
<anchor id="NewLinesInCells"/>
|
<anchor id="NewLinesInCells"/>
|
||||||
<section><title>Using newlines in cells</title>
|
<section><title>Using newlines in cells</title>
|
||||||
<source>
|
<source>
|
||||||
HSSFWorkbook wb = new HSSFWorkbook();
|
Workbook wb = new XSSFWorkbook(); //or new HSSFWorkbook();
|
||||||
HSSFSheet s = wb.createSheet();
|
Sheet sheet = wb.createSheet();
|
||||||
HSSFRow r = null;
|
|
||||||
HSSFCell c = null;
|
|
||||||
HSSFCellStyle cs = wb.createCellStyle();
|
|
||||||
HSSFFont f = wb.createFont();
|
|
||||||
HSSFFont f2 = wb.createFont();
|
|
||||||
|
|
||||||
cs = wb.createCellStyle();
|
Row row = sheet.createRow(2);
|
||||||
|
Cell cell = row.createCell(2);
|
||||||
|
cell.setCellValue("Use \n with word wrap on to create a new line");
|
||||||
|
|
||||||
cs.setFont( f2 );
|
//to enable newlines you need set a cell styles with wrap=true
|
||||||
//Word Wrap MUST be turned on
|
CellStyle cs = wb.createCellStyle();
|
||||||
cs.setWrapText( true );
|
cs.setWrapText(true);
|
||||||
|
cell.setCellStyle(cs);
|
||||||
|
|
||||||
r = s.createRow( (short) 2 );
|
//increase row height to accomodate two lines of text
|
||||||
r.setHeight( (short) 0x349 );
|
row.setHeightInPoints((2*sheet.getDefaultRowHeightInPoints()));
|
||||||
c = r.createCell( (short) 2 );
|
|
||||||
c.setCellType( HSSFCell.CELL_TYPE_STRING );
|
|
||||||
c.setCellValue( "Use \n with word wrap on to create a new line" );
|
|
||||||
c.setCellStyle( cs );
|
|
||||||
s.setColumnWidth( (short) 2, (short) ( ( 50 * 8 ) / ( (double) 1 / 20 ) ) );
|
|
||||||
|
|
||||||
FileOutputStream fileOut = new FileOutputStream( "workbook.xls" );
|
//adjust column width to fit the content
|
||||||
wb.write( fileOut );
|
sheet.autoSizeColumn((short)2);
|
||||||
fileOut.close();</source>
|
|
||||||
|
FileOutputStream fileOut = new FileOutputStream("ooxml-newlines.xlsx");
|
||||||
|
wb.write(fileOut);
|
||||||
|
fileOut.close();
|
||||||
|
</source>
|
||||||
</section>
|
</section>
|
||||||
<anchor id="DataFormats"/>
|
<anchor id="DataFormats"/>
|
||||||
<section><title>Data Formats</title>
|
<section><title>Data Formats</title>
|
||||||
<source>
|
<source>
|
||||||
HSSFWorkbook wb = new HSSFWorkbook();
|
Workbook wb = new HSSFWorkbook();
|
||||||
HSSFSheet sheet = wb.createSheet("format sheet");
|
Sheet sheet = wb.createSheet("format sheet");
|
||||||
HSSFCellStyle style;
|
CellStyle style;
|
||||||
HSSFDataFormat format = wb.createDataFormat();
|
DataFormat format = wb.createDataFormat();
|
||||||
HSSFRow row;
|
Row row;
|
||||||
HSSFCell cell;
|
Cell cell;
|
||||||
short rowNum = 0;
|
short rowNum = 0;
|
||||||
short colNum = 0;
|
short colNum = 0;
|
||||||
|
|
||||||
@ -620,9 +642,9 @@ Examples:
|
|||||||
<anchor id="FitTo"/>
|
<anchor id="FitTo"/>
|
||||||
<section><title>Fit Sheet to One Page</title>
|
<section><title>Fit Sheet to One Page</title>
|
||||||
<source>
|
<source>
|
||||||
HSSFWorkbook wb = new HSSFWorkbook();
|
Workbook wb = new HSSFWorkbook();
|
||||||
HSSFSheet sheet = wb.createSheet("format sheet");
|
Sheet sheet = wb.createSheet("format sheet");
|
||||||
HSSFPrintSetup ps = sheet.getPrintSetup();
|
PrintSetup ps = sheet.getPrintSetup();
|
||||||
|
|
||||||
sheet.setAutobreaks(true);
|
sheet.setAutobreaks(true);
|
||||||
|
|
||||||
@ -640,20 +662,23 @@ Examples:
|
|||||||
<anchor id="PrintArea2"/>
|
<anchor id="PrintArea2"/>
|
||||||
<section><title>Set Print Area</title>
|
<section><title>Set Print Area</title>
|
||||||
<source>
|
<source>
|
||||||
HSSFWorkbook wb = new HSSFWorkbook();
|
Workbook wb = new HSSFWorkbook();
|
||||||
HSSFSheet sheet = wb.createSheet("Sheet1");
|
Sheet sheet = wb.createSheet("Sheet1");
|
||||||
wb.setPrintArea(0, "$A$1:$C$2");
|
|
||||||
//sets the print area for the first sheet
|
//sets the print area for the first sheet
|
||||||
|
wb.setPrintArea(0, "$A$1:$C$2");
|
||||||
|
|
||||||
//Alternatively:
|
//Alternatively:
|
||||||
//wb.setPrintArea(0, 0, 1, 0, 0) is equivalent to using the name reference (See the JavaDocs for more details)
|
wb.setPrintArea(
|
||||||
|
0, //sheet index
|
||||||
// Create various cells and rows for spreadsheet.
|
0, //start column
|
||||||
|
1, //end column
|
||||||
|
0, //start row
|
||||||
|
0 //end row
|
||||||
|
);
|
||||||
|
|
||||||
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
|
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
|
||||||
wb.write(fileOut);
|
wb.write(fileOut);
|
||||||
fileOut.close();
|
fileOut.close();
|
||||||
|
|
||||||
|
|
||||||
</source>
|
</source>
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
@ -728,32 +753,24 @@ Examples:
|
|||||||
<anchor id="ShiftRows"/>
|
<anchor id="ShiftRows"/>
|
||||||
<section><title>Shift rows up or down on a sheet</title>
|
<section><title>Shift rows up or down on a sheet</title>
|
||||||
<source>
|
<source>
|
||||||
HSSFWorkbook wb = new HSSFWorkbook();
|
Workbook wb = new HSSFWorkbook();
|
||||||
HSSFSheet sheet = wb.createSheet("row sheet");
|
Sheet sheet = wb.createSheet("row sheet");
|
||||||
|
|
||||||
// Create various cells and rows for spreadsheet.
|
// Create various cells and rows for spreadsheet.
|
||||||
|
|
||||||
// Shift rows 6 - 11 on the spreadsheet to the top (rows 0 - 5)
|
// Shift rows 6 - 11 on the spreadsheet to the top (rows 0 - 5)
|
||||||
sheet.shiftRows(5, 10, -5);
|
sheet.shiftRows(5, 10, -5);
|
||||||
|
|
||||||
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
|
|
||||||
wb.write(fileOut);
|
|
||||||
fileOut.close();
|
|
||||||
</source>
|
</source>
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
<anchor id="SelectSheet"/>
|
<anchor id="SelectSheet"/>
|
||||||
<section><title>Set a sheet as selected</title>
|
<section><title>Set a sheet as selected</title>
|
||||||
<source>
|
<source>
|
||||||
HSSFWorkbook wb = new HSSFWorkbook();
|
Workbook wb = new HSSFWorkbook();
|
||||||
HSSFSheet sheet = wb.createSheet("row sheet");
|
Sheet sheet = wb.createSheet("row sheet");
|
||||||
sheet.setSelected(true);
|
sheet.setSelected(true);
|
||||||
|
|
||||||
// Create various cells and rows for spreadsheet.
|
|
||||||
|
|
||||||
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
|
|
||||||
wb.write(fileOut);
|
|
||||||
fileOut.close();
|
|
||||||
</source>
|
</source>
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
@ -765,12 +782,9 @@ Examples:
|
|||||||
4 for the denominator.
|
4 for the denominator.
|
||||||
</p>
|
</p>
|
||||||
<source>
|
<source>
|
||||||
HSSFWorkbook wb = new HSSFWorkbook();
|
Workbook wb = new HSSFWorkbook();
|
||||||
HSSFSheet sheet1 = wb.createSheet("new sheet");
|
Sheet sheet1 = wb.createSheet("new sheet");
|
||||||
sheet1.setZoom(3,4); // 75 percent magnification
|
sheet1.setZoom(3,4); // 75 percent magnification
|
||||||
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
|
|
||||||
wb.write(fileOut);
|
|
||||||
fileOut.close();
|
|
||||||
</source>
|
</source>
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
@ -803,7 +817,7 @@ Examples:
|
|||||||
Split panes are created with the following call:
|
Split panes are created with the following call:
|
||||||
</p>
|
</p>
|
||||||
<p>
|
<p>
|
||||||
sheet2.createSplitPane( 2000, 2000, 0, 0, HSSFSheet.PANE_LOWER_LEFT );
|
sheet2.createSplitPane( 2000, 2000, 0, 0, Sheet.PANE_LOWER_LEFT );
|
||||||
</p>
|
</p>
|
||||||
<p>
|
<p>
|
||||||
|
|
||||||
@ -814,15 +828,15 @@ Examples:
|
|||||||
</p>
|
</p>
|
||||||
<p>
|
<p>
|
||||||
The last parameter indicates which pane currently has
|
The last parameter indicates which pane currently has
|
||||||
the focus. This will be one of HSSFSheet.PANE_LOWER_LEFT,
|
the focus. This will be one of Sheet.PANE_LOWER_LEFT,
|
||||||
PANE_LOWER_RIGHT, PANE_UPPER_RIGHT or PANE_UPPER_LEFT.
|
PANE_LOWER_RIGHT, PANE_UPPER_RIGHT or PANE_UPPER_LEFT.
|
||||||
</p>
|
</p>
|
||||||
<source>
|
<source>
|
||||||
HSSFWorkbook wb = new HSSFWorkbook();
|
Workbook wb = new HSSFWorkbook();
|
||||||
HSSFSheet sheet1 = wb.createSheet("new sheet");
|
Sheet sheet1 = wb.createSheet("new sheet");
|
||||||
HSSFSheet sheet2 = wb.createSheet("second sheet");
|
Sheet sheet2 = wb.createSheet("second sheet");
|
||||||
HSSFSheet sheet3 = wb.createSheet("third sheet");
|
Sheet sheet3 = wb.createSheet("third sheet");
|
||||||
HSSFSheet sheet4 = wb.createSheet("fourth sheet");
|
Sheet sheet4 = wb.createSheet("fourth sheet");
|
||||||
|
|
||||||
// Freeze just one row
|
// Freeze just one row
|
||||||
sheet1.createFreezePane( 0, 1, 0, 1 );
|
sheet1.createFreezePane( 0, 1, 0, 1 );
|
||||||
@ -831,7 +845,7 @@ Examples:
|
|||||||
// Freeze the columns and rows (forget about scrolling position of the lower right quadrant).
|
// Freeze the columns and rows (forget about scrolling position of the lower right quadrant).
|
||||||
sheet3.createFreezePane( 2, 2 );
|
sheet3.createFreezePane( 2, 2 );
|
||||||
// Create a split with the lower left side being the active quadrant
|
// Create a split with the lower left side being the active quadrant
|
||||||
sheet4.createSplitPane( 2000, 2000, 0, 0, HSSFSheet.PANE_LOWER_LEFT );
|
sheet4.createSplitPane( 2000, 2000, 0, 0, Sheet.PANE_LOWER_LEFT );
|
||||||
|
|
||||||
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
|
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
|
||||||
wb.write(fileOut);
|
wb.write(fileOut);
|
||||||
@ -855,9 +869,9 @@ Examples:
|
|||||||
To stop the columns from repeating pass in -1 as the start and end rows.
|
To stop the columns from repeating pass in -1 as the start and end rows.
|
||||||
</p>
|
</p>
|
||||||
<source>
|
<source>
|
||||||
HSSFWorkbook wb = new HSSFWorkbook();
|
Workbook wb = new HSSFWorkbook();
|
||||||
HSSFSheet sheet1 = wb.createSheet("new sheet");
|
Sheet sheet1 = wb.createSheet("new sheet");
|
||||||
HSSFSheet sheet2 = wb.createSheet("second sheet");
|
Sheet sheet2 = wb.createSheet("second sheet");
|
||||||
|
|
||||||
// Set the columns to repeat from column 0 to 2 on the first sheet
|
// Set the columns to repeat from column 0 to 2 on the first sheet
|
||||||
wb.setRepeatingRowsAndColumns(0,0,2,-1,-1);
|
wb.setRepeatingRowsAndColumns(0,0,2,-1,-1);
|
||||||
@ -875,10 +889,10 @@ Examples:
|
|||||||
Example is for headers but applies directly to footers.
|
Example is for headers but applies directly to footers.
|
||||||
</p>
|
</p>
|
||||||
<source>
|
<source>
|
||||||
HSSFWorkbook wb = new HSSFWorkbook();
|
Workbook wb = new HSSFWorkbook();
|
||||||
HSSFSheet sheet = wb.createSheet("new sheet");
|
Sheet sheet = wb.createSheet("new sheet");
|
||||||
|
|
||||||
HSSFHeader header = sheet.getHeader();
|
Header header = sheet.getHeader();
|
||||||
header.setCenter("Center Header");
|
header.setCenter("Center Header");
|
||||||
header.setLeft("Left Header");
|
header.setLeft("Left Header");
|
||||||
header.setRight(HSSFHeader.font("Stencil-Normal", "Italic") +
|
header.setRight(HSSFHeader.font("Stencil-Normal", "Italic") +
|
||||||
@ -1103,8 +1117,8 @@ Examples:
|
|||||||
using the POI API. Here's how:
|
using the POI API. Here's how:
|
||||||
</p>
|
</p>
|
||||||
<source>
|
<source>
|
||||||
HSSFWorkbook wb = new HSSFWorkbook();
|
Workbook wb = new HSSFWorkbook();
|
||||||
HSSFSheet sheet1 = wb.createSheet("new sheet");
|
Sheet sheet1 = wb.createSheet("new sheet");
|
||||||
|
|
||||||
sheet1.groupRow( 5, 14 );
|
sheet1.groupRow( 5, 14 );
|
||||||
sheet1.groupRow( 7, 14 );
|
sheet1.groupRow( 7, 14 );
|
||||||
@ -1148,7 +1162,8 @@ Examples:
|
|||||||
<p>
|
<p>
|
||||||
It should be noted that any existing drawings may be erased
|
It should be noted that any existing drawings may be erased
|
||||||
once you add a image to a sheet.
|
once you add a image to a sheet.
|
||||||
</p>
|
</p>
|
||||||
|
<p><strong>HSSF:</strong></p>
|
||||||
<source>
|
<source>
|
||||||
// Create the drawing patriarch. This is the top level container for
|
// Create the drawing patriarch. This is the top level container for
|
||||||
// all shapes. This will clear out any existing shapes for that sheet.
|
// all shapes. This will clear out any existing shapes for that sheet.
|
||||||
@ -1173,18 +1188,44 @@ Examples:
|
|||||||
HSSFPicture picture = patriarch.createPicture(new HSSFClientAnchor(), loadPicture( "src/resources/logos/logoKarmokar4.png", wb ));
|
HSSFPicture picture = patriarch.createPicture(new HSSFClientAnchor(), loadPicture( "src/resources/logos/logoKarmokar4.png", wb ));
|
||||||
HSSFClientAnchor preferredSize = picture.getPreferredSize();
|
HSSFClientAnchor preferredSize = picture.getPreferredSize();
|
||||||
picture.setAnchor(preferredSize);
|
picture.setAnchor(preferredSize);
|
||||||
|
</source>
|
||||||
|
<p><strong>XSSF:</strong></p>
|
||||||
|
<source>
|
||||||
|
//create a new workbook
|
||||||
|
XSSFWorkbook wb = new XSSFWorkbook();
|
||||||
|
|
||||||
|
//add a picture in this workbook.
|
||||||
|
InputStream is = new FileInputStream("lilies.jpg");
|
||||||
|
int pictureIdx = wb.addPicture(is, XSSFWorkbook.PICTURE_TYPE_JPEG);
|
||||||
|
is.close();
|
||||||
|
|
||||||
|
//create sheet
|
||||||
|
XSSFSheet sheet = wb.createSheet();
|
||||||
|
|
||||||
|
//create drawing
|
||||||
|
XSSFDrawing drawing = sheet.createDrawingPatriarch();
|
||||||
|
|
||||||
|
//add a picture shape
|
||||||
|
XSSFPicture pict = drawing.createPicture(new XSSFClientAnchor(), pictureIdx);
|
||||||
|
|
||||||
|
//auto-size picture
|
||||||
|
pict.resize();
|
||||||
|
|
||||||
|
//save workbook
|
||||||
|
FileOutputStream fileOut = new FileOutputStream("xssf-picture.xlsx");
|
||||||
|
wb.write(fileOut);
|
||||||
|
fileOut.close();
|
||||||
</source>
|
</source>
|
||||||
<warning>
|
<warning>
|
||||||
HSSFPicture.resize() works only for JPEG and PNG. Other formats are not yet supported.
|
HSSFPicture.resize() and XSSFPicture.resize() work only for JPEG and PNG. Other formats are not yet supported.
|
||||||
</warning>
|
</warning>
|
||||||
|
|
||||||
<p>Reading images from a workbook:</p>
|
<p>Reading images from a workbook:</p>
|
||||||
<source>
|
<source>
|
||||||
HSSFWorkbook wb;
|
|
||||||
|
List lst = workbook.getAllPictures();
|
||||||
List lst = wb.getAllPictures();
|
|
||||||
for (Iterator it = lst.iterator(); it.hasNext(); ) {
|
for (Iterator it = lst.iterator(); it.hasNext(); ) {
|
||||||
HSSFPictureData pict = (HSSFPictureData)it.next();
|
PictureData pict = (PictureData)it.next();
|
||||||
String ext = pict.suggestFileExtension();
|
String ext = pict.suggestFileExtension();
|
||||||
byte[] data = pict.getData();
|
byte[] data = pict.getData();
|
||||||
if (ext.equals("jpeg")){
|
if (ext.equals("jpeg")){
|
||||||
@ -1440,7 +1481,7 @@ Examples:
|
|||||||
<anchor id="Autofit"/>
|
<anchor id="Autofit"/>
|
||||||
<section><title>Adjust column width to fit the contents</title>
|
<section><title>Adjust column width to fit the contents</title>
|
||||||
<source>
|
<source>
|
||||||
HSSFSheet sheet = workbook.getSheetAt(0);
|
Sheet sheet = workbook.getSheetAt(0);
|
||||||
sheet.autoSizeColumn((short)0); //adjust width of the first column
|
sheet.autoSizeColumn((short)0); //adjust width of the first column
|
||||||
sheet.autoSizeColumn((short)1); //adjust width of the second column
|
sheet.autoSizeColumn((short)1); //adjust width of the second column
|
||||||
</source>
|
</source>
|
||||||
@ -1448,17 +1489,16 @@ Examples:
|
|||||||
To calculate column width HSSFSheet.autoSizeColumn uses Java2D classes
|
To calculate column width HSSFSheet.autoSizeColumn uses Java2D classes
|
||||||
that throw exception if graphical environment is not available. In case if graphical environment
|
that throw exception if graphical environment is not available. In case if graphical environment
|
||||||
is not available, you must tell Java that you are running in headless mode and
|
is not available, you must tell Java that you are running in headless mode and
|
||||||
set the following system property: <code> java.awt.headless=true </code>
|
set the following system property: <code> java.awt.headless=true </code>.
|
||||||
(either via <code>-Djava.awt.headless=true</code> startup parameter or via <code>System.setProperty("java.awt.headless", "true")</code>).
|
|
||||||
</warning>
|
</warning>
|
||||||
</section>
|
</section>
|
||||||
<anchor id="Hyperlinks"/>
|
<anchor id="Hyperlinks"/>
|
||||||
<section><title>How to read hyperlinks</title>
|
<section><title>How to read hyperlinks</title>
|
||||||
<source>
|
<source>
|
||||||
HSSFSheet sheet = workbook.getSheetAt(0);
|
Sheet sheet = workbook.getSheetAt(0);
|
||||||
|
|
||||||
HSSFCell cell = sheet.getRow(0).getCell((short)0);
|
Cell cell = sheet.getRow(0).getCell((short)0);
|
||||||
HSSFHyperlink link = cell.getHyperlink();
|
Hyperlink link = cell.getHyperlink();
|
||||||
if(link != null){
|
if(link != null){
|
||||||
System.out.println(link.getAddress());
|
System.out.println(link.getAddress());
|
||||||
}
|
}
|
||||||
@ -1466,23 +1506,24 @@ Examples:
|
|||||||
</section>
|
</section>
|
||||||
<section><title>How to create hyperlinks</title>
|
<section><title>How to create hyperlinks</title>
|
||||||
<source>
|
<source>
|
||||||
HSSFWorkbook wb = new HSSFWorkbook();
|
Workbook wb = new XSSFWorkbook(); //or new HSSFWorkbook();
|
||||||
|
CreationHelper createHelper = wb.getCreationHelper();
|
||||||
|
|
||||||
//cell style for hyperlinks
|
//cell style for hyperlinks
|
||||||
//by default hypelrinks are blue and underlined
|
//by default hypelrinks are blue and underlined
|
||||||
HSSFCellStyle hlink_style = wb.createCellStyle();
|
CellStyle hlink_style = wb.createCellStyle();
|
||||||
HSSFFont hlink_font = wb.createFont();
|
Font hlink_font = wb.createFont();
|
||||||
hlink_font.setUnderline(HSSFFont.U_SINGLE);
|
hlink_font.setUnderline(Font.U_SINGLE);
|
||||||
hlink_font.setColor(HSSFColor.BLUE.index);
|
hlink_font.setColor(IndexedColors.BLUE.getIndex());
|
||||||
hlink_style.setFont(hlink_font);
|
hlink_style.setFont(hlink_font);
|
||||||
|
|
||||||
HSSFCell cell;
|
Cell cell;
|
||||||
HSSFSheet sheet = wb.createSheet("Hyperlinks");
|
Sheet sheet = wb.createSheet("Hyperlinks");
|
||||||
|
|
||||||
//URL
|
//URL
|
||||||
cell = sheet.createRow(0).createCell((short)0);
|
cell = sheet.createRow(0).createCell((short)0);
|
||||||
cell.setCellValue("URL Link");
|
cell.setCellValue("URL Link");
|
||||||
HSSFHyperlink link = new HSSFHyperlink(HSSFHyperlink.LINK_URL);
|
|
||||||
|
Hyperlink link = createHelper.createHyperlink(Hyperlink.LINK_URL);
|
||||||
link.setAddress("http://poi.apache.org/");
|
link.setAddress("http://poi.apache.org/");
|
||||||
cell.setHyperlink(link);
|
cell.setHyperlink(link);
|
||||||
cell.setCellStyle(hlink_style);
|
cell.setCellStyle(hlink_style);
|
||||||
@ -1490,7 +1531,7 @@ Examples:
|
|||||||
//link to a file in the current directory
|
//link to a file in the current directory
|
||||||
cell = sheet.createRow(1).createCell((short)0);
|
cell = sheet.createRow(1).createCell((short)0);
|
||||||
cell.setCellValue("File Link");
|
cell.setCellValue("File Link");
|
||||||
link = new HSSFHyperlink(HSSFHyperlink.LINK_FILE);
|
link = createHelper.createHyperlink(Hyperlink.LINK_FILE);
|
||||||
link.setAddress("link1.xls");
|
link.setAddress("link1.xls");
|
||||||
cell.setHyperlink(link);
|
cell.setHyperlink(link);
|
||||||
cell.setCellStyle(hlink_style);
|
cell.setCellStyle(hlink_style);
|
||||||
@ -1498,7 +1539,7 @@ Examples:
|
|||||||
//e-mail link
|
//e-mail link
|
||||||
cell = sheet.createRow(2).createCell((short)0);
|
cell = sheet.createRow(2).createCell((short)0);
|
||||||
cell.setCellValue("Email Link");
|
cell.setCellValue("Email Link");
|
||||||
link = new HSSFHyperlink(HSSFHyperlink.LINK_EMAIL);
|
link = createHelper.createHyperlink(Hyperlink.LINK_EMAIL);
|
||||||
//note, if subject contains white spaces, make sure they are url-encoded
|
//note, if subject contains white spaces, make sure they are url-encoded
|
||||||
link.setAddress("mailto:poi@apache.org?subject=Hyperlinks");
|
link.setAddress("mailto:poi@apache.org?subject=Hyperlinks");
|
||||||
cell.setHyperlink(link);
|
cell.setHyperlink(link);
|
||||||
@ -1507,20 +1548,20 @@ Examples:
|
|||||||
//link to a place in this workbook
|
//link to a place in this workbook
|
||||||
|
|
||||||
//create a target sheet and cell
|
//create a target sheet and cell
|
||||||
HSSFSheet sheet2 = wb.createSheet("Target Sheet");
|
Sheet sheet2 = wb.createSheet("Target Sheet");
|
||||||
sheet2.createRow(0).createCell((short)0).setCellValue("Target Cell");
|
sheet2.createRow(0).createCell((short)0).setCellValue("Target Cell");
|
||||||
|
|
||||||
cell = sheet.createRow(3).createCell((short)0);
|
cell = sheet.createRow(3).createCell((short)0);
|
||||||
cell.setCellValue("Worksheet Link");
|
cell.setCellValue("Worksheet Link");
|
||||||
link = new HSSFHyperlink(HSSFHyperlink.LINK_DOCUMENT);
|
Hyperlink link2 = createHelper.createHyperlink(Hyperlink.LINK_DOCUMENT);
|
||||||
link.setAddress("'Target Sheet'!A1");
|
link2.setAddress("'Target Sheet'!A1");
|
||||||
cell.setHyperlink(link);
|
cell.setHyperlink(link2);
|
||||||
cell.setCellStyle(hlink_style);
|
cell.setCellStyle(hlink_style);
|
||||||
|
|
||||||
FileOutputStream out = new FileOutputStream("hssf-links.xls");
|
FileOutputStream out = new FileOutputStream("hyperinks.xlsx");
|
||||||
wb.write(out);
|
wb.write(out);
|
||||||
out.close();
|
out.close();
|
||||||
</source>
|
</source>
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
</body>
|
</body>
|
||||||
|
@ -32,7 +32,7 @@ import org.xml.sax.helpers.DefaultHandler;
|
|||||||
import org.xml.sax.helpers.XMLReaderFactory;
|
import org.xml.sax.helpers.XMLReaderFactory;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Various things from the how-to documentation
|
* XSSF and SAX (Event API)
|
||||||
*/
|
*/
|
||||||
public class FromHowTo {
|
public class FromHowTo {
|
||||||
public void processOneSheet(String filename) throws Exception {
|
public void processOneSheet(String filename) throws Exception {
|
||||||
|
Loading…
Reference in New Issue
Block a user