1

Apache POI 3.6 (最新) を使用して Excel レポートを生成しようとしています。

POI ではヘッダーとフッターの生成 (テキストのみ) のサポートが制限されているため、ヘッダーが既に用意されている空白の Excel ファイルから開始し、POI を使用して Excel セルに入力することにしました (質問714172を参照)。

残念ながら、POI を含むワークブックを開いてすぐに (セル操作なしで) ディスクに書き込むと、ヘッダーが失われるようです。

この動作をテストするために使用したコードは次のとおりです。

public final class ExcelWorkbookCreator {

  public static void main(String[] args) {
    FileOutputStream outputStream = null;
    try {
      outputStream = new FileOutputStream(new File("dump.xls"));
      InputStream inputStream = ExcelWorkbookCreator.class.getResourceAsStream("report_template.xls");
      HSSFWorkbook workbook = new HSSFWorkbook(inputStream, true);
      workbook.write(outputStream);
    } catch (Exception exception) {
      throw new RuntimeException(exception);
    } finally {
      if (outputStream != null) {
        try {
          outputStream.close();
        } catch (IOException exception) {
          // Nothing much to do
        }
      }
    }
  }
}
4

2 に答える 2

1
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet    sheet    = new HSSFSheet();

Header header = sheet.getHeader() //get header from workbook's sheet
header.setCenter(HSSFHeader.font("COURIER", "Normal")+ HSSFHeader.fontSize((short) 15) + "Hello world" +new Date()); // set header with desire font style 

FileOutputStream fileOut = new FileOutputStream("C:\\book.xls");
workbook.write(fileOut);       
于 2010-04-21T11:19:53.060 に答える