之前的导出都很乱,直接写在代码中,等到下回还使用导出功能时又不知如何下手,今天用模板模式重写了一个导出功能,方便以后使用:
package com.sf.addrCheck.util.export.poi;
import com.sf.addrCheck.ebo.QbUserAdmin;
import com.sf.addrCheck.util.export.ExcelUtil;
import org.apache.poi.hssf.usermodel.*;
import org.osgi.resource.Requirement;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.*;
/**
* poi-excel导出模板
*/
public abstract class PoiExport {
/**
* 导出的数据
*/
private List list;
/**
* 标题
*/
private String[] title;
/**
* 文件名
*/
private String fileName = "export_" + System.currentTimeMillis()+".xlsx";
/**
* 表格名
*/
private String sheetName = "sheet1";
public PoiExport(List list, String[] title) {
this.list = list;
this.title = title;
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName + ".xlsx";
}
public String getSheetName() {
return sheetName;
}
public void setSheetName(String sheetName) {
this.sheetName = sheetName;
}
private HSSFWorkbook getHSSFWorkbook(String sheetName, String[] title, String[][] values, HSSFWorkbook wb) {
// 第一步,创建一个HSSFWorkbook,对应一个Excel文件
if (wb == null) {
wb = new HSSFWorkbook();
}
// 第二步,在workbook中添加一个sheet,对应Excel文件中的sheet
HSSFSheet sheet = wb.createSheet(sheetName);
// 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制
HSSFRow row = sheet.createRow(0);
// 第四步,创建单元格,并设置值表头 设置表头居中
HSSFCellStyle style = wb.createCellStyle();
style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式
// 声明列对象
HSSFCell cell = null;
// 创建标题
for (int i = 0; i < title.length; i++) {
cell = row.createCell(i);
cell.setCellValue(title[i]);
cell.setCellStyle(style);
}
// 创建内容
for (int i = 0; i < values.length; i++) {
row = sheet.createRow(i + 1);
for (int j = 0; j < values[i].length; j++) {
// 将内容按顺序赋给对应的列对象
row.createCell(j).setCellValue(values[i][j]);
}
}
return wb;
}
/**
* 需要重写的方法:用来填充二元数组dataTable
*
* <p>这里主要实现把list中的每个数据设置到dataTable中,list中Obj的属性对应dataTable中的列,list的index对应dataTable中的行</p>
*
* @param dataTable 代表数据表,第一位表示行数,第二位表示列数
* @param i 行数
* @return
*/
public abstract void fillingData(String[][] dataTable, int i, List list);
public void export(HttpServletResponse response) throws Exception {
String[][] dataTable = new String[list.size()+1][title.length];
for (int i = 0; i < list.size(); i++) {
this.fillingData(dataTable,i, this.list);
}
//创建HSSFWorkbook
HSSFWorkbook wb = this.getHSSFWorkbook(sheetName, title, dataTable, null);
//响应到客户端
try {
this.setResponseHeader(response, fileName);
OutputStream os = response.getOutputStream();
wb.write(os);
os.flush();
os.close();
} catch (Exception e) {
e.printStackTrace();
}
}
//发送响应流方法
private void setResponseHeader(HttpServletResponse response, String fileName) {
try {
try {
fileName = new String(fileName.getBytes(),"ISO8859-1");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
response.setContentType("application/octet-stream;charset=ISO8859-1");
response.setHeader("Content-Disposition", "attachment;filename="+ fileName);
response.addHeader("Pargam", "no-cache");
response.addHeader("Cache-Control", "no-cache");
} catch (Exception ex) {
ex.printStackTrace();
}
}
/**
* 使用例子
*/
public static void main(String[] args) {
List<QbUserAdmin> list = new ArrayList<>();
QbUserAdmin qbUserAdmin = new QbUserAdmin();
qbUserAdmin.setLoginName("admin");
qbUserAdmin.setRole("1");
String[] titles = {"loginName", "role"};
new PoiExport(Collections.singletonList(list), titles) {
@Override
public void fillingData(String[][] content, int i, List list) {
QbUserAdmin qbUserAdmin1 = (QbUserAdmin)list.get(i);
content[i][0] = qbUserAdmin1.getLoginName();
content[i][1] = qbUserAdmin1.getRole();
}
}.export(response); //这里的main没有response,需要在web应用中使用
}
}