mirror of https://github.com/apache/poi.git
Fix examples and updated documentation
git-svn-id: https://svn.apache.org/repos/asf/jakarta/poi/trunk@352556 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
4f8db56bbd
commit
7f94634b0c
|
@ -75,141 +75,142 @@
|
|||
</p>
|
||||
<p>Here is some example code (excerpted and adapted from
|
||||
org.apache.poi.hssf.dev.HSSF test class):</p>
|
||||
<source><![CDATA[ short rownum;
|
||||
<source><![CDATA[
|
||||
short rownum;
|
||||
|
||||
// create a new file
|
||||
FileOutputStream out = new FileOutputStream("workbook.xls");
|
||||
// create a new workbook
|
||||
HSSFWorkbook wb = new HSSFWorkbook();
|
||||
// create a new sheet
|
||||
HSSFSheet s = wb.createSheet();
|
||||
// declare a row object reference
|
||||
HSSFRow r = null;
|
||||
// declare a cell object reference
|
||||
HSSFCell c = null;
|
||||
// create 3 cell styles
|
||||
HSSFCellStyle cs = wb.createCellStyle();
|
||||
HSSFCellStyle cs2 = wb.createCellStyle();
|
||||
HSSFCellStyle cs3 = wb.createCellStyle();
|
||||
// create 2 fonts objects
|
||||
HSSFFont f = wb.createFont();
|
||||
HSSFFont f2 = wb.createFont();
|
||||
// create a new file
|
||||
FileOutputStream out = new FileOutputStream("workbook.xls");
|
||||
// create a new workbook
|
||||
HSSFWorkbook wb = new HSSFWorkbook();
|
||||
// create a new sheet
|
||||
HSSFSheet s = wb.createSheet();
|
||||
// declare a row object reference
|
||||
HSSFRow r = null;
|
||||
// declare a cell object reference
|
||||
HSSFCell c = null;
|
||||
// create 3 cell styles
|
||||
HSSFCellStyle cs = wb.createCellStyle();
|
||||
HSSFCellStyle cs2 = wb.createCellStyle();
|
||||
HSSFCellStyle cs3 = wb.createCellStyle();
|
||||
// create 2 fonts objects
|
||||
HSSFFont f = wb.createFont();
|
||||
HSSFFont f2 = wb.createFont();
|
||||
|
||||
//set font 1 to 12 point type
|
||||
f.setFontHeightInPoints((short) 12);
|
||||
//make it red
|
||||
f.setColor((short) HSSFCellStyle.RED);
|
||||
// make it bold
|
||||
//arial is the default font
|
||||
f.setBoldweight(f.BOLDWEIGHT_BOLD);
|
||||
//set font 1 to 12 point type
|
||||
f.setFontHeightInPoints((short) 12);
|
||||
//make it red
|
||||
f.setColor((short) HSSFColor.RED.index);
|
||||
// make it bold
|
||||
//arial is the default font
|
||||
f.setBoldweight(f.BOLDWEIGHT_BOLD);
|
||||
|
||||
//set font 2 to 10 point type
|
||||
f2.setFontHeightInPoints((short) 10);
|
||||
//make it the color at palette index 0xf (white)
|
||||
f2.setColor((short) HSSFCellStyle.WHITE);
|
||||
//make it bold
|
||||
f2.setBoldweight(f2.BOLDWEIGHT_BOLD);
|
||||
//set font 2 to 10 point type
|
||||
f2.setFontHeightInPoints((short) 10);
|
||||
//make it the color at palette index 0xf (white)
|
||||
f2.setColor((short) HSSFColor.WHITE.index);
|
||||
//make it bold
|
||||
f2.setBoldweight(f2.BOLDWEIGHT_BOLD);
|
||||
|
||||
//set cell stlye
|
||||
cs.setFont(f);
|
||||
//set the cell format see HSSFDataFromat for a full list
|
||||
cs.setDataFormat(HSSFDataFormat.getFormat("($#,##0_);[Red]($#,##0)"));
|
||||
//set cell stlye
|
||||
cs.setFont(f);
|
||||
//set the cell format see HSSFDataFromat for a full list
|
||||
cs.setDataFormat(HSSFDataFormat.getFormat("($#,##0_);[Red]($#,##0)"));
|
||||
|
||||
//set a thin border
|
||||
cs2.setBorderBottom(cs2.BORDER_THIN);
|
||||
//fill w fg fill color
|
||||
cs2.setFillPattern((short) HSSFCellStyle.SOLID_FOREGROUND);
|
||||
// set foreground fill to red
|
||||
cs2.setFillForegroundColor((short) HSSFCellStyle.RED);
|
||||
//set a thin border
|
||||
cs2.setBorderBottom(cs2.BORDER_THIN);
|
||||
//fill w fg fill color
|
||||
cs2.setFillPattern((short) HSSFCellStyle.SOLID_FOREGROUND);
|
||||
// set foreground fill to red
|
||||
cs2.setFillForegroundColor((short) HSSFColor.RED.index);
|
||||
|
||||
// set the font
|
||||
cs2.setFont(f2);
|
||||
// set the font
|
||||
cs2.setFont(f2);
|
||||
|
||||
// set the sheet name to HSSF Test
|
||||
wb.setSheetName(0, "HSSF Test");
|
||||
// create a sheet with 300 rows (0-299)
|
||||
for (rownum = (short) 0; rownum < 300; rownum++)
|
||||
// set the sheet name to HSSF Test
|
||||
wb.setSheetName(0, "HSSF Test");
|
||||
// create a sheet with 300 rows (0-299)
|
||||
for (rownum = (short) 0; rownum < 300; rownum++)
|
||||
{
|
||||
// create a row
|
||||
r = s.createRow(rownum);
|
||||
// on every other row
|
||||
if ((rownum % 2) == 0)
|
||||
{
|
||||
// make the row height bigger (in twips - 1/20 of a point)
|
||||
r.setHeight((short) 0x249);
|
||||
}
|
||||
|
||||
//r.setRowNum(( short ) rownum);
|
||||
// create 50 cells (0-49) (the += 2 becomes apparent later
|
||||
for (short cellnum = (short) 0; cellnum < 50; cellnum += 2)
|
||||
{
|
||||
// create a numeric cell
|
||||
c = r.createCell(cellnum);
|
||||
// do some goofy math to demonstrate decimals
|
||||
c.setCellValue(rownum * 10000 + cellnum
|
||||
+ (((double) rownum / 1000)
|
||||
+ ((double) cellnum / 10000)));
|
||||
|
||||
// on every other row
|
||||
if ((rownum % 2) == 0)
|
||||
{
|
||||
// create a row
|
||||
r = s.createRow(rownum);
|
||||
// on every other row
|
||||
if ((rownum % 2) == 0)
|
||||
{
|
||||
// make the row height bigger (in twips - 1/20 of a point)
|
||||
r.setHeight((short) 0x249);
|
||||
}
|
||||
|
||||
//r.setRowNum(( short ) rownum);
|
||||
// create 50 cells (0-49) (the += 2 becomes apparent later
|
||||
for (short cellnum = (short) 0; cellnum < 50; cellnum += 2)
|
||||
{
|
||||
// create a numeric cell
|
||||
c = r.createCell(cellnum);
|
||||
// do some goofy math to demonstrate decimals
|
||||
c.setCellValue(rownum * 10000 + cellnum
|
||||
+ (((double) rownum / 1000)
|
||||
+ ((double) cellnum / 10000)));
|
||||
|
||||
// on every other row
|
||||
if ((rownum % 2) == 0)
|
||||
{
|
||||
// set this cell to the first cell style we defined
|
||||
c.setCellStyle(cs);
|
||||
}
|
||||
|
||||
// create a string cell (see why += 2 in the
|
||||
c = r.createCell((short) (cellnum + 1));
|
||||
|
||||
// set the cell's string value to "TEST"
|
||||
c.setCellValue("TEST");
|
||||
// make this column a bit wider
|
||||
s.setColumnWidth((short) (cellnum + 1), (short) ((50 * 8) / ((double) 1 / 20)));
|
||||
|
||||
// on every other row
|
||||
if ((rownum % 2) == 0)
|
||||
{
|
||||
// set this to the white on red cell style
|
||||
// we defined above
|
||||
c.setCellStyle(cs2);
|
||||
}
|
||||
|
||||
}
|
||||
// set this cell to the first cell style we defined
|
||||
c.setCellStyle(cs);
|
||||
}
|
||||
|
||||
//draw a thick black border on the row at the bottom using BLANKS
|
||||
// advance 2 rows
|
||||
rownum++;
|
||||
rownum++;
|
||||
// create a string cell (see why += 2 in the
|
||||
c = r.createCell((short) (cellnum + 1));
|
||||
|
||||
r = s.createRow(rownum);
|
||||
// set the cell's string value to "TEST"
|
||||
c.setCellValue("TEST");
|
||||
// make this column a bit wider
|
||||
s.setColumnWidth((short) (cellnum + 1), (short) ((50 * 8) / ((double) 1 / 20)));
|
||||
|
||||
// define the third style to be the default
|
||||
// except with a thick black border at the bottom
|
||||
cs3.setBorderBottom(cs3.BORDER_THICK);
|
||||
|
||||
//create 50 cells
|
||||
for (short cellnum = (short) 0; cellnum < 50; cellnum++)
|
||||
// on every other row
|
||||
if ((rownum % 2) == 0)
|
||||
{
|
||||
//create a blank type cell (no value)
|
||||
c = r.createCell(cellnum);
|
||||
// set it to the thick black border style
|
||||
c.setCellStyle(cs3);
|
||||
// set this to the white on red cell style
|
||||
// we defined above
|
||||
c.setCellStyle(cs2);
|
||||
}
|
||||
|
||||
//end draw thick black border
|
||||
}
|
||||
}
|
||||
|
||||
//draw a thick black border on the row at the bottom using BLANKS
|
||||
// advance 2 rows
|
||||
rownum++;
|
||||
rownum++;
|
||||
|
||||
r = s.createRow(rownum);
|
||||
|
||||
// define the third style to be the default
|
||||
// except with a thick black border at the bottom
|
||||
cs3.setBorderBottom(cs3.BORDER_THICK);
|
||||
|
||||
//create 50 cells
|
||||
for (short cellnum = (short) 0; cellnum < 50; cellnum++)
|
||||
{
|
||||
//create a blank type cell (no value)
|
||||
c = r.createCell(cellnum);
|
||||
// set it to the thick black border style
|
||||
c.setCellStyle(cs3);
|
||||
}
|
||||
|
||||
//end draw thick black border
|
||||
|
||||
|
||||
// demonstrate adding/naming and deleting a sheet
|
||||
// create a sheet, set its title then delete it
|
||||
s = wb.createSheet();
|
||||
wb.setSheetName(1, "DeletedSheet");
|
||||
wb.removeSheetAt(1);
|
||||
//end deleted sheet
|
||||
// demonstrate adding/naming and deleting a sheet
|
||||
// create a sheet, set its title then delete it
|
||||
s = wb.createSheet();
|
||||
wb.setSheetName(1, "DeletedSheet");
|
||||
wb.removeSheetAt(1);
|
||||
//end deleted sheet
|
||||
|
||||
// write the workbook to the output stream
|
||||
// close our file (don't blow out our file handles
|
||||
wb.write(out);
|
||||
out.close();
|
||||
// write the workbook to the output stream
|
||||
// close our file (don't blow out our file handles
|
||||
wb.write(out);
|
||||
out.close();
|
||||
]]></source>
|
||||
</section>
|
||||
<section title="Reading or modifying an existing file">
|
||||
|
|
|
@ -179,13 +179,13 @@
|
|||
// Style the cell with borders all around.
|
||||
HSSFCellStyle style = wb.createCellStyle();
|
||||
style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
|
||||
style.setBottomBorderColor(HSSFCellStyle.BLACK);
|
||||
style.setBottomBorderColor(HSSFColor.BLACK.index);
|
||||
style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
|
||||
style.setLeftBorderColor(HSSFCellStyle.GREEN);
|
||||
style.setLeftBorderColor(HSSFColor.GREEN.index);
|
||||
style.setBorderRight(HSSFCellStyle.BORDER_THIN);
|
||||
style.setRightBorderColor(HSSFCellStyle.BLUE);
|
||||
style.setRightBorderColor(HSSFColor.BLUE.index);
|
||||
style.setBorderTop(HSSFCellStyle.BORDER_MEDIUM_DASHED);
|
||||
style.setTopBorderColor(HSSFCellStyle.AUTOMATIC);
|
||||
style.setTopBorderColor(HSSFColor.BLACK.index);
|
||||
cell.setCellStyle(style);
|
||||
|
||||
// Write the output to a file
|
||||
|
@ -205,7 +205,7 @@
|
|||
|
||||
// Aqua background
|
||||
HSSFCellStyle style = wb.createCellStyle();
|
||||
style.setFillBackgroundColor(HSSFCellStyle.AQUA);
|
||||
style.setFillBackgroundColor(HSSFColor.AQUA.index);
|
||||
style.setFillPattern(HSSFCellStyle.BIG_SPOTS);
|
||||
HSSFCell cell = row.createCell((short) 1);
|
||||
cell.setCellValue("X");
|
||||
|
@ -213,7 +213,7 @@
|
|||
|
||||
// Orange "foreground", foreground being the fill foreground not the font color.
|
||||
style = wb.createCellStyle();
|
||||
style.setFillForegroundColor(HSSFCellStyle.ORANGE);
|
||||
style.setFillForegroundColor(HSSFColor.ORANGE.index);
|
||||
style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
|
||||
cell = row.createCell((short) 2);
|
||||
cell.setCellValue("X");
|
||||
|
|
|
@ -21,9 +21,8 @@
|
|||
Finish Charts
|
||||
</action>
|
||||
<action context="code">
|
||||
Add Formulas.
|
||||
Finish Formulas.
|
||||
</action>
|
||||
|
||||
</actions>
|
||||
|
||||
<actions priority="medium">
|
||||
|
|
|
@ -55,6 +55,7 @@
|
|||
package org.apache.poi.hssf.usermodel.examples;
|
||||
|
||||
import org.apache.poi.hssf.usermodel.*;
|
||||
import org.apache.poi.hssf.util.HSSFColor;
|
||||
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
|
@ -93,7 +94,7 @@ public class BigExample
|
|||
//set font 1 to 12 point type
|
||||
f.setFontHeightInPoints((short) 12);
|
||||
//make it red
|
||||
f.setColor((short) HSSFCellStyle.RED);
|
||||
f.setColor((short) HSSFColor.RED.index);
|
||||
// make it bold
|
||||
//arial is the default font
|
||||
f.setBoldweight(f.BOLDWEIGHT_BOLD);
|
||||
|
@ -101,7 +102,7 @@ public class BigExample
|
|||
//set font 2 to 10 point type
|
||||
f2.setFontHeightInPoints((short) 10);
|
||||
//make it the color at palette index 0xf (white)
|
||||
f2.setColor((short) HSSFCellStyle.WHITE);
|
||||
f2.setColor((short) HSSFColor.WHITE.index);
|
||||
//make it bold
|
||||
f2.setBoldweight(f2.BOLDWEIGHT_BOLD);
|
||||
|
||||
|
@ -115,7 +116,7 @@ public class BigExample
|
|||
//fill w fg fill color
|
||||
cs2.setFillPattern((short) HSSFCellStyle.SOLID_FOREGROUND);
|
||||
// set foreground fill to red
|
||||
cs2.setFillForegroundColor((short) HSSFCellStyle.RED);
|
||||
cs2.setFillForegroundColor((short) HSSFColor.RED.index);
|
||||
|
||||
// set the font
|
||||
cs2.setFont(f2);
|
||||
|
|
|
@ -55,6 +55,7 @@
|
|||
package org.apache.poi.hssf.usermodel.examples;
|
||||
|
||||
import org.apache.poi.hssf.usermodel.*;
|
||||
import org.apache.poi.hssf.util.HSSFColor;
|
||||
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
|
@ -69,31 +70,6 @@ public class Borders
|
|||
public static void main(String[] args)
|
||||
throws IOException
|
||||
{
|
||||
HSSFWorkbook wb = new HSSFWorkbook();
|
||||
HSSFSheet sheet = wb.createSheet("new sheet");
|
||||
|
||||
// Create a row and put some cells in it. Rows are 0 based.
|
||||
HSSFRow row = sheet.createRow((short) 1);
|
||||
|
||||
// Create a cell and put a value in it.
|
||||
HSSFCell cell = row.createCell((short) 1);
|
||||
cell.setCellValue(4);
|
||||
|
||||
// Style the cell with borders all around.
|
||||
HSSFCellStyle style = wb.createCellStyle();
|
||||
style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
|
||||
style.setBottomBorderColor(HSSFCellStyle.BLACK);
|
||||
style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
|
||||
style.setLeftBorderColor(HSSFCellStyle.GREEN);
|
||||
style.setBorderRight(HSSFCellStyle.BORDER_THIN);
|
||||
style.setRightBorderColor(HSSFCellStyle.BLUE);
|
||||
style.setBorderTop(HSSFCellStyle.BORDER_MEDIUM_DASHED);
|
||||
style.setTopBorderColor(HSSFCellStyle.AUTOMATIC);
|
||||
cell.setCellStyle(style);
|
||||
|
||||
// Write the output to a file
|
||||
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
|
||||
wb.write(fileOut);
|
||||
fileOut.close();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -55,6 +55,7 @@
|
|||
package org.apache.poi.hssf.usermodel.examples;
|
||||
|
||||
import org.apache.poi.hssf.usermodel.*;
|
||||
import org.apache.poi.hssf.util.HSSFColor;
|
||||
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
|
@ -77,7 +78,7 @@ public class FrillsAndFills
|
|||
|
||||
// Aqua background
|
||||
HSSFCellStyle style = wb.createCellStyle();
|
||||
style.setFillBackgroundColor(HSSFCellStyle.AQUA);
|
||||
style.setFillBackgroundColor(HSSFColor.AQUA.index);
|
||||
style.setFillPattern(HSSFCellStyle.BIG_SPOTS);
|
||||
HSSFCell cell = row.createCell((short) 1);
|
||||
cell.setCellValue("X");
|
||||
|
@ -85,7 +86,7 @@ public class FrillsAndFills
|
|||
|
||||
// Orange "foreground", foreground being the fill foreground not the font color.
|
||||
style = wb.createCellStyle();
|
||||
style.setFillForegroundColor(HSSFCellStyle.ORANGE);
|
||||
style.setFillForegroundColor(HSSFColor.ORANGE.index);
|
||||
style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
|
||||
cell = row.createCell((short) 2);
|
||||
cell.setCellValue("X");
|
||||
|
|
Loading…
Reference in New Issue