汎用excelレポート生成ツールクラス

12840 ワード

次のツール類は今日半日の労働成果です.
これからは自分でも使えるかもしれません.ここで記録をします.会社の情報に関するコードおよびコメントは削除されました.ははは...
オープンソースのPOIキット、Webサイトに依存しています.http://poi.apache.org/
 
このツール類は自分でまだ厳しいテストを受けていないので、簡単に測ってみましたが、大体差が少なくOKです.
ソースコードに注記されている部分がテストコードです
 
上源コード羅:
 
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.util.CollectionUtils;


/**
 *   Excel     
 */
public class ExcelProductUtil {

    /**
     *            
     * 
     * @param contentList      
     * @param title         T       
     *                Map<String,String>:key : T       
     *                                   value:        
     * @param seq                 
     *        Map<String,String>:key : T        
     *                           value:         
     * @param sheetName sheet   
     * @param rowNum    (    0)
     * 
     *   : title seq     
     *      seq  key   title Key        
     *      seq  value    1           
     *      title  Key     T      
     *               
     */
    public static HSSFWorkbook productReportSameColumn(List<? extends Object> contentList,
                                                       Map<String, String> title,
                                                       Map<String, Integer> seq, 
                                                       String sheetName,
                                                       int rowNum) {
        //    Excel   
        HSSFWorkbook workbook = new HSSFWorkbook();
        try {
            //  title        T   
            checkBeforeReportSameColumn(contentList, title, seq, sheetName, rowNum);

            HSSFSheet sheet = getSheet(sheetName, workbook);

            //           
            createReportSameColumnHead(sheet, title, seq, rowNum);

            //        
            createReportSameColumnBody(sheet, title, contentList, seq, rowNum);

        } catch (Throwable e) {
            throw new RuntimeException(e);
        }
        return workbook;
    }

    /**
     *     Excel   
     * @param sheetName
     * @return
     */
    private static HSSFSheet getSheet(String sheetName, HSSFWorkbook workbook) {
        HSSFSheet sheet = workbook.createSheet(sheetName);
        return sheet;
    }

    /**
     *         
     * 
     * @param workbook
     * @param title
     * @param contentList
     * @throws IllegalAccessException 
     * @throws IllegalArgumentException 
     */
    private static void createReportSameColumnBody(HSSFSheet sheet, Map<String, String> title,
                                                   List<? extends Object> contentList,
                                                   Map<String, Integer> seq, int rowNum)
                                                    throws IllegalArgumentException,
                                                               IllegalAccessException 
    {
        ++rowNum;
        for (Object content : contentList) {

            HSSFRow row = sheet.createRow(rowNum);

            Field[] fields = content.getClass().getDeclaredFields();
            for (Field f : fields) {
                f.setAccessible(true);
                String name = f.getName();
                if (title.containsKey(name)) {

                    HSSFCell cell = row.createCell(seq.get(name));
                    cell.setCellValue((String) f.get(content));
                }
            }
            ++rowNum;
        }
    }

    /**
     *            
     * 
     * @param workbook
     * @param title
     */
    private static void createReportSameColumnHead(HSSFSheet sheet, Map<String, String> title,
                                                   Map<String, Integer> seq, int rowNum) {
        //           
        List<Map.Entry<String, Integer>> headDesList = getSoredPropertisList(seq);

        HSSFRow row = sheet.createRow(rowNum);

        for (Map.Entry<String, Integer> entry : headDesList) {
            String headName = title.get(entry.getKey());
            HSSFCell cell = row.createCell(entry.getValue());
            cell.setCellValue(headName);
        }
    }

