JFinal框架使用renderFile()進行列表頁面資料匯出Excel
阿新 • • 發佈:2019-02-11
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;
}
}
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;
}
}