使用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>();
			list.add(new User("張三", "23", "廣西"));
			list.add(new User("張四", "24", "湖北"));
            //將數據添加到內存中,key與excel模板中的值對應
			ExcelUtils.addValue("userList", list);
			String path = this.getClass().getResource("/").getPath();
            //模板文件路徑
			String modelPath=path+"userModel.xls";
			//導出的文件
            File out=new File("D:/user.xls");
			
			fos=new FileOutputStream(out);
			System.out.println(path);
            //導出excel
			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例子,目的是能快速的上手使用,更多用法需要參照官方文檔。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章