最近项目中需要用到Excel解析与生成Excel,但现有系统中没有进行解析工具封装于是便封装一个Excel工具包,支持JXL工具包与POI工具包解析与生成数据,POI支持2003与2007。
Excel解析目前网上有JXL与POI两个类库,相比较下JXL功能较少点 比较容易上手。POI功能更强大,支持2003与2007 。目前POI 3.8 版本中提供SXSSFWorkbook类支持大数据量操作 能够避免内存溢出(目前工具包中暂未使用SXSSFWorkbook的例子)。
工具包中用到第三方包有:
cglib-nodep-2.1_3.jar (用于Excel解析时生成BeanMap对象)
commons-lang3-3.1.jar
jxl.jar(jxl工具包)
以下都是POI解析用到的包,如果只是使用2003可以导入少点包
dom4j-1.6.1.jar
org.apache.poi-osgi-3.8.jar
poi-ooxml-3.8-beta5.jar
poi-ooxml-schemas-3.8-beta5.jar
xmlbeans-2.5.0.jar
工具包生成与解析Excel主要依赖于反射与注解,包中申明一个ExcelAnnotation,该注解注释与需要导出字段上,用于申明标题头。生成Excel时根据判断对象是否使用该注解,然后生成Excel。解析时也根据注解进行解析。
使用注解 注解与需要生成Excel对象上
public class Excel {
@ExcelAnnotation("名称")
public String bookName;
@ExcelAnnotation("行数")
public int rows;
@ExcelAnnotation("创建时间")
public Date createTime;
@ExcelAnnotation("内容")
public Date content;
public Date getContent() {
return content;
}
public void setContent(Date content) {
this.content = content;
}
public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
public int getRows() {
return rows;
}
public void setRows(int rows) {
this.rows = rows;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
@Override
public String toString() {
StringBuffer buffer = new StringBuffer();
buffer.append("{[bookName : " + (null == bookName ? "" : bookName)
+ "]");
buffer.append("[rows : " + rows + "]");
buffer.append("[createTime : "
+ (null == createTime ? "" : DateTools.format(createTime))
+ "]}");
return buffer.toString();
}
}
JXL生成与解析例子
public class JxlExcelTest {
/**
* 创建Excel
*/
public static void testCreateWorkbook() {
try {
OutputStream os = new FileOutputStream("e://excel//jxlExcel.xls");
List<Object> allExcel = new ArrayList<Object>(10);
// 创建对象存放数据
for(int i = 0 ; i<10000; i ++){
Excel excel4 = new Excel();
excel4.setBookName("excel"+i);
excel4.setCreateTime(new Date());
excel4.setRows(i);
allExcel.add(excel4);
}
WritableWorkbook book = JxlUtil.createWorkbook("excel23",
Excel.class, allExcel, os);
book.write();
book.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (RowsExceededException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (WriteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 解析Excel
*/
public static void testAnalyticJxlExcel() {
try {
InputStream input = new FileInputStream("e://excel//jxlExcel.xls");
Workbook workBook = Workbook.getWorkbook(input);
List<Object> list = JxlAnalyticalData.AnalyticJxlExcel(workBook, Excel.class);
for(Object object : list){
System.out.println((Excel)object);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (BiffException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* @param args
*/
public static void main(String[] args) {
long a = System.currentTimeMillis();
// TODO Auto-generated method stub
//创建Excle
testCreateWorkbook();
// //解析Excel
// testAnalyticJxlExcel();
System.out.println("执行耗时 : " + (System.currentTimeMillis() - a)
/ 1000f + " 秒 ");
}
}
POI解析与生成例子 包括2003 与 2007
public class PoiExcelTest {
/**
* 生成excel
*
* @throws BiffException
* @throws IOException
* @throws IllegalArgumentException
* @throws IllegalAccessException
* @throws InstantiationException
*/
public static void createWorkbook() throws BiffException, IOException,
IllegalArgumentException, IllegalAccessException,
InstantiationException {
// 创建2003
OutputStream os = new FileOutputStream("e://excel//poiExcel2003.xls");
// 创建2007
// OutputStream os = new FileOutputStream("e://excel//poiExcel2007.xlsx");
List<Object> allExcel = new ArrayList<Object>(10000);
// 创建对象存放数据
for(int i = 0 ; i<10000; i ++){
Excel excel4 = new Excel();
excel4.setBookName("excel"+i);
excel4.setCreateTime(new Date());
excel4.setRows(i);
allExcel.add(excel4);
}
// 创建2003
HSSFWorkbook sr = PoiUtil.createWorkbook("pooi", Excel.class,
allExcel);
// 创建2007
// XSSFWorkbook sr = PoiUtil.createXSSFWorkbook("pooi", Excel.class,
// allExcel);
sr.write(os);
os.flush();
}
/**
* 解析Excel
*
* @throws IllegalArgumentException
* @throws IllegalAccessException
* @throws InstantiationException
*/
public static void testAnalyticPoiExcel() throws IllegalArgumentException,
IllegalAccessException, InstantiationException {
try {
//2003
InputStream input = new FileInputStream("e://excel//poiExcel2003.xls");
HSSFWorkbook book = new HSSFWorkbook(input);
//2007
// InputStream input = new FileInputStream("e://excel//poiExcel2007.xlsx");
// XSSFWorkbook book = new XSSFWorkbook(input);
List<Object> poiList = PoiAnalyticalData.AnalyticPoiExcel(book,
Excel.class);
for (Object object : poiList) {
System.out.println((Excel) object);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
long a = System.currentTimeMillis();
createWorkbook();
// testAnalyticPoiExcel();
System.out.println("执行耗时 : " + (System.currentTimeMillis() - a)
/ 1000f + " 秒 ");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
代码下载地址
http://www.oschina.net/code/snippet_269725_17891
来源:oschina
链接:https://my.oschina.net/u/269725/blog/107166