Listing 1
String value;
switch (cell.getCellType())
{
case HSSFCell.CELL_TYPE_FORMULA :
value = "FORMULA "+ cell.getCellFormula();
break;
case HSSFCell.CELL_TYPE_NUMERIC :
value = "NUMERIC value="
+ String.valueOf(cell.getNumericCellValue());
break;
case HSSFCell.CELL_TYPE_BOOLEAN :
value = "Boolean value="
+ String.valueOf(cell.getBooleanCellValue());
break;
case HSSFCell.CELL_TYPE_STRING :
value = "STRING value="
+ cell.getStringCellValue();
break;
case HSSFCell.CELL_TYPE_DATE :
value = "DATE value="
+ cell.getDateCellValue().toString();
break;
default :
}
Listing 2
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet();
HSSFRow row = sheet.createRow((short)0);
HSSFCell cell = row.createCell((short)0);
cell.setCellValue(1); // cell A1
row.createCell((short)1).setCellValue(1.2); //cell A2
row.createCell((short)2).setCellFormula("A1+A2"); //cell A3 is 2.2
row.createCell((short)3).setCellValue("The next cell is a boolean, and then a
date");
row.createCell((short)4).setCellValue(true); //cell A5
row.createCell((short)4).setCellValue(new Date()); //cell A6 contains todays date
FileOutputStream out = new FileOutputStream("data1.xls");
wb.write(out);
out.close();
Listing 3
InputStream in = new FileInputStream("data.xls"));
HSSFWorkbook wb = new HSSFWorkbook(in); // read in existing workbook
HSSFSheet sheet = wb.getSheetAt(0);
HSSFRow row = sheet.getRow(0);
if (row == null) row = sheet.createRow(0); // check if row already exists
HSSFCell cell = row.getCell(0);
if (cell == null) row.createCell(0); // check if cell already exits
cell.setCellValue(2.5); // update cell value
FileOutputStream out = new FileOutputStream("data2.xls");
wb.write(out); // and write it back out.
in.close; out.close();
Listing 4
5 Range r = doc.getRange();
6
7 int numSections = r.numSections();
8 for(int x = 0; x < numSections; x++)
9 {
10 Section sect = r.getSection(x);
11 int numPars = sect.numParagraphs();
12 for (int y = 0; y < numPars; y++)
13 {
14 Paragraph par = sect.getParagraph(y);
15 int numRuns = par.numCharacterRuns();
16 for(int z = 0; z < numRuns; z++)
17 {
18 CharacterRun run =
19 par.getCharacterRun(z);
20 }
21 }
22}
Listing 5
1 for (int x = 0; x < numPars; x++)
2 {
3 Paragraph par =
4 range.getParagraph(x);
5
6 if (par.isInTable())
7 {
8 Table t = range.getTable(par);
9
10 //do something with the table...
11
12 x += (t.numParagraphs() - 1);
13 }
14}
Listing 6
import java.io.*;
import org.apache.poi.hwpf.*;
import org.apache.poi.hwpf.usermodel.*;
public class Listing1
{
public Listing1()
{
}
public static void main(String[] args)
{
try
{
FileInputStream in = new FileInputStream("C:\\blank.doc");
HWPFDocument doc = new HWPFDocument(in);
Range range = doc.getRange();
CharacterProperties props = new CharacterProperties();
// Set the font size in half points
Range currentRange = range;
// Slowly increase the font size
for (int x = 8; x <= 64; x += 4)
{
// Set the half point size of the font
props.setFontSize(x);
currentRange = currentRange.insertAfter(" Hello World!", props);
}
// Display Bold characters
props.setBold(true);
currentRange = currentRange.insertAfter(" Bold", props);
// Display Italic characters
props.setItalic(true);
currentRange = currentRange.insertAfter(" Italic", props);
// Display charcters with a Double Strikethrough
props.setDoubleStrikeThrough(true);
currentRange = currentRange.insertAfter(" Double Strikethrough", props);
// Insert an empty paragraph for readability
currentRange = currentRange.insertAfter(new ParagraphProperties(), 0);
// Reset the character properties
props = new CharacterProperties();
props.setFontSize(32);
// Create a numbered list
HWPFList list = new HWPFList(true, doc.getStyleSheet());
int listID = doc.registerList(list);
// Insert a list entry
currentRange = currentRange.insertAfter(new ParagraphProperties(), listID, 1,
0);
props.setIco24(0xff0000);
currentRange = currentRange.insertAfter(" Blue list entry", props);
// Insert another list entry
currentRange = currentRange.insertAfter(new ParagraphProperties(), listID, 1,
0);
props.setIco24(0xff);
props.setFontSize(38);
props.setCapitalized(true);
currentRange = currentRange.insertAfter(" larger red capitalized", props);
//Last list entry
currentRange = currentRange.insertAfter(new ParagraphProperties(), listID, 1,
0);
props.setIco24(0);
props.setCapitalized(false);
props.setCharacterSpacing(150);
currentRange = currentRange.insertAfter(" Large character spacing", props);
// Write out the document
FileOutputStream out = new FileOutputStream("C:\\hello.doc");
doc.write(out);
out.flush();
out.close();
}
catch (Throwable t)
{
t.printStackTrace();
}
}
}