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