PS:自行练习poi实现导出Excel(简单样式)数据,由于业务(…)需要,直接在controller层实现;
ok 进入正题!
① 引入POI依赖;
org.apache.poi
poi
3.6
② 直接写接口,调用service层;
@RequestMapping(value = "/exportData", method = RequestMethod.GET)
public void exportData(HttpServletResponse response,xxxx(若有筛选条件所加参数)) throws IOException {
try {
OutputStream out = response.getOutputStream();
// 调用service层
Map map = exportService.selectExportData(xxxx(若有筛选条件所加参数));
//get 这里map中的list
List list = (List) map.get("list");
//获取excel内容
Object[][] values = ListToArray(list, 5);
// 第一步,创建一个HSSFWorkbook,对应一个Excel文件
HSSFWorkbook wb = new HSSFWorkbook();
// 第二步,在workbook中添加一个sheet,对应Excel文件中的sheet
HSSFSheet sheet = wb.createSheet("表一");
// 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制
HSSFRow row = sheet.createRow(0);
// 设置标题行高度
row.setHeightInPoints(18);
// 第四步,创建单元格,并设置表头居中,加边框
HSSFCellStyle style = wb.createCellStyle();
//边框(左、右、上、下)
style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
style.setBorderRight(HSSFCellStyle.BORDER_THIN);
style.setBorderTop(HSSFCellStyle.BORDER_THIN);
style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
//居中设置
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
//声明列对象
HSSFCell cell = null;
int r = 0;
//获取excel标题列
Map firstMap = (Map) list.get(0);
String title[] = new String[firstMap.keySet().size()];
// 新建map将原英文标题替换为中文
HashMap firMap = new HashMap();
firMap.put("英文字段","中文字段");
...
int index = 0;
for (String s : firstMap.keySet()) {
title[index] = s;
index++;
}
//创建标题(遍历)
for (int i = 0; i < title.length; i++) {
cell = row.createCell(i);
//取出新建firMap的key值
cell.setCellValue((String) firMap.get(title[i]));
cell.setCellStyle(style);
}
if (title.length != 0) {
r = 1;
}
//创建内容(遍历)
for (int i = 0; i < values.length; i++) {
row = sheet.createRow(i + r);
for (int j = 0; j < values[i].length; j++) {
//将内容按顺序赋给对应的列对象
cell = row.createCell(j);
cell.setCellValue(values[i][j] == null ? "" : values[i][j].toString());
//此处设置内容字体与标题行相同格式
cell.setCellStyle(style);
}
}
// 设置单元格宽度
int curColWidth = 0;
for (int i = 0; i <= title.length; i++) {
// 列自适应宽度,可能对中文不太友好
sheet.autoSizeColumn(i, true);
// 为每一列设置一个最小值,方便中文显示
curColWidth = sheet.getColumnWidth(i);
if(curColWidth<3500){
sheet.setColumnWidth(i, 3500);
}
}
//调用响应流方法
ExcelUtil.setResponseHeader(response, "设置表名.xls");
wb.write(out);
out.flush();
out.close();
} catch (Exception e) {
}
}
// List转数组
private Object[][] ListToArray(List<Map> list, int KeyLenght) {
if (CollectionUtils.isEmpty(list)) {
return new Object[0][];
}
int size = list.size();
Object[][] array = new Object[size][KeyLenght];
for (int i = 0; i < size; i++) {//循环遍历所有行
array[i] = list.get(i).values().toArray();//每行的列数
}
return array;
}
③ ExcelUtil中的发送响应流方法
//发送响应流方法
public static 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();
}
}
④ service层
Map selectExportData(xxxx(可能需要的参数));
serviceImpl层 (略)
⑤ mapper层
List<Map> selectExportData(xxxx(可能需要的参数));
mapper.xml层(略)