POIのHSSFWorkbookなどクラスを利用するとJavaでデータをエクセルに出力することはできますが、毎回自分でエクセルのシートを作成して、データをセルにセットしなければいけないです。ちょっと面倒じゃないかと思ってます。
その問題を解決するため、以下の共通できるJavaクラスを作りました。データを格納しているリストとシート名を指定して、エクセルを作れます。
※POIとは、Jakarta POIは総称であり、JavaアプリケーションからExcelなどのMicrosoft製品のフォーマットファイルを読み書きすることができます(Excelだけではなくて、Wordなども操作可能ということです)。Excelファイルを取り扱う場合はその中のHSSFを使います(Wordの場合はHWPFのようです)。
では、以下は該当クラスのソースコード(ここにクリックしてダウンロードできます):
- package com.cthq.crm.account.common;
- import java.util.List;
- import org.apache.poi.hssf.usermodel.HSSFCell;
- import org.apache.poi.hssf.usermodel.HSSFCellStyle;
- import org.apache.poi.hssf.usermodel.HSSFFont;
- import org.apache.poi.hssf.usermodel.HSSFRow;
- import org.apache.poi.hssf.usermodel.HSSFSheet;
- import org.apache.poi.hssf.usermodel.HSSFWorkbook;
- public class ExcelUtil {
- /**
- * Excelファイルにエクスポート
- *
- * @param list データ集
- * @param sheetname エクセルシート名
- * @return
- */
- public synchronized HSSFWorkbook getWorkbook(List list,String sheetname){
- HSSFWorkbook hwb = new HSSFWorkbook();// 新しいHSSFWorkbookインスタンス作成
- HSSFSheet hs = hwb.createSheet();// 新しいシート対象を作成する
- hwb.setSheetName(0, sheetname);// 作成したシートの名称
- List excellist=list;
- HSSFFont font = hwb.createFont();
- font.setColor((short) 12);
- HSSFCellStyle style = hwb.createCellStyle();
- style.setFont(font);
- HSSFRow hr = hs.createRow((short) 3);// ヘッダー
- List headerlist=(List)excellist.get(0);
- for (int i = 0; i < headerlist.size(); i++) {
- HSSFCell cell = hr.createCell((short) i);// セル
- //cell.setEncoding(HSSFCell.ENCODING_UTF_16);// 必要であればコマンドアウト
- cell.setCellValue((String)headerlist.get(i));// セル文字列型のセット
- cell.setCellStyle(style);
- }
- for(int i = 1; i < excellist.size(); i++){
- List valuelist=(List)excellist.get(i);
- hr = hs.createRow((short) i);// 新しい行
- for (int ii = 0; ii < valuelist.size(); ii++) {
- HSSFCell cell = hr.createCell((short) ii);// セル
- //cell.setEncoding(HSSFCell.ENCODING_UTF_16);// 必要であればコマンドアウト
- cell.setCellValue((String)valuelist.get(ii));// セル文字列型のセット
- }
- }
- return hwb;
- }
- /**
- * Excelファイルデータにエクスポートする
- *
- * @param lists データ集
- * @param namesエクセルシート名
- * @return
- */
- public synchronized HSSFWorkbook getWorkbook(List[] lists, String[] names){
- HSSFWorkbook hwb = new HSSFWorkbook();// 新しいHSSFWorkbookインスタンス作成
- //シートをワークブックに追加する
- for(int j=0; j<lists.length; j++){
- HSSFSheet hs1 = hwb.createSheet();// 新しいシート対象を作成する
- hwb.setSheetName(j, names[j]); // 作成したシートの名称
- List excellist=lists[j];
- HSSFFont font = hwb.createFont();
- font.setColor((short) 12);
- HSSFCellStyle style = hwb.createCellStyle();
- style.setFont(font);
- HSSFRow hr = hs1.createRow((short) 0);// ヘッダー
- List headerlist=(List)excellist.get(0);
- for (int i = 0; i < headerlist.size(); i++) {
- HSSFCell cell = hr.createCell((short) i);// セル
- //cell.setEncoding(HSSFCell.ENCODING_UTF_16);// 必要であればコマンドアウト
- cell.setCellValue((String)headerlist.get(i));// セル文字列型のセット
- cell.setCellStyle(style);
- }
- for(int i = 1; i < excellist.size(); i++){
- List valuelist=(List)excellist.get(i);
- hr = hs1.createRow((short) i);// 新しい行
- for (int ii = 0; ii < valuelist.size(); ii++) {
- HSSFCell cell = hr.createCell((short) ii);// セル
- //cell.setEncoding(HSSFCell.ENCODING_UTF_16);// 必要であればコマンドアウト
- cell.setCellValue((String)valuelist.get(ii));// セル文字列型のセット
- }
- }
- }
- return hwb;
- }
- public static void main(String[] args) throws Exception {
- }
- }
以下は該当クラスをサブレート上の使用例です。
- private void exportList(List numberList,String numType, HttpServletResponse response) throws Exception {
- List excellist = new ArrayList();
- String docName = "exp189CDMA.xls";
- String sheetname ="";
- ExcelUtil excelutil = new ExcelUtil();
- try {
- List headerlist = new ArrayList();
- headFont(headerlist);
- headerList(headerlist,numType);
- excellist.add(headerlist);
- if (numberList.size() > 0) {
- for (int i = 0; i < numberList.size(); i++) {
- Map map =new HashMap();
- if(numberList.get(i) instanceof Map){
- map = (Map)numberList.get(i);
- List lineDataList = new ArrayList();
- lineDataList.add(map.get("province"));
- lineDataList.add(map.get("localname"));
- lineDataList.add(map.get("telCode"));
- for(int k=0;k<=9;k++){
- lineDataList.add(map.get("hcode"+k));
- }
- excellist.add(lineDataList);
- }
- }
- }
- } catch (Exception e) {
- throw e;
- }
- response.reset();
- response.setCharacterEncoding("UTF-8");
- docName = URLEncoder.encode(docName, "UTF-8");
- response.setContentType("application/x-msdownload;charset=UTF-8");
- response.setHeader("Content-disposition", "attachment; filename="
- + new String(docName.getBytes("UTF-8"), "UTF-8"));
- OutputStream outStream = response.getOutputStream();
- excelutil.getWorkbook(excellist, sheetname).write(outStream);
- }
- /**
- * エクセルにエクスポート
- *
- * @param mapping
- * @param form
- * @param request
- * @param response
- * @throws Exception
- * @throws Exception
- */
- private void exportList(List[] numberList,String[] reportSel, HttpServletResponse response) throws Exception {
- int length = reportSel.length;
- String docName = "exp189CDMA.xls";
- List[] excellist = new ArrayList[length];//データ配列
- String[] sheetnames = new String[length];
- for(int i=0;i<length;i++){
- sheetnames[i] =reportSel[i];
- }
- if(StringUtils.isEmpty("")){}
- ExcelUtil excelutil = new ExcelUtil();
- for(int n = 0;n<reportSel.length;n++){
- String selValue = reportSel[n];
- if("189H".equals(selValue)){
- try {
- //新規
- excellist[n] = new ArrayList();
- List headerlist = new ArrayList();
- headFont(headerlist);
- //ヘッダコード
- headerList(headerlist,"189");
- excellist[n].add(headerlist);
- List list1 = numberList[n];
- if (list1.size() > 0) {
- for (int i = 0; i < list1.size(); i++) {
- Num189CDMAExpVO vo = (Num189CDMAExpVO)list1.get(i);
- List lineDataList = new ArrayList();
- lineDataList.add(vo.getProvince());
- excellist[n].add(lineDataList);
- }
- }
- } catch (Exception e) {
- throw e;
- }
- }
- response.reset();
- response.setCharacterEncoding("UTF-8");
- docName = URLEncoder.encode(docName, "UTF-8");
- response.setContentType("application/x-msdownload;charset=UTF-8");
- response.setHeader("Content-disposition", "attachment; filename="
- + new String(docName.getBytes("UTF-8"), "UTF-8"));
- OutputStream outStream = response.getOutputStream();
- excelutil.getWorkbook(excellist, sheetnames).write(outStream);
- }
メインコンテンツEND ■
Posted on Friday, 15th May 2009 by admin
Tags: HSSFWorkbook, Jakarta, Java, POI
Posted in Java | Comments (1) | 12,578 views

December 30th, 2009 at 3:20 pm
[...] Read the original: Javaでデータをエクセルにエクスポートするクラスと使用例 … By admin | category: データ書き出し | tags: del, poi, stmt-file, データ書き出し | Palm Desktop の予定データ [...]