<!-- Apache POI for Excel file manipulation -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>5.2.3</version> <!-- 请使用最新版本 -->
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>5.2.2</version>
</dependency>
@ApiOperation(value ="导出excel", produces = "application/octet-stream")
@PostMapping("/export")
public void export(HttpServletResponse response, String fileName,String subject) throws IOException {
paperEnumAndConfigService.exportExcel(response,fileName,subject);
}
//假数据 直接运行
public void exportExcel(HttpServletResponse response, String fileName, String subject) throws IOException {
// 示例表头
List<String> headers = new ArrayList<>();
headers.add("ID");
headers.add("Name");
headers.add("Age");
headers.add("Gender");
headers.add("Department");
// 示例假数据
List<List<Object>> data = new ArrayList<>();
data.add(Arrays.asList("1", "John Doe", "28", "Male", "Engineering"));
data.add(Arrays.asList("2", "Jane Smith", "32", "Female", "HR"));
data.add(Arrays.asList("3", "Sam Johnson", "45", "Male", "Marketing"));
data.add(Arrays.asList("4", "Emily Davis", "29", "Female", "Engineering"));
data.add(Arrays.asList("5", "Michael Brown", "38", "Male", "Sales"));
// 创建 Excel 工作簿
XSSFWorkbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("Sheet 1");
// 写入表头
Row headerRow = sheet.createRow(0);
for (int i = 0; i < headers.size(); i++) {
Cell cell = headerRow.createCell(i);
// 设置表头数据
cell.setCellValue(headers.get(i));
}
// 写入数据行
int rowIndex = 1;
for (List<Object> rowData : data) {
Row row = sheet.createRow(rowIndex++);
for (int i = 0; i < rowData.size(); i++) {
Cell cell = row.createCell(i);
Object value = rowData.get(i);
if (value instanceof String) {
cell.setCellValue((String) value);
} else if (value instanceof Integer) {
cell.setCellValue((Integer) value);
} else if (value instanceof Double) {
cell.setCellValue((Double) value);
}
}
}
// 设置 HTTP 响应头,通知浏览器这是一个 Excel 文件并触发下载
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + ".xlsx\"");
response.setCharacterEncoding("UTF-8");
// 使用输出流将 Excel 写入到 HTTP 响应流
try (OutputStream out = response.getOutputStream()) {
workbook.write(out); // 将 Excel 数据写入响应流
} finally {
workbook.close(); // 确保工作簿被关闭
}
}
private static List<List<String>> convertJsonToList(String jsonString) {
List<List<String>> result = new ArrayList<>();
// 去除首尾的方括号
jsonString = jsonString.substring(1, jsonString.length() - 1);
String[] rows = jsonString.split("],\\[");
for (String row : rows) {
row = row.replaceAll("^\"|\"$", "");
String[] cells = row.split(",");
List<String> rowList = new ArrayList<>(Arrays.asList(cells));
result.add(rowList);
}
return result;
}
// 将 parsedDataList 插入到 Excel 中
private static void insertParsedDataList(Sheet sheet, List<List<String>> parsedDataList) {
int currentRowIndex = 10;
insert(sheet, parsedDataList, currentRowIndex);
}
// 插入行到 Excel 表格
static void insert(Sheet sheet, List<List<String>> parsedDataList, int currentRowIndex) {
insertHang(sheet, parsedDataList, currentRowIndex);
}
// 将数据行插入 Excel 表格
public static void insertHang(Sheet sheet, List<List<String>> parsedDataList, int currentRowIndex) {
for (List<String> rowData : parsedDataList) {
Row row = sheet.createRow(currentRowIndex++);
for (int i = 0; i < rowData.size(); i++) {
Cell cell = row.createCell(i);
cell.setCellValue(rowData.get(i));
}
}
}
//真的数据
public void exportExcel(HttpServletResponse response, String fileName, String subject) throws IOException {
// 示例数据(使用 ArrayList 来初始化数据)
List<String> headers = new ArrayList<>();
PaperEnumListParam paperEnumListParam = new PaperEnumListParam();
paperEnumListParam.setSubject(subject);
paperEnumListParam.setIsTemplate(1L);
List<PaperEnumListVO> paperEnumListVOS = paperEnumListService.listData(paperEnumListParam);
paperEnumListVOS.forEach(paperEnumListPO -> {
headers.add(paperEnumListPO.getName());
});
// 创建数据行
List<List<Object>> data = new ArrayList<>();
// 创建 Excel 工作簿
// 创建 Excel 文件(XSSFWorkbook 是 Excel 2007及以后的版本)
XSSFWorkbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("Sheet 1");
PaperEnumListVO paperEnumListVO = new PaperEnumListVO();
paperEnumListVO.setId(1859904874234384299L);
paperEnumListVO.setSubjectCode("注意事项");
paperEnumListVO.setIsTemplate(1L);
PaperEnumListVO paperEnum = paperEnumListService.queryOneData(paperEnumListVO);
// 示例字符串 (假设这是 JSON 格式数据)
String careful = paperEnum.getName();
// 将 JSON 字符串转换回 List<List<String>>
List<List<String>> parsedDataList = convertJsonToList(careful);
// 写入表头
Row headerRow = sheet.createRow(0);
for (int i = 0; i < headers.size(); i++) {
Cell cell = headerRow.createCell(i);
// 设置表头数据
cell.setCellValue(headers.get(i));
}
// 写入初始数据行
int rowIndex = 1;
for (List<Object> rowData : data) {
Row row = sheet.createRow(rowIndex++);
for (int i = 0; i < rowData.size(); i++) {
Cell cell = row.createCell(i);
Object value = rowData.get(i);
if (value instanceof String) {
cell.setCellValue((String) value);
} else if (value instanceof Integer) {
cell.setCellValue((Integer) value);
} else if (value instanceof Double) {
cell.setCellValue((Double) value);
}
}
}
// 从第10行开始插入 parsedDataList 的数据
insertParsedDataList(sheet, parsedDataList);
// 设置 HTTP 响应头,通知浏览器这是一个 Excel 文件并触发下载
// 设置为 Excel 2007及以后的格式
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + ".xlsx\"");
response.setCharacterEncoding("UTF-8");
// 使用输出流将 Excel 写入到 HTTP 响应流
try (OutputStream out = response.getOutputStream()) {
workbook.write(out); // 将 Excel 数据写入响应流
} finally {
workbook.close(); // 确保工作簿被关闭
}
// // 将 Excel 写入文件
// try (FileOutputStream fileOut = new FileOutputStream("E:\\example.xlsx")) {
// workbook.write(fileOut);
// } finally {
// workbook.close();
// }
}
private static List<List<String>> convertJsonToList(String jsonString) {
List<List<String>> result = new ArrayList<>();
// 去除首尾的方括号
jsonString = jsonString.substring(1, jsonString.length() - 1);
String[] rows = jsonString.split("],\\[");
for (String row : rows) {
row = row.replaceAll("^\"|\"$", "");
String[] cells = row.split(",");
List<String> rowList = new ArrayList<>(Arrays.asList(cells));
result.add(rowList);
}
return result;
}
// 将 parsedDataList 插入到 Excel 中
private static void insertParsedDataList(Sheet sheet, List<List<String>> parsedDataList) {
int currentRowIndex = 10;
insert(sheet, parsedDataList, currentRowIndex);
}
// 插入行到 Excel 表格
static void insert(Sheet sheet, List<List<String>> parsedDataList, int currentRowIndex) {
insertHang(sheet, parsedDataList, currentRowIndex);
}
// 将数据行插入 Excel 表格
public static void insertHang(Sheet sheet, List<List<String>> parsedDataList, int currentRowIndex) {
for (List<String> rowData : parsedDataList) {
Row row = sheet.createRow(currentRowIndex++);
for (int i = 0; i < rowData.size(); i++) {
Cell cell = row.createCell(i);
cell.setCellValue(rowData.get(i));
}
}
}

9415

被折叠的 条评论
为什么被折叠?



