POI (工具)

发布于 2019-01-17  1.24k 次阅读




import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.formula.functions.T;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.ss.util.CellRangeAddressList;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.lang.reflect.Field;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 需要引入的约束:
 *         <dependency>
 *             <groupId>org.apache.poi</groupId>
 *             <artifactId>poi</artifactId>
 *             <version>3.9</version>
 *         </dependency>
 *
 *         <!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
 *         <dependency>
 *             <groupId>org.apache.poi</groupId>
 *             <artifactId>poi-ooxml</artifactId>
 *             <version>3.9</version>
 *         </dependency>
 *
 *         <!-- https://mvnrepository.com/artifact/org.apache.poi/poi-excelant -->
 *         <dependency>
 *             <groupId>org.apache.poi</groupId>
 *             <artifactId>poi-excelant</artifactId>
 *             <version>3.9</version>
 *         </dependency>
 *
 *         <!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
 *         <dependency>
 *             <groupId>org.apache.poi</groupId>
 *             <artifactId>poi-ooxml</artifactId>
 *             <version>3.9</version>
 *         </dependency>
 *
 *         <!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml-schemas -->
 *         <dependency>
 *             <groupId>org.apache.poi</groupId>
 *             <artifactId>poi-ooxml-schemas</artifactId>
 *             <version>3.9</version>
 *         </dependency>
 *
 *         <!-- https://mvnrepository.com/artifact/org.apache.poi/poi-scratchpad -->
 *         <dependency>
 *             <groupId>org.apache.poi</groupId>
 *             <artifactId>poi-scratchpad</artifactId>
 *             <version>3.9</version>
 *         </dependency>
 *
 *         <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
 *         <dependency>
 *             <groupId>org.springframework</groupId>
 *             <artifactId>spring-webmvc</artifactId>
 *             <version>5.1.2.RELEASE</version>
 *         </dependency>
 *
 *   <!--  https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
 *         <dependency>
 *             <groupId>javax.servlet</groupId>
 *             <artifactId>javax.servlet-api</artifactId>
 *             <version>4.0.1</version>
 *             <scope>provided</scope>
 *         </dependency>
 */

/**
 * POI 工具类:
 *      功能: 对Excle进行写入操作,读取操作
 */
public class ExcleUtils<T> {

    //文件引出路径
    private String url;

    //文件引出到外部的名字
    private String name;

    //sheet的名字
    private String sheetName;

    //文件名
    private String fileName;

    //标题名称
    private String titleName;

    //标题高度
    private int titleHeight;

    //行数
    private Integer columnNumber;

    //列宽
    private Integer[] columnWidth;

    //列名
    private String[] columnName;

    //数据列表
    private String[][] dataList;

    //字体类型
    private String fontType;

    //字的大小
    private short fontSize;

    //创建第1行,也就是表头 的高度
    private int firstLineHeigit;

    //表头的字体样式
    private String headerFontType;

    //表头的字体大小
    private short headerFontSize;


    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getUrl() {
        return url;
    }

    public void setTitleHeight(int titleHeight) {
        this.titleHeight = titleHeight;
    }

    public void setFontSize(short fontSize) {
        this.fontSize = fontSize;
    }

    public void setFirstLineHeigit(int firstLineHeigit) {
        this.firstLineHeigit = firstLineHeigit;
    }

    public void setHeaderFontSize(short headerFontSize) {
        this.headerFontSize = headerFontSize;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public short getHeaderFontSize() {
        return headerFontSize;
    }

    public void setHeaderFontSize(Short headerFontSize) {
        this.headerFontSize = headerFontSize;
    }

    public String getHeaderFontType() {
        return headerFontType;
    }

    public void setHeaderFontType(String headerFontType) {
        this.headerFontType = headerFontType;
    }

    public String getSheetName() {
        return sheetName;
    }

    public void setSheetName(String sheetName) {
        this.sheetName = sheetName;
    }

    public String getFileName() {
        return fileName;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }

    public String getTitleName() {
        return titleName;
    }

    public void setTitleName(String titleName) {
        this.titleName = titleName;
    }

    public Integer getColumnNumber() {
        return columnNumber;
    }

    public void setColumnNumber(Integer columnNumber) {
        this.columnNumber = columnNumber;
    }

    public Integer[] getColumnWidth() {
        return columnWidth;
    }

    public void setColumnWidth(Integer[] columnWidth) {
        this.columnWidth = columnWidth;
    }

    public String[] getColumnName() {
        return columnName;
    }

    public void setColumnName(String[] columnName) {
        this.columnName = columnName;
    }

    public String[][] getDataList() {
        return dataList;
    }

    public void setDataList(String[][] dataList) {
        this.dataList = dataList;
    }

    public Integer getTitleHeight() {
        return titleHeight;
    }

    public void setTitleHeight(Integer titleHeight) {
        this.titleHeight = titleHeight;
    }

    public String getFontType() {
        return fontType;
    }

    public void setFontType(String fontType) {
        this.fontType = fontType;
    }

    public Short getFontSize() {
        return fontSize;
    }

    public void setFontSize(Short fontSize) {
        this.fontSize = fontSize;
    }

    public Integer getFirstLineHeigit() {
        return firstLineHeigit;
    }

    public void setFirstLineHeigit(Integer firstLineHeigit) {
        this.firstLineHeigit = firstLineHeigit;
    }

    public ExcleUtils() {}

    public ExcleUtils(String excelPath){
        this.excelPath = excelPath;
    }
    Class<T> clazz;
    public ExcleUtils(Class<T> clazz) {
        this.clazz = clazz;
    }

    /**
     * 还原设定(其实是重新new一个新的对象并返回)
     * @return
     */
    public ExcleUtils RestoreSettings(){
        ExcleUtils instance = new  ExcleUtils(this.excelPath);
        return instance;
    }


    // 调用此方法 将 数据写入到Excle中
    public void POICreate(ExcleUtils excleUtils,HttpServletResponse response) throws IOException {

        if (columnNumber == columnWidth.length&& columnWidth.length == columnName.length) {
            // 第一步,创建一个webbook,对应一个Excel文件
            HSSFWorkbook wb = new HSSFWorkbook();
            // 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet
            HSSFSheet sheet = wb.createSheet(sheetName);
            //行数 遍历,将所有的行变成一样
            for (int i = 0; i < columnNumber; i++){
                for (int j = 0; j <= i; j++){
                    if (i == j){
                        sheet.setColumnWidth(i, columnWidth[j] * 256); // 单独设置每列的宽
                    }
                }
            }

            // 创建第0行 也就是标题
            HSSFRow row1 = sheet.createRow((int) 0);
            //设置标题高度:不设置默认为50, 设置则为设置的高度
            if(titleHeight == 0){
                row1.setHeightInPoints(50);// 设备标题的高度
            }else {
                row1.setHeightInPoints(titleHeight);// 设备标题的高度
            }
            // 第三步创建标题的单元格样式style2以及字体样式headerFont1
            HSSFCellStyle style2 = wb.createCellStyle();
            style2.setAlignment(HSSFCellStyle.ALIGN_CENTER);//水平居中
            style2.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//垂直居中
            style2.setFillForegroundColor(HSSFColor.LIGHT_TURQUOISE.index); //背景浅绿蓝
            style2.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); // 填充模式 ==> 坚实地

            HSSFFont headerFont1 = (HSSFFont) wb.createFont(); // 创建字体样式
            headerFont1.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); // 字体加粗
            //字体类型: 默认为黑体
            if(fontType == null){
                headerFont1.setFontName("黑体"); // 设置字体类型
            }else {
                headerFont1.setFontName(fontType);
            }
            //字体大小: 默认为15
            if(fontSize == 0){
                headerFont1.setFontHeightInPoints((short) 15); // 设置字体大小
            }else {
                headerFont1.setFontHeightInPoints(fontSize);
            }
            style2.setFont(headerFont1); // 为标题样式设置字体样式

            HSSFCell cell1 = row1.createCell(0);// 创建标题第一列
            sheet.addMergedRegion(new CellRangeAddress(0, 0, 0,columnNumber - 1)); // 合并列标题
            cell1.setCellValue(titleName); // 设置值标题
            cell1.setCellStyle(style2); // 设置标题样式

            // 创建第1行 也就是表头
            // 设置表头高度 默认为40
            HSSFRow row = sheet.createRow((int) 1);
            if(firstLineHeigit == 0){
                row.setHeightInPoints(40);// 设置表头高度
            }else {
                row.setHeightInPoints(firstLineHeigit);// 设置表头高度
            }

            // 第四步,创建表头单元格样式 以及表头的字体样式
            HSSFCellStyle style = wb.createCellStyle();
            style.setWrapText(true);// 设置自动换行
            style.setAlignment(HSSFCellStyle.ALIGN_CENTER);//水平居中
            style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); // 创建一个居中格式

