easypoi 模板非模板导出

模板

package com.example.easypoi.excel;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.TemplateExportParams;
import org.apache.poi.ss.usermodel.*;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;


public class TemplateExcel {

    public static List<DemoEntry> getDemeEntry() {

        List<DemoEntry> list = new ArrayList<DemoEntry>();
        for (int i = 0; i < 100; i++) {
            int b = i % 2;
            if (b == 0) {
                DemoEntry dd = new DemoEntry();
                dd.setId(UUID.randomUUID().toString());
                dd.setNum(i + 0);
                Calendar calendar1 = Calendar.getInstance();//获取对日期操作的类对象
                calendar1.add(Calendar.DATE, -(i + 0));
                dd.setBirthday(calendar1.getTime());
                list.add(dd);
            } else {
                DemoEntry dd = new DemoEntry();
                dd.setId(UUID.randomUUID().toString());
                dd.setNum(i + 0);
                dd.setBirthday(null);
                list.add(dd);
            }

        }
        return list;
    }

    public static void main(String[] args) throws IOException, ParseException {
        TemplateExportParams params = new TemplateExportParams("exportTemplate/map.xls");
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("list", getDemeEntry());
        Workbook workbook = ExcelExportUtil.exportExcel(params, map);
        // 创建单元格样式
        CellStyle cellStyle = workbook.createCellStyle();
        CreationHelper createHelper = workbook.getCreationHelper();
        cellStyle.setDataFormat(createHelper.createDataFormat().getFormat("yyyy-MM-dd"));
        Sheet sheet1 = workbook.getSheet("sheet1");
        refreshSheet(cellStyle, sheet1);
        // 刷新公式
        workbook.setForceFormulaRecalculation(true);
        File savefile = new File("D:/excel/");
        if (!savefile.exists()) {
            savefile.mkdirs();
        }
        FileOutputStream fos = new FileOutputStream("D:/excel/map.xls");

        workbook.write(fos);
        fos.close();
        workbook.close();
    }

    protected static void refreshSheet(CellStyle cellStyle, Sheet sheet) throws ParseException {
        // 所有的公式拷贝到同一行
        // 如果不拷贝,公式有可能不会再计算
        for (int i = sheet.getFirstRowNum(), end = sheet.getLastRowNum(); i <= end; i++) {
            Row row = sheet.getRow(i + 1);
            if (row != null) {
                //从0开始
                Cell cell = row.getCell(1);
                if (!getValue(cell).equals("")) {
                    //System.out.println(getValue(cell));
                    // 设置单元格格式
                    cell.setCellStyle(cellStyle);
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                    // 设置日期值
                    cell.setCellValue(sdf.parse(getValue(cell)));
                }

            }
        }
    }


    private static String getValue(Cell cell) {
        if (cell == null) {
            return "";
        }
        return getValue(cell.getCellType(), cell);
    }

    private static String getValue(CellType cellType, Cell cell) {
        if (cell == null) {
            return "";
        }
        switch (cellType) {
            case BOOLEAN:
                return String.valueOf(cell.getBooleanCellValue());
            case NUMERIC: {
                double cur = cell.getNumericCellValue();

                String dataFormat = cell.getCellStyle().getDataFormatString();

                //处理日期格式问题
                if ("m/d/yy".equals(dataFormat)
                        || "yyyy\\-mm\\-dd".equals(dataFormat)
                        || "yyyy/m/d;@".equals(dataFormat)
                        || "yyyy\"年\"m\"月\"d\"日\";@".equals(dataFormat)) {
                    Date d = DateUtil.getJavaDate(cur);
                    SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
                    return f.format(d);
                }
                //处理时间格式问题
                else if ("yyyy/m/d\\ h:mm;@".equals(dataFormat)) {
                    Date d = DateUtil.getJavaDate(cur);
                    SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    return f.format(d);
                } else if ("General".equals(dataFormat)) {
                    long longVal = Math.round(cur);
                    Object inputValue = null;
                    if (Double.parseDouble(longVal + ".0") == cur)
                        inputValue = longVal;
                    else
                        inputValue = cur;
                    return String.valueOf(inputValue);
                }
                return String.valueOf(cur);
            }
            case BLANK:
            case ERROR:
                return "";
            case STRING:
                return String.valueOf(cell.getRichStringCellValue());
            case FORMULA:
                try {
                    CellType resultType = cell.getCachedFormulaResultType();
                    return getValue(resultType, cell);
                } catch (IllegalStateException e) {
                    e.printStackTrace();
                    return String.valueOf(cell.getRichStringCellValue());
                }
        }
        return null;
    }

}

模板案例显示模板语法

非模板

