添加导出方法
This commit is contained in:
@@ -73,6 +73,28 @@ public class ExcelUtil {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 指定名称、sheet名、title名、数据下载报表
|
||||||
|
*
|
||||||
|
* @param fileName 文件名
|
||||||
|
*/
|
||||||
|
public static void exportExcel(String fileName, String sheetName, String title, Class<?> pojoClass, Collection<?> dataSet) {
|
||||||
|
HttpServletResponse response = HttpServletUtil.getResponse();
|
||||||
|
try (ServletOutputStream outputStream = response.getOutputStream()) {
|
||||||
|
fileName = URLEncoder.encode(fileName, CharsetUtil.UTF_8);
|
||||||
|
response.reset();
|
||||||
|
response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
|
||||||
|
response.setContentType("application/octet-stream;charset=UTF-8");
|
||||||
|
ExportParams exportParams = new ExportParams();
|
||||||
|
exportParams.setSheetName(sheetName);
|
||||||
|
exportParams.setTitle(title);
|
||||||
|
Workbook workbook = ExcelExportUtil.exportExcel(exportParams, pojoClass, dataSet);
|
||||||
|
workbook.write(outputStream);
|
||||||
|
} catch (IOException e) {
|
||||||
|
log.error(">>> 导出数据异常:{}", e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 指定名称、sheet名(多个sheet)、数据(多组数据)下载报表
|
* 指定名称、sheet名(多个sheet)、数据(多组数据)下载报表
|
||||||
*
|
*
|
||||||
@@ -115,7 +137,7 @@ public class ExcelUtil {
|
|||||||
cellStyle.setDataFormat(format.getFormat("@"));
|
cellStyle.setDataFormat(format.getFormat("@"));
|
||||||
for (PullDown pullDown : pullDowns) {
|
for (PullDown pullDown : pullDowns) {
|
||||||
if (pullDown.getIsText()) {
|
if (pullDown.getIsText()) {
|
||||||
ExcelUtil.selectListText(workbook, pullDown.getFirstCol(),cellStyle);
|
ExcelUtil.selectListText(workbook, pullDown.getFirstCol(), cellStyle);
|
||||||
} else {
|
} else {
|
||||||
String colName = numberToExcelColumn(num);
|
String colName = numberToExcelColumn(num);
|
||||||
int sum = pullDown.getStrings().stream().mapToInt(String::length).sum();
|
int sum = pullDown.getStrings().stream().mapToInt(String::length).sum();
|
||||||
@@ -191,17 +213,17 @@ public class ExcelUtil {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @Description:
|
|
||||||
* @param workbook
|
* @param workbook
|
||||||
* @param firstCol
|
* @param firstCol
|
||||||
|
* @Description:
|
||||||
* @Author: wr
|
* @Author: wr
|
||||||
* @Date: 2024/8/20 10:44
|
* @Date: 2024/8/20 10:44
|
||||||
*/
|
*/
|
||||||
public static void selectListText(Workbook workbook, int firstCol,CellStyle cellStyle) {
|
public static void selectListText(Workbook workbook, int firstCol, CellStyle cellStyle) {
|
||||||
Sheet sheet = workbook.getSheetAt(0);
|
Sheet sheet = workbook.getSheetAt(0);
|
||||||
for (int i = 2; i < 65535; i++) {
|
for (int i = 2; i < 65535; i++) {
|
||||||
Row row = sheet.getRow(i);
|
Row row = sheet.getRow(i);
|
||||||
if(ObjectUtil.isNull(row)){
|
if (ObjectUtil.isNull(row)) {
|
||||||
row = sheet.createRow(i);
|
row = sheet.createRow(i);
|
||||||
}
|
}
|
||||||
Cell cell = row.createCell(firstCol);
|
Cell cell = row.createCell(firstCol);
|
||||||
|
|||||||
Reference in New Issue
Block a user