感官评吸系统
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
package com.ruoyi.common.utils.poi;
|
||||
|
||||
import org.apache.poi.ss.usermodel.Row;
|
||||
import org.apache.poi.ss.usermodel.Sheet;
|
||||
import org.apache.poi.ss.usermodel.Workbook;
|
||||
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
||||
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public class ExcelExporter {
|
||||
public static void exportExcel(List<Map<String, Object>> dataList, String[] headers, String fileName, HttpServletResponse response) throws IOException {
|
||||
Workbook workbook = new XSSFWorkbook();
|
||||
Sheet sheet = workbook.createSheet("Sheet1");
|
||||
|
||||
// 创建表头
|
||||
Row headerRow = sheet.createRow(0);
|
||||
for (int i = 0; i < headers.length; i++) {
|
||||
headerRow.createCell(i).setCellValue(headers[i]);
|
||||
}
|
||||
|
||||
// 填充数据
|
||||
int rowNum = 1;
|
||||
for (Map<String, Object> data : dataList) {
|
||||
Row row = sheet.createRow(rowNum++);
|
||||
for (int i = 0; i < headers.length; i++) {
|
||||
row.createCell(i).setCellValue(data.get(headers[i]) == null ? "" : data.get(headers[i]).toString());
|
||||
}
|
||||
}
|
||||
// 返回到客户端
|
||||
try {
|
||||
workbook.write(response.getOutputStream());
|
||||
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
|
||||
response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
|
||||
}catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}finally {
|
||||
workbook.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user