最近做数据列表导出成Excel表格,借此机会学习了一下。
首先我们需要通过hibernate链接数据库,将数据库字段映射成POJO实体,这里不多做废话。
我们需要用excel表格工具类,首先需要几个poi的jar包,poi读取excel数据所需要的jar包。其他的还需要一些commons-lang等语言包。
poi-3.9-20130828.jar
poi-ooxml-3.9-20130828.jar
poi-ooxml-schemas-3.9-20130828.jar
Apache POI 是用Java编写的免费开源的跨平台的 Java API,Apache POI提供API给Java程式对Microsoft Office格式档案读和写的功能。Apache POI 是创建和维护操作各种符合Office Open XML(OOXML)标准和微软的OLE 2复合文档格式(OLE2)的Java API。用它可以使用Java读取和创建,修改MS Excel文件.而且,还可以使用Java读取和创建MS Word和MSPowerPoint文件。Apache POI 提供Java操作Excel解决方案(适用于Excel97-2008)。 下面我们来看一下Apache POI 中提供的几大部分的作用:
HSSF - 提供读写Microsoft Excel XLS格式档案的功能。
XSSF - 提供读写Microsoft Excel OOXML XLSX格式档案的功能。
HWPF - 提供读写Microsoft Word DOC格式档案的功能。
HSLF - 提供读写Microsoft PowerPoint格式档案的功能。
HDGF - 提供读Microsoft Visio格式档案的功能。
HPBF - 提供读Microsoft Publisher格式档案的功能。
HSMF - 提供读Microsoft Outlook格式档案的功能。
1.ExcelCreater 创建Excel表格类
package com.yuanding.common.data.excel;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.yuanding.common.data.excel.ExcelFile.ExcelStyle;
import com.yuanding.common.util.Constants;
public class ExcelCreater {
static {
ExcelCreater.setFilePath(Constants.Path.DOC_PATH);
}
private String fullFileName;
private static String filePath;
private String title;
private String sheetTitle;
private String[] columnTitle;
private String[] columnKey;
private String[] columnWidth;
private List<Map<String, Object>> data;
public String getFullFileName() {
return filePath + fullFileName;
}
public void setFullFileName(String fullFileName) {
this.fullFileName = fullFileName;
}
public static String getFilePath() {
return filePath;
}
public static void setFilePath(String filePath) {
File file = new File(filePath);
if(file.exists() && file.isDirectory()){
ExcelCreater.filePath = filePath;
}else{
ExcelCreater.filePath = System.getProperty("java.io.tmpdir");
}
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String[] getColumnTitle() {
return columnTitle;
}
public void setColumnTitle(String[] columnTitle) {
this.columnTitle = columnTitle;
}
public String[] getColumnKey() {
return columnKey;
}
public void setColumnKey(String[] columnKey) {
this.columnKey = columnKey;
}
public String[] getColumnWidth() {
return columnWidth;
}
public void setColumnWidth(String[] columnWidth) {
this.columnWidth = columnWidth;
}
public List<Map<String, Object>> getData() {
return data;
}
public void setData(List<Map<String, Object>> data) {
this.data = data;
}
public String getSheetTitle() {
return sheetTitle;
}
public void setSheetTitle(String sheetTitle) {
this.sheetTitle = sheetTitle;
}
public boolean checkParam() {
return fullFileName != null && columnTitle != null && columnKey != null;
}
public boolean create() {
if (checkParam()) {
ExcelFile excel = new ExcelFile();
Map<String, String> sheetStyle = new HashMap<String, String>();
sheetStyle.put(ExcelStyle.SHEET_HEAD, this.getTitle());
sheetStyle.put(ExcelStyle.TITLE_HEIGHT, "500");
sheetStyle.put(ExcelStyle.ROW_HEIGHT, "500");
List<Map<String, String>> rowStyle = new ArrayList<Map<String, String>>();
for (int i = 0, len = columnTitle.length; i < len; i++) {
Map<String, String> styleItem1 = new HashMap<String, String>();
styleItem1.put(ExcelStyle.CELL_TITLE, this.getColumnTitle()[i]);
styleItem1.put(ExcelStyle.CELL_NAME, this.getColumnKey()[i]);
styleItem1.put(ExcelSt