package com.example.easypoi.excel;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import java.io.File;
import java.io.FileOutputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class TestExcel {

    public static List<DemoEntry> getDemeEntry() {

        List<DemoEntry> list = new ArrayList<DemoEntry>();
        for (int i = 0; i < 100; i++) {
            DemoEntry dd = new DemoEntry();
            dd.setId(UUID.randomUUID().toString());
            dd.setNum(i + 0);
            Calendar calendar1 = Calendar.getInstance();//获取对日期操作的类对象
            calendar1.add(Calendar.DATE, -(i + 0));
            dd.setBirthday(calendar1.getTime());
            list.add(dd);
        }
        return list;
    }

    public static void main(String[] args) {
        // 测试下这个看看日期格式是什末
        try {
            System.out.println(getDemeEntry());
            //
            Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(), DemoEntry.class, getDemeEntry());


            // 创建单元格样式
            CellStyle cellStyle = workbook.createCellStyle();
            CreationHelper createHelper = workbook.getCreationHelper();
            cellStyle.setDataFormat(createHelper.createDataFormat().getFormat("yyyy-MM-dd"));
            Sheet sheet0 = workbook.getSheet("sheet0");

            //设置赛选
            CellRangeAddress c = CellRangeAddress.valueOf("B1");
            sheet0.setAutoFilter(c);
            refreshSheet(cellStyle, sheet0);
            // 刷新公式
            workbook.setForceFormulaRecalculation(true);

            FileOutputStream outputStream = new FileOutputStream(new File("E:/aaaa.xls"));
            workbook.write(outputStream);
            outputStream.close();
            workbook.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    protected static void refreshSheet(Sheet sheet) {
        // 所有的公式拷贝到同一行
        // 如果不拷贝,公式有可能不会再计算
        for (int i = sheet.getFirstRowNum(), end = sheet.getLastRowNum(); i <= end; i++) {
            Row row = sheet.getRow(i);
            if (row != null) {
                for (int j = row.getFirstCellNum(); j < row.getLastCellNum(); j++) {
                    Cell cell = row.getCell(j, Row.MissingCellPolicy.RETURN_NULL_AND_BLANK);
                    if (cell != null && cell.getCellTypeEnum() == CellType.FORMULA) {
                        cell.setCellFormula(cell.getCellFormula());
                    }
                }
            }
        }
    }



    protected static void refreshSheet(CellStyle cellStyle, Sheet sheet) throws ParseException {
        // 所有的公式拷贝到同一行
        // 如果不拷贝,公式有可能不会再计算
        for (int i = sheet.getFirstRowNum(), end = sheet.getLastRowNum(); i <= end; i++) {
            Row row = sheet.getRow(i + 1);
            if (row != null) {
                //从0开始
                Cell cell = row.getCell(1);
                if (!getValue(cell).equals("")) {
                    //System.out.println(cell.getCellType());
                    //System.out.println(getValue(cell));
                    // 设置单元格格式
                    cell.setCellStyle(cellStyle);
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                    // 设置日期值
                    cell.setCellValue(sdf.parse(getValue(cell)));
                }

            }
        }
    }


    private static String getValue(Cell cell) {
        if (cell == null) {
            return "";
        }
        return getValue(cell.getCellType(), cell);
    }

    private static String getValue(CellType cellType, Cell cell) {
        if (cell == null) {
            return "";
        }
        switch (cellType) {
            case BOOLEAN:
                return String.valueOf(cell.getBooleanCellValue());
            case NUMERIC: {
                double cur = cell.getNumericCellValue();

                String dataFormat = cell.getCellStyle().getDataFormatString();

                //处理日期格式问题
                if ("m/d/yy".equals(dataFormat)
                        || "yyyy\\-mm\\-dd".equals(dataFormat)
                        || "yyyy/m/d;@".equals(dataFormat)
                        || "yyyy\"年\"m\"月\"d\"日\";@".equals(dataFormat)) {
                    Date d = DateUtil.getJavaDate(cur);
                    SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
                    return f.format(d);
                }
                //处理时间格式问题
                else if ("yyyy/m/d\\ h:mm;@".equals(dataFormat)) {
                    Date d = DateUtil.getJavaDate(cur);
                    SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    return f.format(d);
                } else if ("General".equals(dataFormat)) {
                    long longVal = Math.round(cur);
                    Object inputValue = null;
                    if (Double.parseDouble(longVal + ".0") == cur)
                        inputValue = longVal;
                    else
                        inputValue = cur;
                    return String.valueOf(inputValue);
                }
                return String.valueOf(cur);
            }
            case BLANK:
            case ERROR:
                return "";
            case STRING:
                return String.valueOf(cell.getRichStringCellValue());
            case FORMULA:
                try {
                    CellType resultType = cell.getCachedFormulaResultType();
                    return getValue(resultType, cell);
                } catch (IllegalStateException e) {
                    e.printStackTrace();
                    return String.valueOf(cell.getRichStringCellValue());
                }
        }
        return null;
    }

}

实体就不粘贴了就三个字段 string int date 通过set方法推导

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值