您的位置:首页 > 其它

JFinal框架使用renderFile()进行列表页面数据导出Excel

2018-01-15 18:57 519 查看
package com.business.util;

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.util.List;

import java.util.Map;

import org.apache.poi.hssf.usermodel.HSSFCell;

import org.apache.poi.hssf.usermodel.HSSFRow;

import org.apache.poi.hssf.usermodel.HSSFSheet;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import com.jfinal.plugin.activerecord.Record;

public class ExcelExportUtil {

private static final String FILEPATH = new File("C:/").getAbsolutePath();
private static final String time = DateUtil.formatDate();
public static String getTitle(String title){
//title是Excel命名名称
    String name=FILEPATH+title+"-"+time+"_统计表.xls";  
    return name;
}

public static File saveFile(Map<String, String> headData, List<Record> list, File file) {
// 创建工作薄
HSSFWorkbook hssfWorkbook = new HSSFWorkbook();
// 创建工作薄中的工作表 sheet:一张表的简称
HSSFSheet hssfSheet = hssfWorkbook.createSheet();
// 创建行
HSSFRow row = hssfSheet.createRow(0);
// 创建单元格
HSSFCell cell = null;
// 初始化索引
int rowIndex = 0;
int cellIndex = 0;

// 创建标题行
row = hssfSheet.createRow(rowIndex);
rowIndex++;
// 遍历标题
for (String h : headData.keySet()) {
//创建列
cell = row.createCell(cellIndex);
//索引递增
cellIndex++;
//逐列插入标题
cell.setCellValue(headData.get(h));
}

// 得到所有记录 行:列
Record record = null;

if (list != null) {
// 获取所有的记录 有多少条记录就创建多少行
for (int i = 0; i < list.size(); i++) {
row = hssfSheet.createRow(rowIndex);
// 得到所有的行 一个record就代表 一行
record = list.get(i);
//下一行索引
rowIndex++;
//刷新新行索引
cellIndex = 0;
// 在有所有的记录基础之上,便利传入进来的表头,再创建N行
for (String h : headData.keySet()) {
cell = row.createCell(cellIndex);
cellIndex++;
//按照每条记录匹配数据
cell.setCellValue(record.get(h) == null ? "" : record.get(h).toString());
}
}
}
try {
FileOutputStream fileOutputStreane = new FileOutputStream(file);
hssfWorkbook.write(fileOutputStreane);
fileOutputStreane.flush();
fileOutputStreane.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return file;
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