javaでエクセルを操作するには、poiを利用すると簡単。
エクセルのバージョンによって使用するpoiのバージョンを変える必要がある。
エクセル98の場合は2.5.1
/WEB-INF/lib/poi-2.5.1.jar
/WEB-INF/lib/poi-contrib-2.5.1.jar
/WEB-INF/lib/poi-scratchpad-2.5.1.jar
//poiハンドラー
private PoiHandler poiHandler;
//poiのハンドラを作成
PoiHandler handler = PoiHandler.create();
fileName = “ファイルパス”
//シート作成
HSSFSheet sheet = handler.createSheet(fileName);
// 行の高さを14に指定
sheet.setDefaultRowHeight((short) 14);
// セルのスタイル変更
HSSFCellStyle styleTitle = handler.createCellStyle();
//テキストのフォントを “MS Pゴシック”、サイズ11、黒に設定する。
handler.addCellStyleForFont(styleTitle, “MS Pゴシック”, (short) 11, HSSFColor.BLACK.index);
//テキストの配置を中央揃えにする。
handler.addCellStyleForFormat(styleTitle, “text” , HSSFCellStyle.ALIGN_CENTER);
//背景色
handler.addCellStyleForFill(styleTitle, HSSFColor.LAVENDER.index);
//下枠を細い黒線にする。
handler.addCellStyleForBorder(styleTitle, “bottom” , HSSFCellStyle.BORDER_THIN, HSSFColor.BLACK.index);
//上枠を細い黒線にする。
handler.addCellStyleForBorder(styleTitle, “top” , HSSFCellStyle.BORDER_THIN, HSSFColor.BLACK.index);
//セルのフォーマットを”#,##0″ の右配置にする。
handler.addCellStyleForFormat(styleNum, “#,##0” , HSSFCellStyle.ALIGN_RIGHT);
//指定のセルに値と作成したセルスタイル(styleTitle)を設定する。
cell = handler.getCell(sheet, 行番号, 列番号);
handler.setValueIntoCell(cell, 値, styleTitle);
// 印刷設定
HSSFPrintSetup setup = sheet.getPrintSetup();
//A4サイズにする。
setup.setPaperSize(HSSFPrintSetup.A4_PAPERSIZE);
// 自動改行ON
sheet.setAutobreaks(true);
// 縦幅をフリーにする。
setup.setFitHeight((short) 0);
// 横幅を1ページに収める。
setup.setFitWidth((short) 1);
//ヘッダーマージンをゼロにする。
setup.setHeaderMargin(0);
//フッターマージンをゼロにする。
setup.setFooterMargin(0);
//印刷枚数を1枚にする。
setup.setCopies((short) 1);
// Excelシートハンドラにハンドラを設定
setPoiHandler(handler);
//エクセルファイルの出力
HttpServletResponse res = getServletParameter().getHttpServletResponse();
res.setHeader(“Content-Disposition”,”attachment;filename=” + ファイル名);
//エクセル形式で出力することの宣言
res.setContentType(“application/vnd.ms-excel”);
//エクセル出力
poiHandler.writeWorkbook(res.getOutputStream());