From 5a5ab51f2bdd87347800dc2fe8724220a638212b Mon Sep 17 00:00:00 2001
From: Yegor Kozlov
Date: Tue, 11 Nov 2008 18:57:50 +0000
Subject: [PATCH] docs cleanup and improvements
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@713108 13f79535-47bb-0310-9956-ffa450edef68
---
src/documentation/content/xdocs/index.xml | 4 +-
.../content/xdocs/spreadsheet/converting.xml | 8 +-
.../content/xdocs/spreadsheet/how-to.xml | 2 +-
.../content/xdocs/spreadsheet/quick-guide.xml | 451 ++++++++++--------
.../eventusermodel/examples/FromHowTo.java | 2 +-
5 files changed, 254 insertions(+), 213 deletions(-)
diff --git a/src/documentation/content/xdocs/index.xml b/src/documentation/content/xdocs/index.xml
index 6deed531a..cbf9d3b3a 100644
--- a/src/documentation/content/xdocs/index.xml
+++ b/src/documentation/content/xdocs/index.xml
@@ -31,14 +31,14 @@
- POI 3.5.1 beta 1, and Office Open XML Support (2008-07-18)
+ POI 3.5 beta 3, and Office Open XML Support (2008-07-18)
We are currently working to support the new Office Open XML
file formats, such as XLSX and PPTX, which were introduced in
Office 2007.
Development for this is in a svn branch, but we are please to
announce our first preview release containing this support.
Users interested in the OOXML support should download the
- POI 3.5.1 beta 1
+ POI 3.5 beta 3
the source and binaries from their
local mirror.
People interested should also follow the
diff --git a/src/documentation/content/xdocs/spreadsheet/converting.xml b/src/documentation/content/xdocs/spreadsheet/converting.xml
index 8d0e966e3..c8c891bd9 100644
--- a/src/documentation/content/xdocs/spreadsheet/converting.xml
+++ b/src/documentation/content/xdocs/spreadsheet/converting.xml
@@ -121,12 +121,12 @@ HSSFFont f2 = wb.createFont();
// Set font 1 to 12 point type, blue and bold
f.setFontHeightInPoints((short) 12);
-f.setColor( (short)0xc );
+f.setColor( HSSFColor.RED.index );
f.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
// Set font 2 to 10 point type, red and bold
f2.setFontHeightInPoints((short) 10);
-f2.setColor( (short)HSSFFont.COLOR_RED );
+f2.setColor( HSSFFont.RED.index );
f2.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
// Set cell style and formatting
@@ -183,12 +183,12 @@ for(int i=0; i contents of a cell
diff --git a/src/documentation/content/xdocs/spreadsheet/quick-guide.xml b/src/documentation/content/xdocs/spreadsheet/quick-guide.xml
index 7523f500d..461a5f207 100644
--- a/src/documentation/content/xdocs/spreadsheet/quick-guide.xml
+++ b/src/documentation/content/xdocs/spreadsheet/quick-guide.xml
@@ -112,14 +112,14 @@
// Create a row and put some cells in it. Rows are 0 based.
Row row = sheet.createRow((short)0);
// Create a cell and put a value in it.
- Cell cell = row.createCell((short)0);
+ Cell cell = row.createCell(0);
cell.setCellValue(1);
// Or do it on one line.
- row.createCell((short)1).setCellValue(1.2);
- row.createCell((short)2).setCellValue(
+ row.createCell(1).setCellValue(1.2);
+ row.createCell(2).setCellValue(
createHelper.createRichTextString("This is a string"));
- row.createCell((short)3).setCellValue(true);
+ row.createCell(3).setCellValue(true);
// Write the output to a file
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
@@ -136,11 +136,11 @@
Sheet sheet = wb.createSheet("new sheet");
// 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
// as a date.
- Cell cell = row.createCell((short)0);
+ Cell cell = row.createCell(0);
cell.setCellValue(new Date());
// we style the second cell as a date (and time). It is important to
@@ -149,10 +149,15 @@
CellStyle cellStyle = wb.createCellStyle();
cellStyle.setDataFormat(
createHelper.createDataFormat().getFormat("m/d/yy h:mm"));
- cell = row.createCell((short)1);
+ cell = row.createCell(1);
cell.setCellValue(new Date());
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
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
@@ -162,14 +167,15 @@
Working with different types of cells
- HSSFWorkbook wb = new HSSFWorkbook();
- HSSFSheet sheet1 = wb.createSheet("new sheet");
+ Workbook wb = new HSSFWorkbook();
+ Sheet sheet1 = wb.createSheet("new sheet");
sheet1.setZoom(3,4); // 75 percent magnification
- FileOutputStream fileOut = new FileOutputStream("workbook.xls");
- wb.write(fileOut);
- fileOut.close();
@@ -803,7 +817,7 @@ Examples:
Split panes are created with the following call:
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.
- HSSFWorkbook wb = new HSSFWorkbook();
- HSSFSheet sheet1 = wb.createSheet("new sheet");
- HSSFSheet sheet2 = wb.createSheet("second sheet");
- HSSFSheet sheet3 = wb.createSheet("third sheet");
- HSSFSheet sheet4 = wb.createSheet("fourth sheet");
+ Workbook wb = new HSSFWorkbook();
+ 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 );
@@ -831,7 +845,7 @@ Examples:
// 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, HSSFSheet.PANE_LOWER_LEFT );
+ sheet4.createSplitPane( 2000, 2000, 0, 0, Sheet.PANE_LOWER_LEFT );
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
@@ -855,9 +869,9 @@ Examples:
To stop the columns from repeating pass in -1 as the start and end rows.
- HSSFWorkbook wb = new HSSFWorkbook();
- HSSFSheet sheet1 = wb.createSheet("new sheet");
- HSSFSheet sheet2 = wb.createSheet("second sheet");
+ Workbook wb = new HSSFWorkbook();
+ Sheet sheet1 = wb.createSheet("new sheet");
+ Sheet sheet2 = wb.createSheet("second sheet");
// Set the columns to repeat from column 0 to 2 on the first sheet
wb.setRepeatingRowsAndColumns(0,0,2,-1,-1);
@@ -875,10 +889,10 @@ Examples:
Example is for headers but applies directly to footers.
- HSSFWorkbook wb = new HSSFWorkbook();
- HSSFSheet sheet = wb.createSheet("new sheet");
+ Workbook wb = new HSSFWorkbook();
+ Sheet sheet = wb.createSheet("new sheet");
- HSSFHeader header = sheet.getHeader();
+ Header header = sheet.getHeader();
header.setCenter("Center Header");
header.setLeft("Left Header");
header.setRight(HSSFHeader.font("Stencil-Normal", "Italic") +
@@ -1103,8 +1117,8 @@ Examples:
using the POI API. Here's how:
- HSSFWorkbook wb = new HSSFWorkbook();
- HSSFSheet sheet1 = wb.createSheet("new sheet");
+ Workbook wb = new HSSFWorkbook();
+ Sheet sheet1 = wb.createSheet("new sheet");
sheet1.groupRow( 5, 14 );
sheet1.groupRow( 7, 14 );
@@ -1148,7 +1162,8 @@ Examples:
It should be noted that any existing drawings may be erased
once you add a image to a sheet.
-
+
+
HSSF:
// Create the drawing patriarch. This is the top level container for
// 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 ));
HSSFClientAnchor preferredSize = picture.getPreferredSize();
picture.setAnchor(preferredSize);
+
+
XSSF:
+
+ //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();
- 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.
Reading images from a workbook:
- HSSFWorkbook wb;
-
- List lst = wb.getAllPictures();
+
+ List lst = workbook.getAllPictures();
for (Iterator it = lst.iterator(); it.hasNext(); ) {
- HSSFPictureData pict = (HSSFPictureData)it.next();
+ PictureData pict = (PictureData)it.next();
String ext = pict.suggestFileExtension();
byte[] data = pict.getData();
if (ext.equals("jpeg")){
@@ -1440,7 +1481,7 @@ Examples:
Adjust column width to fit the contents
- HSSFSheet sheet = workbook.getSheetAt(0);
+ Sheet sheet = workbook.getSheetAt(0);
sheet.autoSizeColumn((short)0); //adjust width of the first column
sheet.autoSizeColumn((short)1); //adjust width of the second column
@@ -1448,17 +1489,16 @@ Examples:
To calculate column width HSSFSheet.autoSizeColumn uses Java2D classes
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
- set the following system property: java.awt.headless=true
- (either via -Djava.awt.headless=true startup parameter or via System.setProperty("java.awt.headless", "true")).
+ set the following system property: java.awt.headless=true .
How to read hyperlinks
- HSSFSheet sheet = workbook.getSheetAt(0);
+ Sheet sheet = workbook.getSheetAt(0);
- HSSFCell cell = sheet.getRow(0).getCell((short)0);
- HSSFHyperlink link = cell.getHyperlink();
+ Cell cell = sheet.getRow(0).getCell((short)0);
+ Hyperlink link = cell.getHyperlink();
if(link != null){
System.out.println(link.getAddress());
}
@@ -1466,23 +1506,24 @@ Examples:
How to create hyperlinks
- HSSFWorkbook wb = new HSSFWorkbook();
+ Workbook wb = new XSSFWorkbook(); //or new HSSFWorkbook();
+ CreationHelper createHelper = wb.getCreationHelper();
//cell style for hyperlinks
//by default hypelrinks are blue and underlined
- HSSFCellStyle hlink_style = wb.createCellStyle();
- HSSFFont hlink_font = wb.createFont();
- hlink_font.setUnderline(HSSFFont.U_SINGLE);
- hlink_font.setColor(HSSFColor.BLUE.index);
+ CellStyle hlink_style = wb.createCellStyle();
+ Font hlink_font = wb.createFont();
+ hlink_font.setUnderline(Font.U_SINGLE);
+ hlink_font.setColor(IndexedColors.BLUE.getIndex());
hlink_style.setFont(hlink_font);
- HSSFCell cell;
- HSSFSheet sheet = wb.createSheet("Hyperlinks");
-
+ Cell cell;
+ Sheet sheet = wb.createSheet("Hyperlinks");
//URL
cell = sheet.createRow(0).createCell((short)0);
cell.setCellValue("URL Link");
- HSSFHyperlink link = new HSSFHyperlink(HSSFHyperlink.LINK_URL);
+
+ Hyperlink link = createHelper.createHyperlink(Hyperlink.LINK_URL);
link.setAddress("http://poi.apache.org/");
cell.setHyperlink(link);
cell.setCellStyle(hlink_style);
@@ -1490,7 +1531,7 @@ Examples:
//link to a file in the current directory
cell = sheet.createRow(1).createCell((short)0);
cell.setCellValue("File Link");
- link = new HSSFHyperlink(HSSFHyperlink.LINK_FILE);
+ link = createHelper.createHyperlink(Hyperlink.LINK_FILE);
link.setAddress("link1.xls");
cell.setHyperlink(link);
cell.setCellStyle(hlink_style);
@@ -1498,7 +1539,7 @@ Examples:
//e-mail link
cell = sheet.createRow(2).createCell((short)0);
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
link.setAddress("mailto:poi@apache.org?subject=Hyperlinks");
cell.setHyperlink(link);
@@ -1507,20 +1548,20 @@ Examples:
//link to a place in this workbook
//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");
cell = sheet.createRow(3).createCell((short)0);
cell.setCellValue("Worksheet Link");
- link = new HSSFHyperlink(HSSFHyperlink.LINK_DOCUMENT);
- link.setAddress("'Target Sheet'!A1");
- cell.setHyperlink(link);
+ Hyperlink link2 = createHelper.createHyperlink(Hyperlink.LINK_DOCUMENT);
+ link2.setAddress("'Target Sheet'!A1");
+ cell.setHyperlink(link2);
cell.setCellStyle(hlink_style);
- FileOutputStream out = new FileOutputStream("hssf-links.xls");
+ FileOutputStream out = new FileOutputStream("hyperinks.xlsx");
wb.write(out);
out.close();
-
+
diff --git a/src/examples/src/org/apache/poi/xssf/eventusermodel/examples/FromHowTo.java b/src/examples/src/org/apache/poi/xssf/eventusermodel/examples/FromHowTo.java
index 2c145bfe1..e4fe3fe16 100644
--- a/src/examples/src/org/apache/poi/xssf/eventusermodel/examples/FromHowTo.java
+++ b/src/examples/src/org/apache/poi/xssf/eventusermodel/examples/FromHowTo.java
@@ -32,7 +32,7 @@ import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.helpers.XMLReaderFactory;
/**
- * Various things from the how-to documentation
+ * XSSF and SAX (Event API)
*/
public class FromHowTo {
public void processOneSheet(String filename) throws Exception {