[Java] iReportを使って帳票出力

正直なところ、まだよくわかっていないかも。とりあえず日本語での出力に苦労して、やっと出せるようになったので、備忘録ということで動いたソースをメモメモ。

private static final String   PREFIX = "/WEB-INF/reports/";
private static final String   SUFFIX = ".jasper";
private static final String[] TYPES  = { "text/html", "application/pdf" };
private static final String   ENCODE = "Windows-31J";

public void jasperReport(String name, String type, ResultSet data, Map map)
       throws UnsupportedEncodingException, SQLException, JRException, IOException {

    // コンテントタイプが有効かどうかチェック
    boolean isFound = false;
    for (int i = 0; i < TYPES.length; i++) {
        if (TYPES[i].equals(type)) {
            isFound = true;
            break;
        }
    }
    // 有効なコンテントタイプでない場合は例外をスローする
    if (!isFound) {
        throw new IllegalArgumentException("出力形式異常 [" + type + "]");
    }

    // 文字コードを指定する(ここで指定しないとLatin1になってしまう?)
    ExternalContext econtext = getExternalContext();
    econtext.setResponseCharacterEncoding(ENCODE);
    econtext.setRequestCharacterEncoding(ENCODE);

    // コンパイル後のレポート定義ファイルをチェック
    InputStream stream = econtext.getResourceAsStream(PREFIX + name + SUFFIX);
    if (stream == null) {
        throw new IllegalArgumentException("レポート定義異常 [" + name + "]");
    }

    // カーソルを出力データの先頭に移動
    data.beforeFirst();

    // 出力するデータを読み込む
    JRResultSetDataSource ds = new JRResultSetDataSource(data);
    JasperPrint jasperPrint = null;
    try {
        jasperPrint = JasperFillManager.fillReport(stream, map, ds);
    } catch (JRException e) {
        throw e;
    } finally {
        stream.close();
    }

    // データを整形して出力
    JRExporter exporter = null;
    HttpServletResponse response = (HttpServletResponse) econtext.getResponse();
    response.setContentType(type);

    // ------ PDF出力の場合
    if (TYPES[1].equals(type)) {
        exporter = new JRPdfExporter();
        exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
        exporter.setParameter(JRExporterParameter.OUTPUT_STREAM,
                              response.getOutputStream());

        // ------ ファイル出力にする場合は以下の処理に置き換える ここから
//            JasperExportManager.exportReportToPdfFile(jasperPrint, "JasperSample1.pdf");
        // ------ ファイル出力にする場合は以上の処理に置き換える ここまで
    }
    // ------ HTML出力の場合
    else if (TYPES[0].equals(type)) {
        FacesContext fcontext = FacesContext.getCurrentInstance();

        exporter = new JRHtmlExporter();
        exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
        exporter.setParameter(JRExporterParameter.OUTPUT_WRITER,
                              response.getWriter());
        exporter.setParameter(JRExporterParameter.CHARACTER_ENCODING, ENCODE);

        // ここでの文字コードセットは意味ないかも?
        response.setContentType(TYPES[0] + "; charset=" + ENCODE);

        HttpServletRequest request =
                (HttpServletRequest) fcontext.getExternalContext().getRequest();
        request.getSession().setAttribute(
                ImageServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE,
                jasperPrint);

        fcontext.responseComplete();
    }

    // 出力の実行
    if (exporter != null) { exporter.exportReport(); }
}

これを、例えばボタンが押されたときに呼べば、HTML形式もしくはPDF形式でDBにあるデータを読み込んで表示します。日本語も化けずにちゃんと表示できました。

パラメータに渡す値や例外処理は、上で何とかしてください(笑)

ほとんどが、「これ参考にしてね」と渡されたサンプルプログラムそのままなので、何か変なところがあってもキニシナイ(´Д`)

コメント

タイトルとURLをコピーしました