使用ExcelUtils导出Excel文件

官网地址: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例子,目的是能快速的上手使用,更多用法需要参照官方文档,有不足之处欢迎指正。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
We're headache on making web report all long. 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. After my hardwork, the parser is finished finally, in which report is exported by Excel Template. It's funtions include: 1、${model.name} means getting property of the name from the model object. 2、${!model.name} means that last cell and this cell merge if model.name value equals last cell value. 3、#foreach model in ${list},means that iterate list,modelId is implied index of the list. 4、#each ${model} ${width1},${width2},model can be a Map,JavaBean,Collection or Array object, #each key will show all property of the model.${width?} means merge ${width?} cells. If only one width, all property use the same width. If more than one, use the witdh in order, not set will use "1". 5、${list[0].name} means get the first object from list, then read the property of name. 6、${map(key)} get the value from the map by the key name. 7、${list[${index}].name} [] can be a variable. 8、${map(${key})} () can be a vriable. 9、#sum qty on ${list} where name like/=str sum qty on ${list} collection by where condition. 10、In net.sf.excelutils.tags Package, you can implement ITag to exentd Tag key. eg, FooTag will parse #foo. 11、ExcelResult for webwork. 12、${model${index}} support. 13、#call service.method("str", ${name}) call a method 14、#formual SUM(C${currentRowNo}:F${currentRowNo}) means output excel formula SUM(C?:F?) ? means currentRowNo.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值