当前位置: 首页 > article >正文

easypoi, fastpoi快速导出xlsx,导出多sheet的xlsx

项目有个导出需求,需要导出多个sheet的,xlsx格式的excel表格。

这里简单说明下,在excel中,有xls,xlsx两种格式。

  • xls是excel2005版本以下的文件格式,支持数据量小而且格式很老。
  • xlsx是2005版本以上支持的格式,数据量大而且新特性多

我觉得都2024年了,大家导出excel应该都使用xlsx格式了吧。

在网上找了一圈没有什么好使的代码(其实找到了一个xls的导出代码,可以直接使用)。但是xlsx的却没找到现成的,于是基于xls的修改了下,这里直接贴出可用的,大家拿去直接食用吧。

  1. 引入依赖pom
       <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-base</artifactId>
            <version>4.5.0</version>
        </dependency>
       <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-web</artifactId>
            <version>4.5.0</version>
        </dependency>
	   <dependency>
	        <groupId>cn.afterturn</groupId>
	        <artifactId>easypoi-annotation</artifactId>
	        <version>4.5.0</version>
	    </dependency>

  1. 导入上面的依赖后,样例java代码如下:

大概说明下,以下代码中定义了两个sheet, 并导出到test.xlsx文件中


		import cn.afterturn.easypoi.excel.entity.ExportParams;
		import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
		import cn.afterturn.easypoi.excel.export.ExcelExportService;
		import org.apache.poi.ss.usermodel.Workbook;
		import org.apache.poi.xssf.streaming.SXSSFWorkbook;
		import org.apache.poi.xssf.usermodel.XSSFWorkbook;
		import org.apache.poi.xssf.usermodel.XSSFWorkbookType;

		// java导出代码
		List<Data1> data1List = new ArrayList();
		List<Data2> data2List = new ArrayList();
		Workbook workbook = new XSSFWorkbook(XSSFWorkbookType.XLSX);
		ExportParams exportParams0 = new ExportParams();
		exportParams0.setSheetName("sheet1");
		exportParams0.setType(ExcelType.XSSF);
		ExportParams exportParams1 = new ExportParams();
		exportParams1.setSheetName("shhet2");
		exportParams1.setType(ExcelType.XSSF);

		ExcelExportService service0 = new ExcelExportService();
		// 这里将data1List集合导出到excel,sheet1中
		service0.createSheet(workbook, exportParams0, Data1.class, data1List);
		ExcelExportService service1 = new ExcelExportService();
		// 这里将data2List集合导出到excel,sheet2中
		service1.createSheet(workbook, exportParams1, Data2.class, data2List);


		FileOutputStream fos = null;
		try {
		    // 写出文件
			File f = new File("./test.xlsx");
			f.createNewFile();
			fos = new FileOutputStream(f);
			workbook.write(fos);
			fos.close();
		} catch (Exception e) {
			throw new RuntimeException(e);
		}

最后一点,定义导出实体类时需要注意, 所有的导出字段需要指定width属性,不然当前版本会报错。

@Data
@ExcelTarget(value = "Data1")
public class Data1 implements Serializable {
    /**
     * 审核状态
     * commAuditStatus
     */
    @Excel(name = "审核状态", width = 20)
    private String commAuditStatus;

    /**
     * 申请单号
     * billId
     */
    @Excel(name = "申请单号", width = 20)
    private String billId;
}


@Data
@ExcelTarget(value = "Data2")
public class Data2 implements Serializable {
    /**
     * 审核状态2
     * commAuditStatus
     */
    @Excel(name = "审核状态2", width = 20)
    private String commAuditStatus;

    /**
     * 申请单号2
     * billId
     */
    @Excel(name = "申请单号2", width = 20)
    private String billId;
}

上面代码粘贴即可直接食用,好用点个赞!


http://www.kler.cn/news/334047.html

相关文章:

  • mysql-索引笔记
  • 有关自连接表的统一封装
  • 基于定制开发与2+1链动模式的商城小程序搭建策略
  • 招联2025校招内推倒计时
  • 如何在电脑上浏览手机界面
  • 【部署项目】禹神:前端项目部署上线笔记
  • 设计模式-模版方法模式
  • No.4 笔记 | 探索网络安全:揭开Web世界的隐秘防线
  • 如何在每台设备上恢复已删除的照片
  • 最新版的dubbo服务调用(用nacos做注册中心用)
  • 【解决办法】git clone报错unable to access ‘xxx‘: SSL certificate problem:
  • rtmp协议转websocketflv的去队列积压
  • 【2024年最新】基于Spring Boot+vue的旅游管理系统lw+ppt
  • 从一到无穷大 #36 Lindorm 宽表:东西互联,南北互联,AI一体
  • 真正的Open AI ——LLaMA颠覆开源大模型
  • LeetCode hot100---二叉树专题(C++语言)
  • 《python语言程序设计》2018版第8章19题几何Rectangle2D类(下)-头疼的几何和数学
  • 从Rally到Atlassian Cloud:思科Cisco实现云迁移的技术挑战与解决方案
  • 基于keras的停车场车位识别
  • Spring Boot新闻推荐系统:用户体验优化