1、先定义一个方法:
获取到一个map格式的数据后,作为参数传给exportExcel(response,map)
public String exportExcel(PageData pd, HttpServletResponse response) throws Exception {
Map<String, Object> fsclssExcelMap = getFsclssExcelMap(pd);
ExportExcelUtils.exportExcel(response, fsclssExcelMap);
}
2、然后新增文件导出类,ExportExcelUtils类:
public class ExportExcelUtils {
/**
* 下载文件
*
* @param response
* @param data
* @throws Exception
*/
public static void exportExcel(HttpServletResponse response, Map<String, Object> data) throws Exception {
// 下载文件的默认名称
String fileName = data.get("filename") + ".xls";
if (StringUtils.isEmpty(fileName)) {
DateTimeFormatter dfTime = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
fileName =dfTime.format(LocalDateTime.now())+ ".xls"; ;
}
response.setCharacterEncoding("UTF-8");
response.setContentType("application/vnd.ms-excel;charset=UTF-8");
response.addHeader("Content-Disposition", "attachment; filename=" + new String(fileName.getBytes("UTF-8"), "ISO8859-1"));
exportExcel(data, response.getOutputStream());
}
/**
* 创建 表格
*
* @param data
* @param out
* @throws Exception
*/
public static void exportExcel(Map<String, Object> data, OutputStream out) throws Exception {
HSSFWorkbook wb = new HSSFWorkbook();
try {
HSSFSheet sheet = wb.createSheet("Sheet1");
writeExcel(wb, sheet, data);
wb.write(out);
} finally {
wb.close();
}
}
@SuppressWarnings({ "unchecked", "deprecation" })
private static void writeExcel(HSSFWorkbook workbook, HSSFSheet sheet, Map<String, Object> data) {
List<String> titles = (List<String>) data.get("titles");
int len = titles.size();
// 标题样式
HSSFCellStyle headerStyle = workbook.createCellStyle();
headerStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
headerStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//垂直居中
// 标题字体
HSSFFont headerFont = workbook.createFont();
headerFont.setBold(true);
headerFont.setFontHeightInPoints((short) 11);
headerStyle.setFont(headerFont);
short width = 20, height = 25 * 20;
sheet.setDefaultColumnWidth(width);
HSSFCell cell;
// 设置标题
for (int i = 0; i < len; i++) {
String title = titles.get(i);
cell = getCell(sheet, 0, i);
cell.setCellStyle(headerStyle);
setText(cell, title);
}
sheet.getRow(0).setHeight(height);
// 内容样式
HSSFCellStyle contentStyle = workbook.createCellStyle();
contentStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
List<Map<String,String>> varList = (List<Map<String,String>>) data.get("varList");
int varCount = varList.size();
for (int i = 0; i < varCount; i++) {
Map<String,String> vpd = varList.get(i);
for (int j = 0; j < len; j++) {
String varstr = vpd.get("var" + j) != null ? vpd.get("var" + j) : "";
cell = getCell(sheet, i + 1, j);
cell.setCellStyle(contentStyle);
setText(cell, varstr);
}
}
}
private static HSSFCell getCell(final HSSFSheet sheet, final int row, final int col) {
HSSFRow sheetRow = sheet.getRow(row);
if (sheetRow == null) {
sheetRow = sheet.createRow(row);
}
HSSFCell cell = sheetRow.getCell(col);
if (cell == null) {
cell = sheetRow.createCell(col);
}
return cell;
}
private static void setText(final HSSFCell cell, final String text) {
cell.setCellType(1);
cell.setCellValue(text);
}
}
3、最后结果如下: