全体像
参照したり読者自身の環境にコピーしたりしやすいように、この例で使ったWriterクラスの全体を示しておきます。
package com.fywservices.poi.hssf;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;
import org.apache.poi.hssf.usermodel.*;
public class ExcelWriterDemo
{
private static final String[] HEADINGS =
new String[]{"Symbol", "Price", "PEG", "Yield", "Change"};
public HSSFWorkbook
createExcelWorkbook(ExcelWriterDemoData[] data)
{
HSSFWorkbook excelWorkbook = new HSSFWorkbook();
HSSFSheet excelSheet = null;
HSSFRow sheetRow = null;
HSSFCell rowCell = null;
ExcelWriterDemoStyles =
new ExcelWriterDemoStyles(excelWorkbook);
ExcelWriterDemoStock stock = null;
HSSFCellStyle headerStyle = styles.getHeaderStyle();
HSSFDataFormat dataFormat = styles.getDataFormat();
HSSFFont headerFont = styles.getHeaderFont();
HSSFPalette palette = styles.getPalette();
int rowIndex = 1;
excelSheet =
excelWorkbook.createSheet("Today Stock Analysis");
sheetRow = excelSheet.createRow(0);
excelSheet.setColumnWidth(0, 6000);
excelSheet.setColumnWidth(1, 6000);
excelSheet.setColumnWidth(2, 2500);
excelSheet.setColumnWidth(3, 2500);
excelSheet.setColumnWidth(4, 3000);
sheetRow.setHeight((short)600);
for(int cellIndex = 0; cellIndex < HEADINGS.length;
{
rowCell = sheetRow.createCell(cellIndex);
rowCell.setCellType(HSSFCell.CELL_TYPE_STRING);
rowCell.setCellValue(new
HSSFRichTextString(HEADINGS[cellIndex]));
rowCell.setCellStyle(headerStyle);
}
for(Iterator<ExcelWriterDemoStock> stockIt =
data[0].getDemoDataCollection().iterator();
stockIt.hasNext(); rowIndex++)
{
sheetRow = excelSheet.createRow(rowIndex);
stock = stockIt.next();
rowCell = sheetRow.createCell(0);
rowCell.setCellType(HSSFCell.CELL_TYPE_STRING);
rowCell.setCellStyle(styles.getDataStringStyle());
rowCell.setCellValue(new
HSSFRichTextString(stock.getSymbol()));
rowCell = sheetRow.createCell(1);
rowCell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
rowCell.setCellStyle(styles.getDataDollarStyle());
rowCell.setCellValue(stock.getPrice());
rowCell = sheetRow.createCell(2);
rowCell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
rowCell.setCellValue(stock.getPeg());
rowCell = sheetRow.createCell(3);
rowCell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
rowCell.setCellValue(stock.getYield());
rowCell = sheetRow.createCell(4);
rowCell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
rowCell.setCellValue(stock.getChange());
}
excelSheet =
excelWorkbook.createSheet("Yesterday Stock Analysis");
sheetRow = excelSheet.createRow(0);
excelSheet.setColumnWidth(0, 6000);
excelSheet.setColumnWidth(1, 6000);
excelSheet.setColumnWidth(2, 2500);
excelSheet.setColumnWidth(3, 2500);
excelSheet.setColumnWidth(4, 3000);
sheetRow.setHeight((short)600);
for(int cellIndex =
0; cellIndex < HEADINGS.length; cellIndex++)
{
rowCell = sheetRow.createCell(cellIndex);
rowCell.setCellType(HSSFCell.CELL_TYPE_STRING);
rowCell.setCellValue(new
HSSFRichTextString(HEADINGS[cellIndex]));
//TODO: SHOW RICH TEXT STRING APPROACH TOO
rowCell.setCellStyle(headerStyle);
}
rowIndex = 1;
for(Iterator<ExcelWriterDemoStock> stockIt =
data[1].getDemoDataCollection().iterator();
stockIt.hasNext(); rowIndex++)
{
sheetRow = excelSheet.createRow(rowIndex);
stock = stockIt.next();
rowCell = sheetRow.createCell(0);
rowCell.setCellType(HSSFCell.CELL_TYPE_STRING);
rowCell.setCellStyle(styles.getDataStringStyle());
rowCell.setCellValue(new
HSSFRichTextString(stock.getSymbol()));
rowCell = sheetRow.createCell(1);
rowCell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
rowCell.setCellStyle(styles.getDataDollarStyle());
rowCell.setCellValue(stock.getPrice());
rowCell = sheetRow.createCell(2);
rowCell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
rowCell.setCellValue(stock.getPeg());
rowCell = sheetRow.createCell(3);
rowCell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
rowCell.setCellValue(stock.getYield());
rowCell = sheetRow.createCell(4);
rowCell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
rowCell.setCellValue(stock.getChange());
}
return excelWorkbook;
}
}
まとめ
Webアプリケーションはユーザーに情報を提示する便利な手段です。情報を提示することが第一段階だとすれば、その次の段階は情報自体を役に立つものにすることです。Web 2.0はWebベースのデータの有用性を高める1つの方法ですが、それ以前、つまり企業に有用性を売り込むためのさまざまなキャッチコピーが登場する以前から存在していた方法も数多くあります。そのような(インターネットの年表で言えば)"古い"方法のうち、見直していきたいのがApache POIプロジェクトとHSSFライブラリです。あるいは、ただの整然とレイアウトされたHTMLとMIMEタイプでも役に立つかもしれません。
