使用ExcelUtils导出Excel文件

ExcelUtils是一款用于Java项目导出复杂Excel报表的工具,通过使用Excel模板,可以自由设计报表格式,仅需处理数据,无需修改源代码。基于POI项目,适用于小批量数据导出。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

官网地址:http://excelutils.sourceforge.net/index.html

ExcelUtils is a helper to export excel report in java web project. 
It's like velocity, has own tags, but these tags is written in excel file. 
By these tags, you can custom your excel report format freely, 
not edit any your source, just ExcelUtils parses your excel template and fills values to export your report.
It is based POI project and beanutils project.
It uses excel and template language's profit to make web reports easily. 

从官网的介绍我们就可以看出,ExcelUtils是一个专门为导出Java报表而设计的工具类,他需要一个专门的Excel模板文件作为模板,将具体的数据导出到Excel模板中。

优点:使用了Excel模板可以设计出复杂的Excel文件,导出Excel也非常简单,只需要将数据组织好,直接添加到内存即可。

缺点:

1、数据都一次性保存到内存当中了,不适合大批量的数据导出,大批量的数据导出还是建议使用POI进行操作比较好。

2、excel模板只能选择xls格式的模板。

3、作者早在2005年就已经停更了,所以使用上如果有什么不好的地方只能自己啃源码了。

一个简单的ExcelUtils导出实例:

1、jar包依赖

  • excelutils.jar
  • poi-2.5.1.jar
  • commons-logging.jar
  • commons-digester.jar
  • commons-beanutils.jar

 maven依赖

  	<dependency>
	    <groupId>com.github.hxbkx</groupId>
	    <artifactId>ExcelUtils</artifactId>
	    <version>1.4.2</version>
	</dependency>
  	<dependency>
	    <groupId>org.apache.poi</groupId>
	    <artifactId>poi</artifactId>
	    <version>3.17</version>
	</dependency>
	<dependency>
	    <groupId>commons-logging</groupId>
	    <artifactId>commons-logging</artifactId>
	    <version>1.1.1</version>
	</dependency>
	<dependency>
	    <groupId>org.apache.commons</groupId>
	    <artifactId>commons-digester3</artifactId>
	    <version>3.2</version>
	</dependency>
	<dependency>
	    <groupId>commons-beanutils</groupId>
	    <artifactId>commons-beanutils</artifactId>
	    <version>1.9.3</version>
	</dependency>

 2、配置excel模板:

 项目目录结构:

 

项目代码:

package excelUtils;

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

import net.sf.excelutils.ExcelException;
import net.sf.excelutils.ExcelUtils;

public class ExcelUtilsTest {
	public static void main(String[] args) {
		new ExcelUtilsTest().export();
	}
	public void export() {
		FileOutputStream fos=null;
		try {
			List<User> list=new ArrayList<User>();
			for (int i = 0; i < 300; i++) {
				list.add(new User("张三", "23", "广西"));
			}
			ExcelUtils.addValue("userList", list);
			String path = this.getClass().getResource("/").getPath();
			String modelPath=path+"userModel.xlsx";
			File out=new File("D:/user.xlsx");
			
			fos=new FileOutputStream(out);
			System.out.println(path);
			ExcelUtils.export(modelPath, fos);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ExcelException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			if(fos!=null) {
				try {
					fos.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}
}

效果:

总结:这只是一个简单的导出excel例子,目的是能快速的上手使用,更多用法需要参照官方文档,有不足之处欢迎指正。

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值