import java.io.IOException;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
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 org.apache.poi.ss.util.CellRangeAddress;
import lombok.extern.slf4j.Slf4j;
/***
* 导出EXCEL工具类
*/
@Slf4j
public class GenerateExcelUtils {
/***
* 工作簿
*/
private static HSSFWorkbook workbook;
/***
* sheet
*/
private static HSSFSheet sheet;
/***
* 标题行开始位置
*/
private static final int TITLE_START_POSITION = 0;
/***
* 时间行开始位置
*/
private static final int DATEHEAD_START_POSITION = 1;
/***
* 表头行开始位置
*/
private static final int HEAD_START_POSITION = 3;
/***
* 统计金额行
*/
private static final int TOTAL_PAYMENT_POSITION = 2;
/***
* 文本行开始位置
*/
private static final int CONTENT_START_POSITION = 4;
/**
* @author: WANG BO
* @param dataList
* 对象集合
* @param titleMap
* 表头信息(对象属性名称->要显示的标题值)[按顺序添加]
* @param sheetName
* sheet名称和表头值
* @param filePath
* 文件路径
*@param totalPayMent
* 总金额
*/
public static HSSFWorkbook excelExport(List<?> dataList, Map<String, String> titleMap, String sheetName, BigDecimal totalPayMent) {
// 初始化workbook
// 写入处理结果
try {
initHSSFWorkbook(sheetName);
// 标题行
createTitleRow(titleMap, sheetName);
// 时间行
createDateHeadRow(titleMap);
// 表头行
createHeadRow(titleMap);
//统计行
createCountRow(totalPayMent,titleMap);
// 文本行
createContentRow(dataList, titleMap);
//设置自动伸缩
//autoSizeColumn(titleMap.size());
//OutputStream out = new FileOutputStream(filePath);
//workbook.write(out);
// out.close();
}
catch (Exception e) {
e.printStackTrace();
}
return workbook;
}
/**
* @Title: excelExport
* @Description: 导出Excel的方法
* @author:
* @date:
* @param dataList
* 对象集合
* @param fileName
* 文件名称
* @param sheetName
* sheet名称和表头值
* @param titleMap
* 表头信息(对象属性名称->要显示的标题值)[按顺序添加]
* @param totalPayMent
* 总金额
* @throws Exception
*/
public static void excelExport(HttpServletResponse response, List<?> dataList, String fileName, String sheetName, Map<String, String> titleMap, BigDecimal totalPayMent) throws Exception {
// 初始化workbook
initHSSFWorkbook(sheetName);
// 标题行
createTitleRow(titleMap, sheetName);
// 时间行
createDateHeadRow(titleMap);
// 表头行
createHeadRow(titleMap);
// 统计行
createCountRow(totalPayMent,titleMap);
// 文本行
createContentRow(dataList, titleMap);
// 设置自动伸缩
autoSizeColumn(titleMap.size());
// 写入处理结果
response.setHeader("Content-disposition",
"attachment;filename=" + new String(fileName.getBytes("gbk"), "iso8859-1") + ".xls");
response.flushBuffer();
ServletOutputStream outStream = response.getOutputStream();
try {
workbook.write(outStream);
} catch (Exception e) {
log.error("导出Excel出错!详情:[{}]", e.toString());
throw new IOException(e);
} finally {
outStream.close();
}
}
/***
*
* @param sheetName
* sheetName
*/
private static void initHSSFWorkbook(String sheetName) {
workbook = new HSSFWorkbook();
sheet = workbook.createSheet(sheetName);
}
/**
* 生成标题(第零行创建)
* @param titleMap 对象属性名称->表头显示名称
* @param sheetName sheet名称
*/
private static void createTitleRow(Map<String, String> titleMap, String sheetName) {
CellRangeAddress titleRange = new CellRangeAddress(0, 0, 0, titleMap.size() - 1);
sheet.addMergedRegion(titleRange);
HSSFRow titleRow = sheet.createRow(TITLE_START_POSITION);
HSSFCell titleCell = titleRow.createCell(0);
titleCell.setCellValue(sheetName);
}
/**
* 创建时间行(第一行创建)
* @param titleMap 对象属性名称->表头显示名称
*/
private static void createDateHeadRow(Map<String, String> titleMap) {
CellRangeAddress dateRange = new CellRangeAddress(1, 1, 0, titleMap.size() - 1);
sheet.addMergedRegion(dateRange);
HSSFRow dateRow = sheet.createRow(DATEHEAD_START_POSITION);
HSSFCell dateCell = dateRow.createCell(0);
dateCell.setCellValue(new SimpleDateFormat("yyyy年MM月dd日").format(new Date()));
}
/**
* 创建表头行(第三行创建)
* @param titleMap 对象属性名称->表头显示名称
*/
private static void createHeadRow(Map<String, String> titleMap) {
// 第3行创建
HSSFRow headRow = sheet.createRow(HEAD_START_POSITION);
int i = 0;
for (String entry : titleMap.keySet()) {
HSSFCell headCell = headRow.createCell(i);
headCell.setCellValue(titleMap.get(entry));
i++;
}
}
/**
* 创建统计行(第二行创建)
* @param totalPayment 统计金额
*/
private static void createCountRow(BigDecimal totalPayment,Map<String, String> titleMap) {
// 第2行创建
CellRangeAddress dateRange = new CellRangeAddress(2, 2, 0, titleMap.size() - 1);
sheet.addMergedRegion(dateRange);
HSSFRow headRow = sheet.createRow(TOTAL_PAYMENT_POSITION);
// int i = 0;
// for (String entry : titleMap.keySet()) {
HSSFCell headCell = headRow.createCell(0);
headCell.setCellValue("支付金额总计:¥"+totalPayment);
//i++;
// }
}
/**
*
* @param dataList 对象数据集合
* @param titleMap 表头信息
*/
private static void createContentRow(List<?> dataList, Map<String, String> titleMap) {
try {
int i=0;
for (Object obj : dataList) {
HSSFRow textRow = sheet.createRow(CONTENT_START_POSITION + i);
int j = 0;
for (String entry : titleMap.keySet()) {
String method = "get" + entry.substring(0, 1).toUpperCase() + entry.substring(1);
Method m = obj.getClass().getMethod(method, null);
String value="";
if(m.invoke(obj, null) != null){
value = m.invoke(obj, null).toString();
}
HSSFCell textcell = textRow.createCell(j);
textcell.setCellValue(value);
j++;
}
i++;
}
}
catch (Exception e) {
e.printStackTrace();
}
}
/**
* 自动伸缩列
* @param size 列数
*/
private static void autoSizeColumn(Integer size) {
for (int j = 0; j < size; j++) {
sheet.autoSizeColumn(j);
}
}
}