修改后的PoiUtils 20180730

用到的版本


          org.apache.poi
          poi
          3.9
      

以下是正文

 

package com.zh.parking.utils;


import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.CellRangeAddress;
import org.apache.poi.ss.usermodel.*;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.List;

public class PoiUtils {
    private Workbook workBook;

    private CellStyle bigTilteStyle;
    private CellStyle titleStyle;
    private CellStyle textStyle;

    /*private String address;

    // 创建对象的时候设置路径和文件名称 屏蔽无参构造
    public PoiUtils(String address) {
        this.address = address;
        workBook = new HSSFWorkbook();
    }*/
    public PoiUtils(){
        workBook = new HSSFWorkbook();
        bigTilteStyle = bigTilteStyle(workBook);
        titleStyle = titleStyle(workBook);
        textStyle = textStyle(workBook);
    }

    /**
     *
     * @param mergeCell 合并的单元格数量
     * @param titleColumnData 列名数据
     * @param ShowData	 真实数据
     * @param columnWidth 列宽
     * @param title 题头
     * @return Workbook  可以直接write的excel对象          
     * @throws Exception
     */
    public Workbook mainMethod(Integer mergeCell, String[] titleColumnData, List ShowData, Integer columnWidth,
                           String title) throws Exception {
        int rowNo = 0;
        int colNo = 0;
        int defaultColumnWidth = 30;
        Row row = null;
        Cell cell = null;
        if (columnWidth != null) {
            defaultColumnWidth = columnWidth;
        }
        if (mergeCell == null || titleColumnData == null || ShowData == null || title == null) {
            throw new Exception("必要参数不能为空");
        }
        if (mergeCell <= 0) {
            throw new Exception("第一个参数不能为负数");
        }
        // 创建表
        Sheet sheet = workBook.createSheet();
        sheet.setColumnWidth(0, defaultColumnWidth * 256); // 设置列宽
        row = sheet.createRow(rowNo);
        row.setHeightInPoints(40);
        sheet.addMergedRegion(new CellRangeAddress(rowNo, rowNo, 0, 6)); // 合并单元格,新对象,不会覆盖合并的那些单元格,只是遮住
        rowNo++;
        // 设定合并单元格的内容与样式
        cell = row.createCell(0);
        cell.setCellValue(title);
        cell.setCellStyle(bigTilteStyle);

        // 写标题
        row = sheet.createRow(rowNo++);
        row.setHeightInPoints(28); // 设置行高
        // 加样式
        for (int i = 0; i < titleColumnData.length; i++) {
            cell = row.createCell(i);
            cell.setCellValue(titleColumnData[i]);
            cell.setCellStyle(titleStyle); // 绑定样式
        }
        // 写数据
        for (Object obj : ShowData) {
            colNo = 0; // 初始化
            row = sheet.createRow(rowNo++);
            row.setHeightInPoints(21);
            String[] filedNames = this.getFiledName(obj);
            for (String string : filedNames) {
                cell = row.createCell(colNo++);
                if (this.getFieldValueByName(string, obj) ==null) {
                    continue;
                }
                cell.setCellValue(this.getFieldValueByName(string, obj).toString());
                cell.setCellStyle(textStyle);
            }
        }
        return workBook;
    }

    // 大标题样式
    private CellStyle bigTilteStyle(Workbook wb) {
        // 创建一个单元格样式对象
        CellStyle curStyle = wb.createCellStyle();
        curStyle.setAlignment(CellStyle.ALIGN_CENTER); // 横向居中
        curStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER); // 纵向居中

        Font curFont = wb.createFont(); // 创建字体对象
        curFont.setFontName("华文隶书"); // 设置字体
        curFont.setFontHeightInPoints((short) 30); // 设置字体大小

        curStyle.setFont(curFont); // 将字体对象绑定到样式对象上

        return curStyle;
    }

    // 标题样式
    private CellStyle titleStyle(Workbook wb) {
        // 创建一个单元格样式对象
        CellStyle curStyle = wb.createCellStyle();
        curStyle.setAlignment(CellStyle.ALIGN_CENTER); // 横向居中
        curStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER); // 纵向居中

        Font curFont = wb.createFont(); // 创建字体对象
        curFont.setFontName("微软雅黑"); // 设置字体
        curFont.setFontHeightInPoints((short) 12); // 设置字体大小

        curStyle.setFont(curFont); // 将字体对象绑定到样式对象上

        // 画线
        curStyle.setBorderTop(CellStyle.BORDER_THIN); // 细实线
        curStyle.setBorderBottom(CellStyle.BORDER_THIN);
        curStyle.setBorderLeft(CellStyle.BORDER_THIN);
        curStyle.setBorderRight(CellStyle.BORDER_THIN);

        return curStyle;
    }

    // 文本样式
    private CellStyle textStyle(Workbook wb) {
        CellStyle xStyle = wb.createCellStyle();
        Font xFont = wb.createFont();
        xStyle.setFont(xFont);

        xStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER); // 纵向居中

        // 画线
        xStyle.setBorderTop(CellStyle.BORDER_THIN); // 细实线
        xStyle.setBorderBottom(CellStyle.BORDER_THIN);
        xStyle.setBorderLeft(CellStyle.BORDER_THIN);
        xStyle.setBorderRight(CellStyle.BORDER_THIN);

        return xStyle;
    }

    /**
     * 获取属性名数组
     */
    public String[] getFiledName(Object o) {
        Field[] fields = o.getClass().getDeclaredFields();
        String[] fieldNames = new String[fields.length];
        for (int i = 0; i < fields.length; i++) {
            System.out.println(fields[i].getType());
            fieldNames[i] = fields[i].getName();
        }
        return fieldNames;
    }

    /*
     * 根据属性名获取属性值
     */
    public Object getFieldValueByName(String fieldName, Object o) {
        try {
            String firstLetter = fieldName.substring(0, 1).toUpperCase();
            String getter = "get" + firstLetter + fieldName.substring(1);
            Method method = o.getClass().getMethod(getter, new Class[] {});
            Object value = method.invoke(o, new Object[] {});
            return value;
        } catch (Exception e) {

            return null;
        }
    }

}

 

你可能感兴趣的:(demo实例)