Fisshplateのサンプルプログラムを作ってみた。
プロフィールにSeasarとか書いてあるくせに、全くJavaについてのエントリーがなかったので書いてみます。
今度、仕事でFisshplateを使って帳票を出力する事になったので、勉強がてら、
http://fisshplate.sandbox.seasar.org/ja/index.html
を参考にサンプルプログラムを書いてみました。
メインクラス
/** * FisshPlateによりExcelファイルを出力するクラスです。 * * @author shun */ public class FisshPlateLogic { public static void main(String args[]) throws FPParseException, FPMergeException, IOException { new FisshPlateLogic().output(); } /** * Excelファイルを出力します。 * * @throws FPParseException * @throws FPMergeException * @throws IOException */ private void output() throws FPParseException, FPMergeException, IOException { // Templateに埋め込むデータを作成する Map<String, Object> map = new HashMap<String, Object>(); map.put("title", "Fisshplateテストです。"); List<SampleDataDto> aList = new ArrayList<SampleDataDto>(); aList.add(new SampleDataDto("1行目", 10, new Date())); aList.add(new SampleDataDto("2行目", 20, new Date())); aList.add(new SampleDataDto("3行目", 30, new Date())); aList.add(new SampleDataDto("4行目", 10, new Date())); aList.add(new SampleDataDto("5行目", 20, new Date())); aList.add(new SampleDataDto("6行目", 30, new Date())); map.put("b", aList); // Templateを作成する String path = "/fisshplateTest.xls"; FPTemplate template = new FPTemplate(); InputStream is = FisshPlateLogic.class.getResourceAsStream(path); HSSFWorkbook wb; try { wb = template.process(is, map); } catch (FPParseException e) { throw e; } catch (FPMergeException e) { throw e; } finally { is.close(); } FileOutputStream fos = new FileOutputStream("target/result.xls"); wb.write(fos); fos.close(); System.out.println("完了"); } }
データクラス
/** * Templateに渡すデータクラスです。 * * @author shun */ public class SampleDataDto { public String name; public int num; public Date date; /** * コンストラクタです。 * * @param name * @param num * @param date */ SampleDataDto(String name, int num, Date date) { this.name = name; this.num = num; this.date = date; } }
お〜。ちゃんとExcelが出力されますねぇ。って、ほとんどまんまなんですが…
違うのはSampleDataDtoのプロパティをpublicにしてる事ぐらいですかねw
おかげで、publicでもいける事を確認できた!(当然か…)
今までApache POIしか知らなかった(実装はしたことない…)のですが、
これは便利だと思いました。