            //背景颜色
            style.setBottomBorderColor(HSSFColor.BLACK.index);
            // 设置边框
            style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
            style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
            style.setBorderRight(HSSFCellStyle.BORDER_THIN);
            style.setBorderTop(HSSFCellStyle.BORDER_THIN);

            HSSFFont headerFont = (HSSFFont) wb.createFont(); // 创建字体样式
            headerFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); // 字体加粗
            if(headerFontType == null){
                headerFont.setFontName("黑体"); // 设置字体类型
            }else {
                headerFont.setFontName(headerFontType); // 设置字体类型
            }
            if(headerFontSize == 0){
                headerFont.setFontHeightInPoints((short) 10); // 设置字体大小
            }else {
                headerFont.setFontHeightInPoints(headerFontSize);// 设置字体大小
            }
            style.setFont(headerFont); // 为标题样式设置字体样式
            // 第四.一步,创建表头的列
            for (int i = 0; i < columnNumber; i++){
                HSSFCell cell = row.createCell(i);
                cell.setCellValue(columnName[i]);
                cell.setCellStyle(style);
            }
            for (int i = 0; i < dataList.length; i++){
                row = sheet.createRow((int) i + 2);
                // 为数据内容设置特点新单元格样式1 自动换行 上下居中
                HSSFCellStyle newline = wb.createCellStyle();
                newline.setWrapText(true);// 设置自动换行
                newline.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); // 创建一个居中格式

                // 设置边框
                newline.setBottomBorderColor(HSSFColor.BLACK.index);
                newline.setBorderBottom(HSSFCellStyle.BORDER_THIN);
                newline.setBorderLeft(HSSFCellStyle.BORDER_THIN);
                newline.setBorderRight(HSSFCellStyle.BORDER_THIN);
                newline.setBorderTop(HSSFCellStyle.BORDER_THIN);

                // 为数据内容设置特点新单元格样式2 自动换行 上下居中左右也居中
                HSSFCellStyle newline2 = wb.createCellStyle();
                newline2.setWrapText(true);// 设置自动换行
                newline2.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); // 创建一个上下居中格式
                newline2.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 左右居中

                // 设置边框
                newline2.setBottomBorderColor(HSSFColor.BLACK.index);
                newline2.setBorderBottom(HSSFCellStyle.BORDER_THIN);
                newline2.setBorderLeft(HSSFCellStyle.BORDER_THIN);
                newline2.setBorderRight(HSSFCellStyle.BORDER_THIN);
                newline2.setBorderTop(HSSFCellStyle.BORDER_THIN);
                HSSFCell datacell = null;
                for (int j = 0; j < columnNumber; j++){
                    datacell = row.createCell(j);
                    datacell.setCellValue(dataList[i][j]);
                    datacell.setCellStyle(newline2);
                }
            }

            // 第六步,将文件存到浏览器设置的下载位置
            String filename = fileName + ".xls";
            response.setContentType("application/ms-excel;charset=UTF-8");
            response.setHeader("Content-Disposition", "attachment;filename="
                    .concat(String.valueOf(URLEncoder.encode(filename, "UTF-8"))));
            OutputStream out = response.getOutputStream();
            try{
                wb.write(out);// 将数据写出去
                String str = "导出" + fileName + "成功!";
                System.out.println(str);
            } catch (Exception e) {
                e.printStackTrace();
                String str1 = "导出" + fileName + "失败!";
                System.out.println(str1);
            } finally {
                out.close();
            }
        }else {
            System.out.println("列数目长度名称三个数组长度要一致");
        }
    }


    // 调用此方法 将 数据写入到Excle中
    public void POICreateNo(ExcleUtils excleUtils){
        if (columnNumber == columnWidth.length && columnWidth.length == columnName.length){
            // 第一步,创建一个webbook,对应一个Excel文件
            HSSFWorkbook wb = new HSSFWorkbook();
            // 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet
            HSSFSheet sheet = wb.createSheet(sheetName);
            // sheet.setDefaultColumnWidth(15); //统一设置列宽
            for (int i = 0; i < columnNumber; i++){
                for (int j = 0; j <= i; j++){
                    if (i == j){
                        sheet.setColumnWidth(i, columnWidth[j] * 256); // 单独设置每列的宽
                    }
                }
            }

            // 创建第0行 也就是标题
            HSSFRow row1 = sheet.createRow((int) 0);
            if(titleHeight == 0){
                row1.setHeightInPoints(50);// 设备标题的高度
            }else{
                row1.setHeightInPoints(titleHeight);// 设备标题的高度
            }
            // 第三步创建标题的单元格样式style2以及字体样式headerFont1
            HSSFCellStyle style2 = wb.createCellStyle();
            style2.setAlignment(HSSFCellStyle.ALIGN_CENTER);
            style2.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
            style2.setFillForegroundColor(HSSFColor.LIGHT_TURQUOISE.index);
            style2.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
            HSSFFont headerFont1 = (HSSFFont) wb.createFont(); // 创建字体样式
            headerFont1.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); // 字体加粗
            if(fontType == null){
                headerFont1.setFontName("黑体"); // 设置字体类型
            }else {
                headerFont1.setFontName(fontType); // 设置字体类型
            }
            if(fontSize == 0){
                headerFont1.setFontHeightInPoints((short) 15); // 设置字体大小
            }else {
                headerFont1.setFontHeightInPoints(fontSize); // 设置字体大小
            }
            style2.setFont(headerFont1); // 为标题样式设置字体样式

            HSSFCell cell1 = row1.createCell(0);// 创建标题第一列
            sheet.addMergedRegion(new CellRangeAddress(0, 0, 0,columnNumber - 1)); // 合并第0到第17列
            cell1.setCellValue(titleName); // 设置值标题
            cell1.setCellStyle(style2); // 设置标题样式

            // 创建第1行 也就是表头
            HSSFRow row = sheet.createRow((int) 1);
            if(firstLineHeigit == 0){
                row.setHeightInPoints(37);// 设置表头高度
            }else {
                row.setHeightInPoints(firstLineHeigit);// 设置表头高度
            }

            // 第四步,创建表头单元格样式 以及表头的字体样式
            HSSFCellStyle style = wb.createCellStyle();
            style.setWrapText(true);// 设置自动换行
            style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
            style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); // 创建一个居中格式

            style.setBottomBorderColor(HSSFColor.BLACK.index);
            style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
            style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
            style.setBorderRight(HSSFCellStyle.BORDER_THIN);
            style.setBorderTop(HSSFCellStyle.BORDER_THIN);

            HSSFFont headerFont = (HSSFFont) wb.createFont(); // 创建字体样式
            headerFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); // 字体加粗
            if(headerFontType == null){
                headerFont.setFontName("黑体"); // 设置字体类型
            }else {
                headerFont.setFontName(headerFontType); // 设置字体类型
            }
            if(headerFontSize == 0){
                headerFont.setFontHeightInPoints((short) 10); // 设置字体大小
            }else {
                headerFont.setFontHeightInPoints(headerFontSize); // 设置字体大小
            }
            style.setFont(headerFont); // 为标题样式设置字体样式

            // 第四.一步,创建表头的列
            for (int i = 0; i < columnNumber; i++){
                HSSFCell cell = row.createCell(i);
                cell.setCellValue(columnName[i]);
                cell.setCellStyle(style);
            }

            // 第五步,创建单元格,并设置值
            for (int i = 0; i < dataList.length; i++){
                row = sheet.createRow((int) i + 2);
                // 为数据内容设置特点新单元格样式1 自动换行 上下居中
                HSSFCellStyle zidonghuanhang = wb.createCellStyle();
                zidonghuanhang.setWrapText(true);// 设置自动换行
                zidonghuanhang
                        .setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); // 创建一个居中格式

                // 设置边框
                zidonghuanhang.setBottomBorderColor(HSSFColor.BLACK.index);
                zidonghuanhang.setBorderBottom(HSSFCellStyle.BORDER_THIN);
                zidonghuanhang.setBorderLeft(HSSFCellStyle.BORDER_THIN);
                zidonghuanhang.setBorderRight(HSSFCellStyle.BORDER_THIN);
                zidonghuanhang.setBorderTop(HSSFCellStyle.BORDER_THIN);

                // 为数据内容设置特点新单元格样式2 自动换行 上下居中左右也居中
                HSSFCellStyle zidonghuanhang2 = wb.createCellStyle();
                zidonghuanhang2.setWrapText(true);// 设置自动换行
                zidonghuanhang2
                        .setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); // 创建一个上下居中格式
                zidonghuanhang2.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 左右居中

                // 设置边框
                zidonghuanhang2.setBottomBorderColor(HSSFColor.BLACK.index);
                zidonghuanhang2.setBorderBottom(HSSFCellStyle.BORDER_THIN);
                zidonghuanhang2.setBorderLeft(HSSFCellStyle.BORDER_THIN);
                zidonghuanhang2.setBorderRight(HSSFCellStyle.BORDER_THIN);
                zidonghuanhang2.setBorderTop(HSSFCellStyle.BORDER_THIN);
                HSSFCell datacell = null;
                for (int j = 0; j < columnNumber; j++)
                {
                    datacell = row.createCell(j);
                    datacell.setCellValue(dataList[i][j]);
                    datacell.setCellStyle(zidonghuanhang2);
                }
            }
            // 第六步,将文件存到指定位置
            try {
                FileOutputStream fout = new FileOutputStream(url + name + ".xls");
                wb.write(fout);
                String str = "导出" + fileName + "成功!";
                System.out.println(str);
                fout.close();
            } catch (Exception e) {
                e.printStackTrace();
                String str1 = "导出" + fileName + "失败!";
                System.out.println(str1);
            }
        } else {
            System.out.println("列数目长度名称三个数组长度要一致");
        }
    }
























    //%%%%%%%%-------常量部分 开始----------%%%%%%%%%
    /**
     * 默认的开始读取的行位置为第一行(索引值为0)
     */
    private final static int READ_START_POS = 0;

    /**
     * 默认结束读取的行位置为最后一行(索引值=0,用负数来表示倒数第n行)
     */
    private final static int READ_END_POS = 0;

    /**
     * 默认Excel内容的开始比较列位置为第一列(索引值为0)
     */
    private final static int COMPARE_POS = 0;

    /**
     * 默认多文件合并的时需要做内容比较(相同的内容不重复出现)
     */
    private final static boolean NEED_COMPARE = true;

    /**
     * 默认多文件合并的新文件遇到名称重复时,进行覆盖
     */
    private final static boolean NEED_OVERWRITE = true;

    /**
     * 默认只操作一个sheet
     */
    private final static boolean ONLY_ONE_SHEET = true;

    /**
     * 默认读取第一个sheet中(只有当ONLY_ONE_SHEET = true时有效)
     */
    private final static int SELECTED_SHEET = 0;

    /**
     * 默认从第一个sheet开始读取(索引值为0)
     */
    private final static int READ_START_SHEET= 0;

    /**
     * 默认在最后一个sheet结束读取(索引值=0,用负数来表示倒数第n行)
     */
    private final static int READ_END_SHEET = 0;

    /**
     * 默认打印各种信息
     */
    private final static boolean PRINT_MSG = true;

    //%%%%%%%%-------常量部分 结束----------%%%%%%%%%


    //%%%%%%%%-------字段部分 开始----------%%%%%%%%%
    /**
     * Excel文件路径
     */
    private String excelPath = "data.xlsx";

    /**
     * 设定开始读取的位置,默认为0
     */
    private int startReadPos = READ_START_POS;

    /**
     * 设定结束读取的位置,默认为0,用负数来表示倒数第n行
     */
    private int endReadPos = READ_END_POS;

    /**
     * 设定开始比较的列位置,默认为0
     */
    private int comparePos = COMPARE_POS;

    /**
     *  设定汇总的文件是否需要替换,默认为true
     */
    private boolean isOverWrite = NEED_OVERWRITE;

    /**
     *  设定是否需要比较,默认为true(仅当不覆写目标内容是有效,即isOverWrite=false时有效)
     */
    private boolean isNeedCompare = NEED_COMPARE;

    /**
     * 设定是否只操作第一个sheet
     */
    private boolean onlyReadOneSheet = ONLY_ONE_SHEET;

    /**
     * 设定操作的sheet在索引值
     */
    private int selectedSheetIdx =SELECTED_SHEET;

    /**
     * 设定操作的sheet的名称
     */
    private String selectedSheetName = "";

    /**
     * 设定开始读取的sheet,默认为0
     */
    private int startSheetIdx = READ_START_SHEET;

    /**
     * 设定结束读取的sheet,默认为0,用负数来表示倒数第n行
     */
    private int endSheetIdx = READ_END_SHEET;

    /**
     * 设定是否打印消息
     */
    private boolean printMsg = PRINT_MSG;


    //%%%%%%%%-------字段部分 结束----------%%%%%%%%%






    /**
     * 自动根据文件扩展名,调用对应的读取方法
     * @throws IOException
     */
    public List<Row> readExcel() throws IOException{
        return readExcel(this.excelPath);
    }

    /**
     * 自动根据文件扩展名,调用对应的读取方法
     * @throws IOException
     */
    public List<Row> readExcel(String xlsPath) throws IOException{

        //扩展名为空时,
        if (xlsPath.equals("")){
            throw new IOException("文件路径不能为空!");
        }else{
            File file = new File(xlsPath);
            if(!file.exists()){
                throw new IOException("文件不存在!");
            }
        }

        //获取扩展名
        String ext = xlsPath.substring(xlsPath.lastIndexOf(".")+1);

        try {

            if("xls".equals(ext)){              //使用xls方式读取
                return readExcel_xls(xlsPath);
            }else if("xlsx".equals(ext)){       //使用xlsx方式读取
                return readExcel_xlsx(xlsPath);
            }else{                                  //依次尝试xls、xlsx方式读取
                out("您要操作的文件没有扩展名,正在尝试以xls方式读取...");
                try{
                    return readExcel_xls(xlsPath);
                } catch (IOException e1) {
                    out("尝试以xls方式读取,结果失败!,正在尝试以xlsx方式读取...");
                    try{
                        return readExcel_xlsx(xlsPath);
                    } catch (IOException e2) {
                        out("尝试以xls方式读取,结果失败!\n请您确保您的文件是Excel文件,并且无损,然后再试。");
                        throw e2;
                    }
                }
            }
        } catch (IOException e) {
            throw e;
        }
    }

    /**
     * 自动根据文件扩展名,调用对应的写入方法
     * @throws IOException
     */
    public void writeExcel(List<Row> rowList) throws IOException{
        writeExcel(rowList,excelPath);
    }

    /**
     * 自动根据文件扩展名,调用对应的写入方法
     * @throws IOException
     */
    public void writeExcel(List<Row> rowList, String xlsPath) throws IOException {

        //扩展名为空时,
        if (xlsPath.equals("")){
            throw new IOException("文件路径不能为空!");
        }

        //获取扩展名
        String ext = xlsPath.substring(xlsPath.lastIndexOf(".")+1);

        try {

            if("xls".equals(ext)){              //使用xls方式写入
                writeExcel_xls(rowList,xlsPath);
            }else if("xlsx".equals(ext)){       //使用xlsx方式写入
                writeExcel_xlsx(rowList,xlsPath);
            }else{                                  //依次尝试xls、xlsx方式写入
                out("您要操作的文件没有扩展名,正在尝试以xls方式写入...");
                try{
                    writeExcel_xls(rowList,xlsPath);
                } catch (IOException e1) {
                    out("尝试以xls方式写入,结果失败!,正在尝试以xlsx方式读取...");
                    try{
                        writeExcel_xlsx(rowList,xlsPath);
                    } catch (IOException e2) {
                        out("尝试以xls方式写入,结果失败!\n请您确保您的文件是Excel文件,并且无损,然后再试。");
                        throw e2;
                    }
                }
            }
        } catch (IOException e) {
            throw e;
        }
    }

    /**
     * 修改Excel(97-03版,xls格式)
     * @throws IOException
     */
    public void writeExcel_xls(List<Row> rowList, String dist_xlsPath) throws IOException {
        writeExcel_xls(rowList, excelPath,dist_xlsPath);
    }

    /**
     * 修改Excel(97-03版,xls格式)
     * @throws IOException
     */
    public void writeExcel_xls(List<Row> rowList, String src_xlsPath, String dist_xlsPath) throws IOException {

        // 判断文件路径是否为空
        if (dist_xlsPath == null || dist_xlsPath.equals("")) {
            out("文件路径不能为空");
            throw new IOException("文件路径不能为空");
        }
        // 判断文件路径是否为空
        if (src_xlsPath == null || src_xlsPath.equals("")) {
            out("文件路径不能为空");
            throw new IOException("文件路径不能为空");
        }

        // 判断列表是否有数据,如果没有数据,则返回
        if (rowList == null || rowList.size() == 0) {
            out("文档为空");
            return;
        }

        try {
            HSSFWorkbook wb = null;

            // 判断文件是否存在
            File file = new File(dist_xlsPath);
            if (file.exists()) {
                // 如果复写,则删除后
                if (isOverWrite) {
                    file.delete();
                    // 如果文件不存在,则创建一个新的Excel
                    // wb = new HSSFWorkbook();
                    // wb.createSheet("Sheet1");
                    wb = new HSSFWorkbook(new FileInputStream(src_xlsPath));
                } else {
                    // 如果文件存在,则读取Excel
                    wb = new HSSFWorkbook(new FileInputStream(file));
                }
            } else {
                // 如果文件不存在,则创建一个新的Excel
                // wb = new HSSFWorkbook();
                // wb.createSheet("Sheet1");
                wb = new HSSFWorkbook(new FileInputStream(src_xlsPath));
            }

            // 将rowlist的内容写到Excel中
            writeExcel(wb, rowList, dist_xlsPath);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 修改Excel(97-03版,xls格式)
     * @throws IOException
     */
    public void writeExcel_xlsx(List<Row> rowList, String dist_xlsPath) throws IOException {
        writeExcel_xls(rowList, excelPath , dist_xlsPath);
    }

    /**
     * 修改Excel(2007版,xlsx格式)
     * @throws IOException
     */
    public void writeExcel_xlsx(List<Row> rowList, String src_xlsPath, String dist_xlsPath) throws IOException {

        // 判断文件路径是否为空
        if (dist_xlsPath == null || dist_xlsPath.equals("")) {
            out("文件路径不能为空");
            throw new IOException("文件路径不能为空");
        }
        // 判断文件路径是否为空
        if (src_xlsPath == null || src_xlsPath.equals("")) {
            out("文件路径不能为空");
            throw new IOException("文件路径不能为空");
        }

        // 判断列表是否有数据,如果没有数据,则返回
        if (rowList == null || rowList.size() == 0) {
            out("文档为空");
            return;
        }

        try {
            // 读取文档
            XSSFWorkbook wb = null;

            // 判断文件是否存在
            File file = new File(dist_xlsPath);
            if (file.exists()) {
                // 如果复写,则删除后
                if (isOverWrite) {
                    file.delete();
                    // 如果文件不存在,则创建一个新的Excel
                    // wb = new XSSFWorkbook();
                    // wb.createSheet("Sheet1");
                    wb = new XSSFWorkbook(new FileInputStream(src_xlsPath));
                } else {
                    // 如果文件存在,则读取Excel
                    wb = new XSSFWorkbook(new FileInputStream(file));
                }
            } else {
                // 如果文件不存在,则创建一个新的Excel
                // wb = new XSSFWorkbook();
                // wb.createSheet("Sheet1");
                wb = new XSSFWorkbook(new FileInputStream(src_xlsPath));
            }
            // 将rowlist的内容添加到Excel中
            writeExcel(wb, rowList, dist_xlsPath);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * //读取Excel 2007版,xlsx格式
     * @throws IOException
     */
    public List<Row> readExcel_xlsx() throws IOException {
        return readExcel_xlsx(excelPath);
    }

    /**
     * //读取Excel 2007版,xlsx格式
     * @throws Exception
     */
    public List<Row> readExcel_xlsx(String xlsPath) throws IOException {
        // 判断文件是否存在
        File file = new File(xlsPath);
        if (!file.exists()) {
            throw new IOException("文件名为" + file.getName() + "Excel文件不存在!");
        }

        XSSFWorkbook wb = null;
        List<Row> rowList = new ArrayList<Row>();
        try {
            FileInputStream fis = new FileInputStream(file);
            // 去读Excel
            wb = new XSSFWorkbook(fis);

            // 读取Excel 2007版,xlsx格式
            rowList = readExcel(wb);

        } catch (IOException e) {
            e.printStackTrace();
        }
        return rowList;
    }

    /***
     * 读取Excel(97-03版,xls格式)
     */
    public List<Row> readExcel_xls() throws IOException {
        return readExcel_xls(excelPath);
    }

    /***
     * 读取Excel(97-03版,xls格式)
     */
    public List<Row> readExcel_xls(String xlsPath) throws IOException {

        // 判断文件是否存在
        File file = new File(xlsPath);
        if (!file.exists()) {
            throw new IOException("文件名为" + file.getName() + "Excel文件不存在!");
        }

        HSSFWorkbook wb = null;// 用于Workbook级的操作,创建、删除Excel
        List<Row> rowList = new ArrayList<Row>();

        try {
            // 读取Excel
            wb = new HSSFWorkbook(new FileInputStream(file));

            // 读取Excel 97-03版,xls格式
            rowList = readExcel(wb);

        } catch (IOException e) {
            e.printStackTrace();
        }
        return rowList;
    }

    /***
     * 读取单元格的值
     * @return
     */
    private String getCellValue(Cell cell) {
        Object result = "";
        if (cell != null) {
            switch (cell.getCellType()) {
                case Cell.CELL_TYPE_STRING:
                    result = cell.getStringCellValue();
                    break;
                case Cell.CELL_TYPE_NUMERIC:
                    result = cell.getNumericCellValue();
                    break;
                case Cell.CELL_TYPE_BOOLEAN:
                    result = cell.getBooleanCellValue();
                    break;
                case Cell.CELL_TYPE_FORMULA:
                    result = cell.getCellFormula();
                    break;
                case Cell.CELL_TYPE_ERROR:
                    result = cell.getErrorCellValue();
                    break;
                case Cell.CELL_TYPE_BLANK:
                    break;
                default:
                    break;
            }
        }
        return result.toString();
    }

    /**
     * 通用读取Excel
     * @return
     */
    private List<Row> readExcel(Workbook wb) {
        List<Row> rowList = new ArrayList<Row>();

        int sheetCount = 1;//需要操作的sheet数量

        Sheet sheet = null;
        if(onlyReadOneSheet){   //只操作一个sheet
            // 获取设定操作的sheet(如果设定了名称,按名称查,否则按索引值查)
            sheet =selectedSheetName.equals("")? wb.getSheetAt(selectedSheetIdx):wb.getSheet(selectedSheetName);
        }else{                          //操作多个sheet
            sheetCount = wb.getNumberOfSheets();//获取可以操作的总数量
        }

        // 获取sheet数目
        for(int t=startSheetIdx; t<sheetCount+endSheetIdx;t++){
            // 获取设定操作的sheet
            if(!onlyReadOneSheet) {
                sheet =wb.getSheetAt(t);
            }

            //获取最后行号
            int lastRowNum = sheet.getLastRowNum();

            if(lastRowNum>0){    //如果>0,表示有数据
                out("\n开始读取名为【"+sheet.getSheetName()+"】的内容:");
            }

            Row row = null;
            // 循环读取
            for (int i = startReadPos; i <= lastRowNum + endReadPos; i++) {
                row = sheet.getRow(i);
                if (row != null) {
                    rowList.add(row);
                    out("第"+(i+1)+"行:",false);
                    // 获取每一单元格的值
                    for (int j = 0; j < row.getLastCellNum(); j++) {
                        String value = getCellValue(row.getCell(j));
                        if (!value.equals("")) {
                            out(value + " | ",false);
                        }
                    }
                    out("");
                }
            }
        }
        return rowList;
    }

    /**
     * 修改Excel,并另存为
     */
    private void writeExcel(Workbook wb, List<Row> rowList, String xlsPath) {

        if (wb == null) {
            out("操作文档不能为空!");
            return;
        }

        Sheet sheet = wb.getSheetAt(0);// 修改第一个sheet中的值

        // 如果每次重写,那么则从开始读取的位置写,否则果获取源文件最新的行。
        int lastRowNum = isOverWrite ? startReadPos : sheet.getLastRowNum() + 1;
        int t = 0;//记录最新添加的行数
        out("要添加的数据总条数为:"+rowList.size());
        for (Row row : rowList) {
            if (row == null) continue;
            // 判断是否已经存在该数据
            int pos = findInExcel(sheet, row);

            Row r = null;// 如果数据行已经存在,则获取后重写,否则自动创建新行。
            if (pos >= 0) {
                sheet.removeRow(sheet.getRow(pos));
                r = sheet.createRow(pos);
            } else {
                r = sheet.createRow(lastRowNum + t++);
            }

            //用于设定单元格样式
            CellStyle newstyle = wb.createCellStyle();

            //循环为新行创建单元格
            for (int i = row.getFirstCellNum(); i < row.getLastCellNum(); i++) {
                Cell cell = r.createCell(i);// 获取数据类型
                cell.setCellValue(getCellValue(row.getCell(i)));// 复制单元格的值到新的单元格
                // cell.setCellStyle(row.getCell(i).getCellStyle());//出错
                if (row.getCell(i) == null) continue;
                copyCellStyle(row.getCell(i).getCellStyle(), newstyle); // 获取原来的单元格样式
                cell.setCellStyle(newstyle);// 设置样式
                // sheet.autoSizeColumn(i);//自动跳转列宽度
            }
        }
        out("其中检测到重复条数为:" + (rowList.size() - t) + " ,追加条数为:"+t);

        // 统一设定合并单元格
        setMergedRegion(sheet);

        try {
            // 重新将数据写入Excel中
            FileOutputStream outputStream = new FileOutputStream(xlsPath);
            wb.write(outputStream);
            outputStream.flush();
            outputStream.close();
        } catch (Exception e) {
            out("写入Excel时发生错误! ");
            e.printStackTrace();
        }
    }

    /**
     * 查找某行数据是否在Excel表中存在,返回行数。
     * @return
     */
    private int findInExcel(Sheet sheet, Row row) {
        int pos = -1;

        try {
            // 如果覆写目标文件,或者不需要比较,则直接返回
            if (isOverWrite || !isNeedCompare) {
                return pos;
            }
            for (int i = startReadPos; i <= sheet.getLastRowNum() + endReadPos; i++) {
                Row r = sheet.getRow(i);
                if (r != null && row != null) {
                    String v1 = getCellValue(r.getCell(comparePos));
                    String v2 = getCellValue(row.getCell(comparePos));
                    if (v1.equals(v2)) {
                        pos = i;
                        break;
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return pos;
    }

    /**
     * 复制一个单元格样式到目的单元格样式
     */
    public static void copyCellStyle(CellStyle fromStyle, CellStyle toStyle) {
        toStyle.setAlignment(fromStyle.getAlignment());
        // 边框和边框颜色
        toStyle.setBorderBottom(fromStyle.getBorderBottom());
        toStyle.setBorderLeft(fromStyle.getBorderLeft());
        toStyle.setBorderRight(fromStyle.getBorderRight());
        toStyle.setBorderTop(fromStyle.getBorderTop());
        toStyle.setTopBorderColor(fromStyle.getTopBorderColor());
        toStyle.setBottomBorderColor(fromStyle.getBottomBorderColor());
        toStyle.setRightBorderColor(fromStyle.getRightBorderColor());
        toStyle.setLeftBorderColor(fromStyle.getLeftBorderColor());

        // 背景和前景
        toStyle.setFillBackgroundColor(fromStyle.getFillBackgroundColor());
        toStyle.setFillForegroundColor(fromStyle.getFillForegroundColor());

        // 数据格式
        toStyle.setDataFormat(fromStyle.getDataFormat());
        toStyle.setFillPattern(fromStyle.getFillPattern());
        // toStyle.setFont(fromStyle.getFont(null));
        toStyle.setHidden(fromStyle.getHidden());
        toStyle.setIndention(fromStyle.getIndention());// 首行缩进
        toStyle.setLocked(fromStyle.getLocked());
        toStyle.setRotation(fromStyle.getRotation());// 旋转
        toStyle.setVerticalAlignment(fromStyle.getVerticalAlignment());
        toStyle.setWrapText(fromStyle.getWrapText());

    }

    /**
     * 获取合并单元格的值
     * @return
     */
    public void setMergedRegion(Sheet sheet) {
        int sheetMergeCount = sheet.getNumMergedRegions();

        for (int i = 0; i < sheetMergeCount; i++) {
            // 获取合并单元格位置
            CellRangeAddress ca = sheet.getMergedRegion(i);
            int firstRow = ca.getFirstRow();
            if (startReadPos - 1 > firstRow) {// 如果第一个合并单元格格式在正式数据的上面,则跳过。
                continue;
            }
            int lastRow = ca.getLastRow();
            int mergeRows = lastRow - firstRow;// 合并的行数
            int firstColumn = ca.getFirstColumn();
            int lastColumn = ca.getLastColumn();
            // 根据合并的单元格位置和大小,调整所有的数据行格式,
            for (int j = lastRow + 1; j <= sheet.getLastRowNum(); j++) {
                // 设定合并单元格
                sheet.addMergedRegion(new CellRangeAddress(j, j + mergeRows, firstColumn, lastColumn));
                j = j + mergeRows;// 跳过已合并的行
            }

        }
    }


    /**
     * 打印消息,
     */
    private void out(String msg){
        if(printMsg){
            out(msg,true);
        }
    }
    /**
     * 打印消息,
     * @param msg 消息内容
     * @param tr 换行
     */
    private void out(String msg,boolean tr){
        if(printMsg){
            System.out.print(msg+(tr?"\n":""));
        }
    }

    public String getExcelPath() {
        return this.excelPath;
    }

    public void setExcelPath(String excelPath) {
        this.excelPath = excelPath;
    }

    public boolean isNeedCompare() {
        return isNeedCompare;
    }

    public void setNeedCompare(boolean isNeedCompare) {
        this.isNeedCompare = isNeedCompare;
    }

    public int getComparePos() {
        return comparePos;
    }

    public void setComparePos(int comparePos) {
        this.comparePos = comparePos;
    }

    public int getStartReadPos() {
        return startReadPos;
    }

    public void setStartReadPos(int startReadPos) {
        this.startReadPos = startReadPos;
    }

    public int getEndReadPos() {
        return endReadPos;
    }

    public void setEndReadPos(int endReadPos) {
        this.endReadPos = endReadPos;
    }

    public boolean isOverWrite() {
        return isOverWrite;
    }

    public void setOverWrite(boolean isOverWrite) {
        this.isOverWrite = isOverWrite;
    }

    public boolean isOnlyReadOneSheet() {
        return onlyReadOneSheet;
    }

    public void setOnlyReadOneSheet(boolean onlyReadOneSheet) {
        this.onlyReadOneSheet = onlyReadOneSheet;
    }

    public int getSelectedSheetIdx() {
        return selectedSheetIdx;
    }

    public void setSelectedSheetIdx(int selectedSheetIdx) {
        this.selectedSheetIdx = selectedSheetIdx;
    }

    public String getSelectedSheetName() {
        return selectedSheetName;
    }

    public void setSelectedSheetName(String selectedSheetName) {
        this.selectedSheetName = selectedSheetName;
    }

    public int getStartSheetIdx() {
        return startSheetIdx;
    }

    public void setStartSheetIdx(int startSheetIdx) {
        this.startSheetIdx = startSheetIdx;
    }

    public int getEndSheetIdx() {
        return endSheetIdx;
    }

    public void setEndSheetIdx(int endSheetIdx) {
        this.endSheetIdx = endSheetIdx;
    }

    public boolean isPrintMsg() {
        return printMsg;
    }

    public void setPrintMsg(boolean printMsg) {
        this.printMsg = printMsg;
    }




    public List<T> importExcel(String sheetName, InputStream input) {
        int maxCol = 0;
        List<T> list = new ArrayList<T>();
        try {
            HSSFWorkbook workbook = new HSSFWorkbook(input);
            HSSFSheet sheet = workbook.getSheet(sheetName);
            if (!sheetName.trim().equals("")) {
                sheet = workbook.getSheet(sheetName);// 如果指定sheet名,则取指定sheet中的内容.
            }
            if (sheet == null) {
                sheet = workbook.getSheetAt(0); // 如果传入的sheet名不存在则默认指向第1个sheet.
            }
            int rows = sheet.getPhysicalNumberOfRows();

            if (rows > 0) {// 有数据时才处理
                // Field[] allFields = clazz.getDeclaredFields();// 得到类的所有field.
                List<Field> allFields = getMappedFiled(clazz, null);

                Map<Integer, Field> fieldsMap = new HashMap<Integer, Field>();// 定义一个map用于存放列的序号和field.
                for (Field field : allFields) {
                    // 将有注解的field存放到map中.
                    if (field.isAnnotationPresent(ExcliConfig.class)) {
                        ExcliConfig attr = field
                                .getAnnotation(ExcliConfig.class);
                        int col = getExcelCol(attr.column());// 获得列号
                        maxCol = Math.max(col, maxCol);
                        // System.out.println(col + "====" + field.getName());
                        field.setAccessible(true);// 设置类的私有字段属性可访问.
                        fieldsMap.put(col, field);
                    }
                }
                for (int i = 1; i < rows; i++) {// 从第2行开始取数据,默认第一行是表头.
                    HSSFRow row = sheet.getRow(i);
                    // int cellNum = row.getPhysicalNumberOfCells();
                    // int cellNum = row.getLastCellNum();
                    int cellNum = maxCol;
                    T entity = null;
                    for (int j = 0; j < cellNum; j++) {
                        HSSFCell cell = row.getCell(j);
                        if (cell == null) {
                            continue;
                        }
                        int cellType = cell.getCellType();
                        String c = "";
                        if (cellType == HSSFCell.CELL_TYPE_NUMERIC) {
                            c = String.valueOf(cell.getNumericCellValue());
                        } else if (cellType == HSSFCell.CELL_TYPE_BOOLEAN) {
                            c = String.valueOf(cell.getBooleanCellValue());
                        } else {
                            c = cell.getStringCellValue();
                        }
                        if (c == null || c.equals("")) {
                            continue;
                        }
                        entity = (entity == null ? clazz.newInstance() : entity);// 如果不存在实例则新建.
                        // System.out.println(cells[j].getContents());
                        Field field = fieldsMap.get(j);// 从map中得到对应列的field.
                        if (field==null) {
                            continue;
                        }
                        // 取得类型,并根据对象类型设置值.
                        Class<?> fieldType = field.getType();
                        if (String.class == fieldType) {
                            field.set(entity, String.valueOf(c));
                        } else if ((Integer.TYPE == fieldType)
                                || (Integer.class == fieldType)) {
                            field.set(entity, Integer.parseInt(c));
                        } else if ((Long.TYPE == fieldType)
                                || (Long.class == fieldType)) {
                            field.set(entity, Long.valueOf(c));
                        } else if ((Float.TYPE == fieldType)
                                || (Float.class == fieldType)) {
                            field.set(entity, Float.valueOf(c));
                        } else if ((Short.TYPE == fieldType)
                                || (Short.class == fieldType)) {
                            field.set(entity, Short.valueOf(c));
                        } else if ((Double.TYPE == fieldType)
                                || (Double.class == fieldType)) {
                            field.set(entity, Double.valueOf(c));
                        } else if (Character.TYPE == fieldType) {
                            if ((c != null) && (c.length() > 0)) {
                                field.set(entity, Character
                                        .valueOf(c.charAt(0)));
                            }
                        }

                    }
                    if (entity != null) {
                        list.add(entity);
                    }
                }
            }

        } catch (IOException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        }
        return list;
    }

    /**
     * 对list数据源将其里面的数据导入到excel表单
     *
     * @param //sheetName
     *            工作表的名称
     * @param output
     *            java输出流
     */
    public boolean exportExcel(List<T> lists[], String sheetNames[],OutputStream output) {
        if (lists.length != sheetNames.length) {
            System.out.println("数组长度不一致");
            return false;
        }

        HSSFWorkbook workbook = new HSSFWorkbook();// 产生工作薄对象

        for (int ii = 0; ii < lists.length; ii++) {
            List<T> list = lists[ii];
            String sheetName = sheetNames[ii];

            List<Field> fields = getMappedFiled(clazz, null);

            HSSFSheet sheet = workbook.createSheet();// 产生工作表对象

            workbook.setSheetName(ii, sheetName);

            HSSFRow row;
            HSSFCell cell;// 产生单元格
            HSSFCellStyle style = workbook.createCellStyle();
            style.setFillForegroundColor(HSSFColor.SKY_BLUE.index);
            style.setFillBackgroundColor(HSSFColor.GREY_40_PERCENT.index);
            row = sheet.createRow(0);// 产生一行
//            HSSFFont headerFont1 = (HSSFFont) workbook.createFont(); // 创建字体样式
//            headerFont1.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); // 字体加粗

            // 写入各个字段的列头名称
            for (int i = 0; i < fields.size(); i++) {
                Field field = fields.get(i);
                ExcliConfig attr = field
                        .getAnnotation(ExcliConfig.class);
                int col = getExcelCol(attr.column());// 获得列号
                row.setHeightInPoints(attr.titleHeight());// 设备标题的高度
//                headerFont1.setFontName(attr.fontType()); // 设置字体类型
//                headerFont1.setFontHeightInPoints(attr.fontSize()); // 设置字体大小
                cell = row.createCell(col);// 创建列
                cell.setCellType(HSSFCell.CELL_TYPE_STRING);// 设置列中写入内容为String类型
                cell.setCellValue(attr.name());// 写入列名

                // 如果设置了提示信息则鼠标放上去提示.
                if (!attr.prompt().trim().equals("")) {
                    setHSSFPrompt(sheet, "", attr.prompt(), 1, 100, col, col);// 这里默认设了2-101列提示.
                }
                // 如果设置了combo属性则本列只能选择不能输入
                if (attr.combo().length > 0) {
                    setHSSFValidation(sheet, attr.combo(), 1, 100, col, col);// 这里默认设了2-101列只能选择不能输入.
                }
                cell.setCellStyle(style);
            }

            int startNo = 0;
            int endNo = list.size();
            // 写入各条记录,每条记录对应excel表中的一行
            for (int i = startNo; i < endNo; i++) {
                row = sheet.createRow(i + 1 - startNo);
                HSSFFont headerFont = (HSSFFont) workbook.createFont(); // 创建字体样式
                headerFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); // 字体加粗
                T vo = (T) list.get(i); // 得到导出对象.
                for (int j = 0; j < fields.size(); j++) {
                    Field field = fields.get(j);// 获得field.
                    field.setAccessible(true);// 设置实体类私有属性可访问
                    ExcliConfig attr = field
                            .getAnnotation(ExcliConfig.class);
                    try {
                        // 根据ExcelVOAttribute中设置情况决定是否导出,有些情况需要保持为空,希望用户填写这一列.
                        if (attr.isExport()) {
                            cell = row.createCell(getExcelCol(attr.column()));// 创建cell
                            cell.setCellType(HSSFCell.CELL_TYPE_STRING);
//                            headerFont.setFontName(attr.fontType()); // 设置字体类型
//                            headerFont.setFontHeightInPoints(attr.fontSize());
                            cell.setCellValue(field.get(vo) == null ? ""
                                    : String.valueOf(field.get(vo)));// 如果数据存在就填入,不存在填入空格.
                        }
                    } catch (IllegalArgumentException e) {
                        e.printStackTrace();
                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                    }
                }
            }
        }

        try {
            output.flush();
            workbook.write(output);
            output.close();
            return true;
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("Output is closed ");
            return false;
        }

    }

    /**
     * 对list数据源将其里面的数据导入到excel表单
     *
     * @param sheetName
     *            工作表的名称
     * @param //sheetSize
     *            每个sheet中数据的行数,此数值必须小于65536
     * @param output
     *            java输出流
     */
    public boolean exportExcel(List<T> list, String sheetName,OutputStream output) {
        List<T>[] lists = new ArrayList[1];
        lists[0] = list;

        String[] sheetNames = new String[1];
        sheetNames[0] = sheetName;

        return exportExcel(lists, sheetNames, output);
    }

    /**
     * 将EXCEL中A,B,C,D,E列映射成0,1,2,3
     *
     * @param col
     */
    public static int getExcelCol(String col) {
        col = col.toUpperCase();
        // 从-1开始计算,字母重1开始运算。这种总数下来算数正好相同。
        int count = -1;
        char[] cs = col.toCharArray();
        for (int i = 0; i < cs.length; i++) {
            count += (cs[i] - 64) * Math.pow(26, cs.length - 1 - i);
        }
        return count;
    }

    /**
     * 设置单元格上提示
     *
     * @param sheet
     *            要设置的sheet.
     * @param promptTitle
     *            标题
     * @param promptContent
     *            内容
     * @param firstRow
     *            开始行
     * @param endRow
     *            结束行
     * @param firstCol
     *            开始列
     * @param endCol
     *            结束列
     * @return 设置好的sheet.
     */
    public static HSSFSheet setHSSFPrompt(HSSFSheet sheet, String promptTitle,
                                          String promptContent, int firstRow, int endRow, int firstCol,
                                          int endCol) {
        // 构造constraint对象
        DVConstraint constraint = DVConstraint
                .createCustomFormulaConstraint("DD1");
        // 四个参数分别是:起始行、终止行、起始列、终止列
        CellRangeAddressList regions = new CellRangeAddressList(firstRow,
                endRow, firstCol, endCol);
        // 数据有效性对象
        HSSFDataValidation data_validation_view = new HSSFDataValidation(
                regions, constraint);
        data_validation_view.createPromptBox(promptTitle, promptContent);
        sheet.addValidationData(data_validation_view);
        return sheet;
    }

    /**
     * 设置某些列的值只能输入预制的数据,显示下拉框.
     *
     * @param sheet
     *            要设置的sheet.
     * @param textlist
     *            下拉框显示的内容
     * @param firstRow
     *            开始行
     * @param endRow
     *            结束行
     * @param firstCol
     *            开始列
     * @param endCol
     *            结束列
     * @return 设置好的sheet.
     */
    public static HSSFSheet setHSSFValidation(HSSFSheet sheet,
                                              String[] textlist, int firstRow, int endRow, int firstCol,
                                              int endCol) {
        // 加载下拉列表内容
        DVConstraint constraint = DVConstraint
                .createExplicitListConstraint(textlist);
        // 设置数据有效性加载在哪个单元格上,四个参数分别是:起始行、终止行、起始列、终止列
        CellRangeAddressList regions = new CellRangeAddressList(firstRow,
                endRow, firstCol, endCol);
        // 数据有效性对象
        HSSFDataValidation data_validation_list = new HSSFDataValidation(
                regions, constraint);
        sheet.addValidationData(data_validation_list);
        return sheet;
    }

    /**
     * 得到实体类所有通过注解映射了数据表的字段
     *
     * @param //map
     * @return
     */
    private List<Field> getMappedFiled(Class clazz, List<Field> fields) {
        if (fields == null) {
            fields = new ArrayList<Field>();
        }

        Field[] allFields = clazz.getDeclaredFields();// 得到所有定义字段
        // 得到所有field并存放到一个list中.
        for (Field field : allFields) {
            if (field.isAnnotationPresent(ExcliConfig.class)) {
                fields.add(field);
            }
        }
        if (clazz.getSuperclass() != null
                && !clazz.getSuperclass().equals(Object.class)) {
            getMappedFiled(clazz.getSuperclass(), fields);
        }

        return fields;
    }


























































    public static void main(String[] args) {

//        ExcleUtils eu = new ExcleUtils();
//        //从第一行开始读取
//        eu.setStartReadPos(1);
//
//        String src_xlspath = "f:\\ss.xls";
//        String dist_xlsPath = "f:\\ss.xls";
//        List<Row> rowList;
//        try {
//            rowList = eu.readExcel(src_xlspath);
//            //eu.writeExcel_xls(rowList, src_xlspath, dist_xlsPath);
//        } catch (IOException e) {
//            e.printStackTrace();
//        }



//        ExcleUtils excleUtil = new ExcleUtils();
//        excleUtil.setUrl("F:\\");
//        excleUtil.setName("ss");
//        String[] columnName = {"单号","申请时间","申请部门"};
//        excleUtil.setColumnName(columnName);
//        excleUtil.setSheetName("快购用户统计表");
//        excleUtil.setTitleName("快购会员申请数据统计表");
//        excleUtil.setFileName("快购申请统计表");
//        excleUtil.setColumnNumber(3);
//
//        Integer[] columnWidth = {10,20,30};
//        excleUtil.setColumnWidth(columnWidth);
//        excleUtil.setColumnNumber(3);
//
//        String[][] dataList = {{"001","2018-11-19","aa"},{"002","2018-11-19","bb"},
//                {"003","2018-11-19","cc"}};
//        excleUtil.setDataList(dataList);
//
//        excleUtil.POICreateNo(excleUtil);

    }

}




</pre>
<pre>

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD})
public @interface ExcliConfig {

    /**
     * 导出到Excel中的名字.
     */
    public abstract String name();

    /**
     * 配置列的名称,对应A,B,C,D....
     */
    public abstract String column();

    /**
     * 提示信息
     */
    public abstract String prompt() default "";

    /**
     * 设置只能选择不能输入的列内容.
     */
    public abstract String[] combo() default {};

    /**
     * 是否导出数据,应对需求:有时我们需要导出一份模板,这是标题需要但内容需要用户手工填写.
     */
    public abstract boolean isExport() default true;

    /**
     * 标题的高度
     */
    public abstract  int titleHeight() default 50;


    /**
     * 字体类型
     */
    public abstract String fontType() default "黑体";

    /**
     * 字体大小
     */
    public abstract short fontSize() default 15;



}</pre>
<pre>

public class Test {
    
    @ExcliConfig(name = "序号", column = "A",fontSize = 100,fontType = "宋体",titleHeight = 3000)
    private int id;

    @ExcliConfig(name = "姓名", column = "B",fontSize = 80,fontType = "宋体",titleHeight = 3000)
    private String name;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}</pre>
<pre>


import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;

public class ExcleTest {

    public static void main(String[] args){
        List<Test> list = new ArrayList<Test>();
        Test test = new Test();
        test.setId(1);
        test.setName("11");
        list.add(test);

        FileOutputStream out = null;
        try{
            out = new FileOutputStream("F:\\学生表.xls");
        }catch (FileNotFoundException e){
            e.printStackTrace();
        }

        ExcleUtils<Test> util = new ExcleUtils<Test>(Test.class);
        util.exportExcel(list,"学生ge信息",out);//导出
        System.out.println("执行完毕");


    }

}

公交车司机终于在众人的指责中将座位让给了老太太