JAva POI excelをエクスポートし、セルを結合して幅の高さを設定

7628 ワード

最近ネットでjavaを勉強しました POI excelエクスポート
例をまとめると、コードも簡単で、行、列の作成、幅の高さの設定、列のマージ
私のコードを貼り付けます.
public class Total{
    private Integer payWay;
    private Float amount;
    private String creatTime;
    private String endTime;

    /**
     *    get/set
     */
}
package com.excel;
import java.io.FileOutputStream;
import java.util.List;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.util.CellRangeAddress;
import org.junit.Test;

public class TestExportExcel {

    private final static  String  PAY_WAY1 = "  ";

    private final static  String PAY_WAY2 = "  ";

    private final static  String PAY_WAY3 = "  ";

    private final static  String PAY_WAY4 = "   ";

    private final static String PAY_WAY5 = "  ";

    private final static String PAY_WAY46 = "  ";

    private final static String NUMBER = "  ";

    private final static String PAY_TYPE = "    ";

    private final static String PAY_AMOUNT = "    ( )";

    private final static String REMARKS  = "  ";

    public static final short EXCEL_COLUMN_WIDTH_FACTOR = 256;

    public static final int UNIT_OFFSET_LENGTH = 7;

    public static final int[] UNIT_OFFSET_MAP = new int[] { 0, 36, 73, 109, 146, 182, 219 };

    @Test
    public void test(){
            TestExportExcel t = new TestExportExcel();
        try {
            t.testExport();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void testExport() throws Exception  {

        HSSFWorkbook wb = new HSSFWorkbook();
        /**
         *   table   
         */
        HSSFSheet sheet = wb.createSheet("     ");
        String data = "[{\"payWay\":1,\"endTime\":\"2017-09\",\"creatTime\":\"2017-08\",\"amount\":408.5},{\"payWay\":3,\"endTime\":\"2017-09\",\"creatTime\":\"2017-08\",\"amount\":520}]";
        JSONArray jsonArray = JSON.parseArray(data);
        List totalList = JSON.parseArray(jsonArray+"", Total.class);

        HSSFRow row;
        HSSFCell cell;

        int maixRowNumb = totalList.size()+4;
        for(int rowNumb = 0; rowNumb < maixRowNumb; rowNumb++) {
            /**
             *        
             */
            row = sheet.createRow(rowNumb);

            for(int columnNumb = 0; columnNumb < 4; columnNumb++) {
                /**
                 *       
                 */
                cell = row.createCell(columnNumb);
                HSSFCellStyle cellStyle = wb.createCellStyle();
                //   
                if( rowNumb == 0 ) {
                    cell.setCellValue("     ");
                }else if( rowNumb == 1 ) {//   
                    if( columnNumb == 3 ){
                        cell.setCellValue("    :2017-08 2017-09");
                    }
                }else if( rowNumb == 2 ){//   
                    switch (columnNumb){
                        case 0:
                            cell.setCellValue(TestExportExcel.NUMBER);
                            break;
                        case 1:
                            cell.setCellValue(TestExportExcel.PAY_TYPE);
                            break;
                        case 2:
                            cell.setCellValue(TestExportExcel.PAY_AMOUNT);
                            break;
                        case 3:
                            cell.setCellValue(TestExportExcel.REMARKS);
                            break;
                    }
                }else if( rowNumb < maixRowNumb-2 ){
                    Total Total = totalList.get(rowNumb-3);
                    if( columnNumb == 0 ){
                        /**
                         *   
                         */
                        int index = rowNumb-2;
                        cell.setCellValue(index);
                    }else if( columnNumb == 1 ){
                        switch (Total.getPayWay()){
                            case 1:
                                cell.setCellValue(TestExportExcel.PAY_WAY1);
                                break;
                            case 2:
                                cell.setCellValue(TestExportExcel.PAY_WAY2);
                                break;
                            case 3:
                                cell.setCellValue(TestExportExcel.PAY_WAY3);
                                break;
                            case 4:
                                cell.setCellValue(TestExportExcel.PAY_WAY4);
                                break;
                            case 5:
                                cell.setCellValue(TestExportExcel.PAY_WAY5);
                                break;
                            case 6:
                                cell.setCellValue(TestExportExcel.PAY_WAY46);
                                break;
                        }
                    }else if( columnNumb == 2 ){
                        cell.setCellValue(Total.getAmount());
                    }

                }else if( rowNumb < maixRowNumb-1 ){
                    if( columnNumb == 1 ){
                        cell.setCellValue("  ");
                    }else if( columnNumb == 2){
                        cell.setCellValue(928.5);
                    }
                }
                if( rowNumb < maixRowNumb-1){
                    /**
                     *         
                     */
                    cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
                }
                /**
                 *         
                 */
                cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
                cell.setCellStyle(cellStyle);
            }
        }

        //          
        for(int i = 0; i < maixRowNumb; i++) {
            //     
            if( i == 0 ){
                //  :    ,    ,     ,    
                sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 4));
            } else if( i == 1 ){
                sheet.addMergedRegion(new CellRangeAddress(1, 1, 0, 1));
                sheet.addMergedRegion(new CellRangeAddress(1, 1, 3, 4));
            }else{
                sheet.addMergedRegion(new CellRangeAddress(i, i, 3, 4));
            }
            /**
             *       
             */
            row = sheet.getRow(i);
            row.setHeightInPoints(30);
        }
        //    
        for(int j = 0; j < maixRowNumb; j++) {
            switch (j){
                case 0:
                case 1:
                case 2:
                    sheet.setColumnWidth(j, TestExportExcel.pixelWidth(100));
                    break;
                case 3:
                    sheet.setColumnWidth(j, TestExportExcel.pixelWidth(200));
                    break;
            }

        }
        wb.write(new FileOutputStream("E:\\table6.xls"));
    }

    /**
     *    
     * @param pxs
     * @return
     */
    public static short pixelWidth(int pxs) {
        short width = (short) (EXCEL_COLUMN_WIDTH_FACTOR * (pxs / UNIT_OFFSET_LENGTH));
        width += UNIT_OFFSET_MAP[(pxs % UNIT_OFFSET_LENGTH)];
        return width;
    }
}