Java教程

EasyPoi

本文主要是介绍EasyPoi,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
/**
 * @author Administrator
 */
public class DemoData {

    @Excel(name = "字符串标题",mergeVertical = true,needMerge=true)
    private String name;

    @Excel(name = "日期标题",
    format = "yyyy-MM-dd HH:mm:ss")
    private Date date;

    @Excel(name = "数字标题",suffix="%")
    private Double dou;

    @Excel(name = "图片",type = 2,width = 30 , height = 50)
    private String file;

    public DemoData() {
    }
    public DemoData(String name, Date date, Double dou, String file) {
        this.name = name;
        this.date = date;
        this.dou = dou;
        this.file = file;
    }

    @Override
    public String toString() {
        return "DemoData{" +
                "name='" + name + '\'' +
                ", date='" + date + '\'' +
                ", dou='" + dou + '\'' +
                ", file=" + file +
                '}';
    }

    public String getName() {
        return name;
    }

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

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    public Double getDou() {
        return dou;
    }

    public void setDou(Double dou) {
        this.dou = dou;
    }

    public String getFile() {
        return file;
    }

    public void setFile(String file) {
        this.file = file;
    }
}
======================
public class writer {

    public void main(String[] args) throws Exception {
        /**
         * 读
         */
        read();

        /**
         * 写
         */
//        writer();
    }

    public void read() throws Exception {
        FileInputStream  fileInputStream = new FileInputStream("E:/test/test20210603194325.xlsx");
        //默认选择第一个sheet
        ImportParams params = new ImportParams();
        params.setStartSheetIndex(0);
        List<DemoData> importList = ExcelImportUtil.importExcel(fileInputStream,DemoData.class,params);
        for (DemoData data : importList){
            System.out.println(DateUtil.format(data.getDate(),"yyyy-MM-dd"));
            System.out.println(JSON.toJSONString(data));
        }
    }

    public void writer() throws IOException {
        List<DemoData> list = new ArrayList<>();
        DemoData data = new DemoData("文件",new Date(),3.14,"E:\\test\\1622531517(1).jpg");
        list.add(data);

        //模板写出
//        TemplateExportParams templateExport = new TemplateExportParams(
//                "E:\\test\\moban.xlsx", true);
//        Map<String,Object> map = new HashMap<>();
//        List<Map<String, Object>> list1 = new ArrayList<>();
        // 创建数据对象
//        Map<String, Object> temp1 = new HashMap<String, Object>();
//        // 创建图片
//        ImageEntity image1 = new ImageEntity();
//        image1.setHeight(2000);
//        image1.setWidth(5000);
//        image1.setUrl("E:\\test\\1622531517(1).jpg");
//        //
//        temp1.put("name","树");
//        temp1.put("date",new Date()) ;
//        temp1.put("dou",3.14) ;
//        temp1.put("file", image1);
//        //
//        list1.add(temp1);
//
//        map.put("i",list1);

        ExportParams exportParams = new ExportParams();
        exportParams.setSheetName("sheet1");
        Workbook workbook = ExcelExportUtil.exportExcel(exportParams,DemoData.class,list);
//        Workbook workbook = ExcelExportUtil.exportExcel(templateExport,map);

        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        FileOutputStream outputStream = new FileOutputStream("E:/test/test"+sdf.format(new Date())+".xlsx");
        workbook.write(outputStream);
    }
这篇关于EasyPoi的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!