プロが教えるわが家の防犯対策術!

サーブレットとApachePOIについて質問です。
下記のソースで書き込んだセルをすべて黄色で塗りつぶしをしたいのですが、
ソースの書き方を教えてください。
以上、お願いします。

「ソース」
// 業務名前

String[] name4 = request.getParameterValues("gyoumuname");
for (int i = 0; i < name4.length; i++) {
System.out.println(i + " " + name4[i]);
name4[i] = new String(name4[i].getBytes("8859_1"), "UTF-8");
List outList=new ArrayList();
for (int i = 0; i < name4.length; i++) {
outList.add(name4[i]);
}
for (int i = 0; i < outList.size(); i++) {
Row row5 = sheet.getRow(8 + i);
row5.getCell(3).setCellValue(new HSSFRichTextString(outList.get(i).toString()));
}
// 値を書き込んだエクセルを出力する
FileOutputStream out = null;
try {
out = new FileOutputStream(
"C:\\Users\\satou\\Desktop\\weekreport.xls");
workbook.write(out);
} catch (IOException e) {
System.out.println(e.toString());
} finally {
try {
out.close();
} catch (IOException e) {
System.out.println(e.toString());
}
}
以下省略。

A 回答 (2件)

> //HSSFCellStyle style = workbook.createCellStyle();


> //style.setFillForegroundColor(HSSFColor.YELLOW.index); //黄色
> //style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); //塗り潰し

これはスタイルを定義しただけであって、org.apache.poi.ss.usermodel.Cell::setCellStyle(CellStyle)を呼び出さないと
対象セルにスタイル反映しませんよ。
https://poi.apache.org/apidocs/org/apache/poi/ss …


値を設定している場面は
> row5.getCell(3).setCellValue(new HSSFRichTextString(outList.get(i).toString()));
なのですから、
Cell cell = row5.getCell(3);
cell.setCellValue(new HSSFRichTextString(outList.get(i).toString()));
CellStyle style = workbook.createCellStyle();
style.setFillForegroundColor(IndexedColors.YELLOW.getIndex()); //黄色
style.setFillPattern(CellStyle.SOLID_FOREGROUND); //塗り潰し
cell.setCellStyle(style);
などとなりませんか?
    • good
    • 0

この回答への補足

このように書いたのですが、だめでした。
for (int i = 0; i < name4.length; i++) {
//HSSFCellStyle style = workbook.createCellStyle();
//style.setFillForegroundColor(HSSFColor.YELLOW.index); //黄色
//style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); //塗り潰し

補足日時:2014/12/11 19:11
    • good
    • 0

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!