    /**
     *            
     * 
     * @param seq
     * @return
     */
    private static List<Map.Entry<String, Integer>> getSoredPropertisList(Map<String, Integer> seq) {

        List<Map.Entry<String, Integer>> list = new ArrayList<Map.Entry<String, Integer>>(seq
            .entrySet());

        //  
        Collections.sort(list, new Comparator<Map.Entry<String, Integer>>() {
            public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
                return o1.getValue() - o2.getValue();
            }
        });
        return list;
    }

    /**
     *   title        T   
     * 
     * @param contentList
     * @param title
     */
    private static void checkBeforeReportSameColumn(List<? extends Object> contentList,
                                                    Map<String, String> title,
                                                    Map<String, Integer> seq, 
                                                    String sheetName,
                                                    int rowNum) {
        if (CollectionUtils.isEmpty(contentList) || CollectionUtils.isEmpty(title)
            || CollectionUtils.isEmpty(seq) || StringUtil.isBlank(sheetName)) {
            throw new RuntimeException("        !");
        }
        if (rowNum < 0) {
            throw new RuntimeException("rowNum      0!");
        }

        //  T     
        List<String> propertiesNamesOfT = getTPropertiesName(contentList.get(0));

        //  title     
        List<String> titlesNames = getTitleNames(title);

        //    
        compareProperties(propertiesNamesOfT, titlesNames);

        //seq  key   title Key    
        compareKey(title, seq);

        //seq  value    1        
        compareSeqValue(seq);
    }

    /**
     * seq  value    1        
     * 
     * @param seq
     */
    private static void compareSeqValue(Map<String, Integer> seq) {

        List<Map.Entry<String, Integer>> list = getSoredPropertisList(seq);

        if (list.get(0).getValue() != 1 || list.get(list.size() - 1).getValue() != list.size()) {
            throw new RuntimeException("seq  value    1        ");
        }

    }

    /**
     * seq  key   title Key    
     * 
     * @param title
     * @param seq
     */
    private static void compareKey(Map<String, String> title, Map<String, Integer> seq) {
        Set<String> titleSet = title.keySet();
        Set<String> seqSet = seq.keySet();

        if (titleSet.size() != seqSet.size()) {
            throw new RuntimeException("seq  key   title Key      !");
        }

        Set<String> titleTempSet = new HashSet<String>();
        titleTempSet.addAll(titleSet);
        titleTempSet.addAll(seqSet);
        if (titleTempSet.size() != seqSet.size()) {
            throw new RuntimeException("seq  key   title Key      !");
        }
    }

    /**
     *     :titlesNames   propertiesNamesOfT   
     * 
     * @param propertiesNamesOfT
     * @param titlesNames
     */
    private static void compareProperties(List<String> propertiesNamesOfT, List<String> titlesNames) {
        List<String> tempPropertiesNamesOfT = new ArrayList<String>();
        List<String> tempTitlesNames = new ArrayList<String>();

        //             
        for (String tName : propertiesNamesOfT) {
            String temp = tName.toLowerCase();
            tempPropertiesNamesOfT.add(temp);
        }
        for (String tName : titlesNames) {
            String temp = tName.toLowerCase();
            tempTitlesNames.add(temp);
        }
        if (!tempPropertiesNamesOfT.containsAll(titlesNames)) {
            throw new RuntimeException("title        T      !");
        }
    }

    /**
     *   title     
     * 
     * @param title
     * @return
     */
    private static List<String> getTitleNames(Map<String, String> title) {
        List<String> titleNames = new ArrayList<String>();
        for (Map.Entry<String, String> entry : title.entrySet()) {
            titleNames.add(entry.getKey());
        }
        return titleNames;
    }

    /**
     *   T     
     * 
     * @param t
     * @return
     */
    private static List<String> getTPropertiesName(Object o) {
        List<String> list = new ArrayList<String>();

        Field[] fields = o.getClass().getDeclaredFields();
        for (Field f : fields) {
            f.setAccessible(true);
            list.add(f.getName());
        }
        return list;
    }

    //    private static class BankLog {
    //        private String name;
    //        private String password;
    //
    //        public String getName() {
    //            return name;
    //        }
    //
    //        public void setName(String name) {
    //            this.name = name;
    //        }
    //
    //        public String getPassword() {
    //            return password;
    //        }
    //
    //        public void setPassword(String password) {
    //            this.password = password;
    //        }
    //    }
    //
    //    public static void main(String[] args) throws Exception {
    //
    //        List<BankLog> bankLog = new ArrayList<BankLog>();
    //        BankLog bankLog1 = new BankLog();
    //        bankLog1.setName("111");
    //        bankLog1.setPassword("222");
    //        BankLog bankLog2 = new BankLog();
    //        bankLog2.setName("0000");
    //        bankLog2.setPassword("333");
    //        bankLog.add(bankLog1);
    //        bankLog.add(bankLog2);
    //
    //        Map<String, String> title = new HashMap<String, String>();
    //        title.put("name", "  ");
    //        title.put("password", "  ");
    //
    //        Map<String, Integer> seq = new HashMap<String, Integer>();
    //        seq.put("name", 2);
    //        seq.put("password", 1);
    //
    //        String sheetName = "Sheet1";
    //        int rowNum = 1;
    //
    //        HSSFWorkbook book = productReportSameColumn(bankLog, title, seq, sheetName, rowNum);
    //        String filename = "text.xls";
    //        FileOutputStream fOut = new FileOutputStream(filename);
    //        book.write(fOut);
    //        fOut.flush();
    //        fOut.close();
    //    }
}