1. 引入依赖
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.2</version>
</dependency>
2. 简单导出实现
package cn.sto.station.twin.common.excel;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.util.IOUtils;
import org.apache.poi.xssf.streaming.SXSSFSheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Slf4j
public class SXSSFWorkbookExporter {
private String[] headerNames;
private Workbook workBook;
private Sheet sheet;
public SXSSFWorkbookExporter(String[] headerNames, String sheetName) {
this.headerNames = headerNames;
workBook = new SXSSFWorkbook(100);
sheet = workBook.createSheet(sheetName);
initHeader();
}
private void initHeader() {
Row row = sheet.createRow(0);
Cell cell = null;
for (int i = 0; i < headerNames.length; i++) {
cell = row.createCell(i);
cell.setCellValue(headerNames[i]);
setHeaderCellStyle(cell);
}
}
public void setHeaderCellStyle(Cell cell) {
CellStyle cellStyle = workBook.createCellStyle();
cellStyle.setAlignment(HorizontalAlignment.CENTER);
Font font = workBook.createFont();
font.setFontName(HSSFFont.FONT_ARIAL);
font.setBold(true);
font.setFontHeightInPoints((short) 13);
cellStyle.setFont(font);
cell.setCellStyle(cellStyle);
}
public void createTableRows(List<Map<String, Object>> datas, String[] keys) {
for (int i = 0, length_1 = datas.size(); i < length_1; i++) {
if (ArrayUtils.isEmpty(keys)) {
keys = headerNames;
}
Map<String, Object> data = datas.get(i);
Row row = sheet.createRow(i + 1);
Cell cell = null;
for (int j = 0, length_2 = keys.length; j < length_2; j++) {
String key = keys[j];
String value = MapUtils.getString(data, key, "");
cell = row.createCell(j);
cell.setCellType(CellType.STRING);
cell.setCellValue(value);
}
}
}
public void autoAllSizeColumn() {
if (sheet instanceof SXSSFSheet) {
SXSSFSheet tmpSheet = (SXSSFSheet) sheet;
tmpSheet.trackAllColumnsForAutoSizing();
}
for (int i = 0, length = headerNames.length; i < length; i++) {
sheet.autoSizeColumn(i);
}
}
public void exportExcel(OutputStream outputStream) {
this.autoAllSizeColumn();
try {
workBook.write(outputStream);
} catch (IOException e) {
log.error(" exportExcel error", e);
} finally {
IOUtils.closeQuietly(outputStream);
}
}
public void exportExcel(String outputFilePath) {
this.autoAllSizeColumn();
FileOutputStream outputStream = null;
try {
outputStream = new FileOutputStream(outputFilePath);
workBook.write(outputStream);
} catch (IOException e) {
log.error(" exportExcel error", e);
} finally {
IOUtils.closeQuietly(outputStream);
}
}
public static void main(String[] args) {
test();
}
private static void test() {
SXSSFWorkbookExporter hssfWorkExcel = new SXSSFWorkbookExporter(new String[] { "姓名", "年龄" }, "人员基本信息");
List<Map<String, Object>> datas = new ArrayList<>();
for (int i = 0; i < 10; i++) {
Map data = new HashMap<>();
data.put("name", "name" + i);
data.put("age", "age" + i);
datas.add(data);
}
hssfWorkExcel.createTableRows(datas, new String[] { "name", "age" });
try {
hssfWorkExcel.exportExcel(new FileOutputStream(new File("e:/test1.xlsx")));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
3.自定义模板导出
public void customizationExcel(Map<String, String> dateMap, List<Map<String, String>> dateMapList, HttpServletResponse response) {
try {
SXSSFWorkbook workbook = new SXSSFWorkbook(100);
Sheet sheet = workbook.createSheet();
Font ParamFontStyle = workbook.createFont();
CellStyle cellParamStyle = workbook.createCellStyle();
cellParamStyle.setAlignment(HorizontalAlignment.CENTER);
cellParamStyle.setVerticalAlignment(VerticalAlignment.CENTER);
cellParamStyle.setWrapText(false);
ParamFontStyle.setFontHeightInPoints((short) 11);
ParamFontStyle.setFontName("宋体");
cellParamStyle.setFont(ParamFontStyle);
Font ParamFontStyle1 = workbook.createFont();
CellStyle cellParamStyle1 = workbook.createCellStyle();
cellParamStyle1.setAlignment(HorizontalAlignment.LEFT);
cellParamStyle1.setVerticalAlignment(VerticalAlignment.DISTRIBUTED);
cellParamStyle1.setWrapText(false);
ParamFontStyle1.setFontHeightInPoints((short) 15);
ParamFontStyle1.setFontName("黑体");
ParamFontStyle1.setBold(true);
cellParamStyle1.setFont(ParamFontStyle1);
Font ParamFontStyle2 = workbook.createFont();
CellStyle cellParamStyle2 = workbook.createCellStyle();
cellParamStyle2.setAlignment(HorizontalAlignment.CENTER);
cellParamStyle2.setVerticalAlignment(VerticalAlignment.CENTER);
cellParamStyle2.setWrapText(true);
ParamFontStyle2.setFontHeightInPoints((short) 11);
ParamFontStyle2.setFontName("黑体");
ParamFontStyle2.setBold(true);
cellParamStyle2.setFont(ParamFontStyle2);
Font ParamFontStyle3 = workbook.createFont();
CellStyle cellParamStyle3 = workbook.createCellStyle();
cellParamStyle3.setAlignment(HorizontalAlignment.CENTER);
cellParamStyle3.setVerticalAlignment(VerticalAlignment.CENTER);
cellParamStyle3.setWrapText(true);
ParamFontStyle3.setFontHeightInPoints((short) 18);
ParamFontStyle3.setFontName("黑体");
ParamFontStyle3.setBold(true);
cellParamStyle3.setFont(ParamFontStyle3);
Short height = 390;
sheet.setDefaultRowHeight((Short) height);
sheet.setColumnWidth(0, 5000);
sheet.setColumnWidth(1, 5000);
sheet.setColumnWidth(2, 5000);
sheet.setColumnWidth(3, 5000);
sheet.setColumnWidth(4, 5000);
sheet.setColumnWidth(5, 5000);
sheet.setColumnWidth(6, 5000);
sheet.setColumnWidth(7, 5000);
sheet.setColumnWidth(8, 5000);
sheet.setColumnWidth(9, 5000);
sheet.setColumnWidth(10, 5000);
sheet.setColumnWidth(11, 5000);
sheet.setColumnWidth(12, 5000);
Row rows1 = sheet.createRow(0);
Cell cell1 = rows1.createCell(0);
Cell cell7 = rows1.createCell(1);
Cell cell2 = rows1.createCell(2);
Cell cell8 = rows1.createCell(3);
Cell cell3 = rows1.createCell(6);
Cell cell9 = rows1.createCell(7);
Cell cell4 = rows1.createCell(8);
Cell cell10 = rows1.createCell(9);
Row rows5 = sheet.createRow(1);
Cell cell5 = rows5.createCell(0);
Cell cell11 = rows5.createCell(1);
Row rows6 = sheet.createRow(2);
Cell cell6 = rows6.createCell(0);
Cell cell12 = rows6.createCell(1);
CellRangeAddress region = new CellRangeAddress(0, 0, 3, 5);
sheet.addMergedRegion(region);
CellRangeAddress region1 = new CellRangeAddress(1, 1, 1, 12);
sheet.addMergedRegion(region1);
CellRangeAddress region2 = new CellRangeAddress(2, 2, 1, 12);
sheet.addMergedRegion(region2);
CellRangeAddress region3 = new CellRangeAddress(0, 0, 9, 12);
sheet.addMergedRegion(region3);
cell1.setCellStyle(cellParamStyle2);
cell2.setCellStyle(cellParamStyle2);
cell3.setCellStyle(cellParamStyle2);
cell4.setCellStyle(cellParamStyle2);
cell5.setCellStyle(cellParamStyle2);
cell6.setCellStyle(cellParamStyle2);
cell7.setCellStyle(cellParamStyle);
cell8.setCellStyle(cellParamStyle);
cell9.setCellStyle(cellParamStyle);
cell10.setCellStyle(cellParamStyle);
cell11.setCellStyle(cellParamStyle);
cell12.setCellStyle(cellParamStyle);
cell1.setCellValue("交班日期:");
cell2.setCellValue("科室:");
cell3.setCellValue("班次:");
cell4.setCellValue("时间:");
cell5.setCellValue("全天班次:");
cell6.setCellValue("班制:");
cell7.setCellValue(dateMap.get("交班日期"));
cell8.setCellValue(dateMap.get("科室"));
cell9.setCellValue(dateMap.get("班次"));
cell10.setCellValue((Objects.equals(dateMap.get("开始时间"), null) ? "" : dateMap.get("开始时间") + " - " + (Objects.equals(dateMap.get("结束时间"), null) ? "" : dateMap.get("结束时间"))));
cell11.setCellValue(dateMap.get("全天班次"));
cell12.setCellValue(dateMap.get("白班"));
Row row1 = sheet.createRow(3);
Cell row1Cell0 = row1.createCell(0);
row1Cell0.setCellValue("类别");
row1Cell0.setCellStyle(cellParamStyle2);
Cell row1Cell = row1.createCell(1);
row1Cell.setCellValue("床号");
row1Cell.setCellStyle(cellParamStyle2);
Cell row1Cell1 = row1.createCell(2);
row1Cell1.setCellValue("姓名");
row1Cell1.setCellStyle(cellParamStyle2);
Cell row1Cell2 = row1.createCell(3);
row1Cell2.setCellValue("出生日期");
row1Cell2.setCellStyle(cellParamStyle2);
Cell row1Cell3 = row1.createCell(4);
row1Cell3.setCellValue("入院日期");
row1Cell3.setCellStyle(cellParamStyle2);
Cell row1Cell4 = row1.createCell(5);
row1Cell4.setCellValue("诊断");
row1Cell4.setCellStyle(cellParamStyle2);
Cell row1Cell5 = row1.createCell(6);
row1Cell5.setCellValue("现状");
row1Cell5.setCellStyle(cellParamStyle2);
Cell row1Cell6 = row1.createCell(7);
row1Cell6.setCellValue("背景");
row1Cell6.setCellStyle(cellParamStyle2);
Cell row1Cell7 = row1.createCell(8);
row1Cell7.setCellValue("评估");
row1Cell7.setCellStyle(cellParamStyle2);
Cell row1Cell8 = row1.createCell(9);
row1Cell8.setCellValue("建议");
row1Cell8.setCellStyle(cellParamStyle2);
Cell row1Cell9 = row1.createCell(10);
row1Cell9.setCellValue("P班");
row1Cell9.setCellStyle(cellParamStyle2);
Cell row1Cell10 = row1.createCell(11);
row1Cell10.setCellValue("N班");
row1Cell10.setCellStyle(cellParamStyle2);
Cell row1Cell11 = row1.createCell(12);
row1Cell11.setCellValue("交/接班护士");
row1Cell11.setCellStyle(cellParamStyle2);
int sheet1Colume = 0;
for (Map<String, String> hospitalExcel : dateMapList) {
Row row2 = sheet.createRow(sheet1Colume + 4);
Cell row2Cell = row2.createCell(0);
row2Cell.setCellStyle(cellParamStyle);
row2Cell.setCellValue(hospitalExcel.get("类别"));
Cell row2Cell1 = row2.createCell(1);
row2Cell1.setCellStyle(cellParamStyle);
row2Cell1.setCellValue(hospitalExcel.get("cw"));
Cell row2Cell2 = row2.createCell(2);
row2Cell2.setCellStyle(cellParamStyle);
row2Cell2.setCellValue(hospitalExcel.get("xm"));
Cell row2Cell3 = row2.createCell(3);
row2Cell3.setCellStyle(cellParamStyle);
row2Cell3.setCellValue(hospitalExcel.get("bir"));
Cell row2Cell4 = row2.createCell(4);
row2Cell4.setCellStyle(cellParamStyle);
row2Cell4.setCellValue(hospitalExcel.get("rysj"));
Cell row2Cell5 = row2.createCell(5);
row2Cell5.setCellStyle(cellParamStyle);
row2Cell5.setCellValue(hospitalExcel.get("zd"));
Cell row2Cell6 = row2.createCell(6);
row2Cell6.setCellStyle(cellParamStyle);
row2Cell6.setCellValue(hospitalExcel.get("xz"));
Cell row2Cell7 = row2.createCell(7);
row2Cell7.setCellStyle(cellParamStyle);
row2Cell7.setCellValue(hospitalExcel.get("bj"));
Cell row2Cell8 = row2.createCell(8);
row2Cell8.setCellStyle(cellParamStyle);
row2Cell8.setCellValue(hospitalExcel.get("pg"));
Cell row2Cell9 = row2.createCell(9);
row2Cell9.setCellStyle(cellParamStyle);
row2Cell9.setCellValue(hospitalExcel.get("jy"));
Cell row2Cell10 = row2.createCell(10);
row2Cell10.setCellStyle(cellParamStyle);
row2Cell10.setCellValue(hospitalExcel.get("Pb"));
Cell row2Cell11 = row2.createCell(11);
row2Cell11.setCellStyle(cellParamStyle);
row2Cell11.setCellValue(hospitalExcel.get("Nb"));
Cell row2Cell12 = row2.createCell(12);
row2Cell12.setCellStyle(cellParamStyle);
row2Cell12.setCellValue((Objects.equals(hospitalExcel.get("jbhs"), null) ? "" : (hospitalExcel.get("jbhs")) + "/" + (Objects.equals(hospitalExcel.get("sbhs"), null) ? "" : hospitalExcel.get("sbhs"))));
sheet1Colume++;
}
String fileName = new String(DateUtil.now().getBytes(), "UTF-8") + ".xls";
response.setContentType("application/vnd.ms-excel;charset=UTF-8");
response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
response.setCharacterEncoding("UTF-8");
OutputStream os = response.getOutputStream();
workbook.write(os);
os.flush();
try {
os.close();
} catch (Exception e) {
e.printStackTrace();
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
导出效果