/**-----------------------------根据实体导出数据---------------------------------**/
public void finacePayrollExport(@RequestBody 实体对象 实体对象, HttpServletResponse response) {
try {
response.setHeader("content-Type", "application/vnd.ms-excel");
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("名称", "UTF-8") + ".xlsx");
response.setCharacterEncoding("UTF-8");
List<实体对象> list = finacePayrollService.finacePayrollExport(finacePayroll);
Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(), 实体对象.class, list);
workbook.write(response.getOutputStream());
} catch (Exception e) {
e.printStackTrace();
log.error("导出在职人员工资单异常", e);
}
}
/--------------------------------------对象实体------------------------------------------/
@Data
@ExcelTarget("finacepayrollexcel")
public class 实体对象 implements Serializable {
@Excel(name = "年月", orderNum = "1", isImportField = "date")
private LocalDate payrollMonth;
@Excel(name = "员工编码", orderNum = "2", isImportField = "type")
private String empNo;
@Excel(name = "员工姓名", orderNum = "3", isImportField = "type")
private String empName;
@Excel(name = "院校代码", orderNum = "4", isImportField = "type")
private String collegeCode;
@Excel(name = "院校名称", orderNum = "5", isImportField = "type")
private String collegeDeptName;
@Excel(name = "部门代码", orderNum = "6", isImportField = "type")
private String deptCode;
@Excel(name = "部门名称", orderNum = "7", isImportField = "type")
private String empDeptName;
}
第二种
/**----------------------------根据模板导出数据---------------------------------**/
public void exportExcel(HttpServletResponse response) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("pw22", pw22List);
map.put("month", 可以放单独的时间和其它 );
// 设置导出配置
// 获取导出excel指定模版
TemplateExportParams params = new TemplateExportParams("doc/manpowerStatistics.xls", true);
// 设置sheetName,若不设置该参数,则使用得原本得sheet名称
String[] sheetNameArray = {"PW22", "PW23", "PW24", "PW28", "PW29", "PW30", "PW31", "PW32", "PW33", "PW36", "PW37"};
params.setSheetName(sheetNameArray);
// 导出excel
Workbook workbook = ExcelExportUtil.exportExcel(params, map1);
/**合并单元格**/
Sheet sheet = workbook.getSheetAt(0);
PoiMergeCellUtil.mergeCells(sheet, 13, 1, 0, 0);
String exportFileName = "名称.xslx";
ExcelUtil.export(response, workbook, exportFileName);
}
/-------------------------导出工具类-------------------------------------------------/
public class ExcelUtil {
// Excel 导出 通过浏览器下载的形式
public static void export(HttpServletResponse response, Workbook workbook, String fileName) {
BufferedOutputStream bufferedOutPut = null;
try {
response.setHeader("Content-Disposition",
"attachment;filename=" + new String(fileName.getBytes("UTF-8"), "iso8859-1"));
response.setContentType("application/vnd.ms-excel;charset=UTF-8");
response.setHeader("Pragma", "no-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
try {
bufferedOutPut = new BufferedOutputStream(response.getOutputStream());
workbook.write(bufferedOutPut);
bufferedOutPut.flush();
} catch (IOException e) {
e.printStackTrace();
log.error("导出文件异常",e);
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
log.error("导出文件异常",e);
} finally {
if (bufferedOutPut != null) {
try {
bufferedOutPut.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
![在这里插入图片描述](https://img-blog.csdnimg.cn/1ffcee3825d24331ac365aa363a6f0a7.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5oSf6LCi55u46YGH44CC77-9,size_20,color_FFFFFF,t_70,g_se,x_16)
第三种
/---------------------------表头-------------------------------/
![在这里插入图片描述](https://img-blog.csdnimg.cn/61f035c9b3444d5abb972c394e52f7e9.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5oSf6LCi55u46YGH44CC77-9,size_20,color_FFFFFF,t_70,g_se,x_16)
public Workbook exportOutPaidList(ReportPaid reportPaid, HttpServletResponse response) throws Exception {
List<ReportPaid> dataList = baseMapper.getExportPaidList(reportPaid);
//如果不同表头,
TemplateExportParams params = new TemplateExportParams("doc/reportPaid.xlsx");
Map<String, Object> mapList = new HashMap<>();
mapList.put("titlePrefix", "劳务派遣员工其他工资");
//到这可以不写,然后创建row cell 创建表头,
Workbook workbook = null;
try {
workbook = ExcelExportUtil.exportExcel(params, mapList);
Sheet sheet = workbook.getSheetAt(0);
Row row;
Cell cell;
CellStyle cellStyle = ExcelUtil.getCellStyle(workbook, null, null, false, null);
for (int i = 0;i < dataList.size(); i++) {
ReportPaid obj = dataList.get(i);
row = sheet.createRow(i + 3);
cell = row.createCell(0);
cell.setCellStyle(cellStyle);
cell.setCellValue((i + 1) + "");
cell = row.createCell(1);
cell.setCellStyle(cellStyle);
cell.setCellValue(obj.getReportMonth().format(DateTimeFormatter.ofPattern("yyyy-MM")));
cell = row.createCell(2);
cell.setCellStyle(cellStyle);
cell.setCellValue(obj.getEmpNo());
cell = row.createCell(3);
cell.setCellStyle(cellStyle);
cell.setCellValue(stringIsEmpty(CalculateUtil.division(obj.getReturnPay() ,100,2)+ ""));
cell = row.createCell(4);
cell.setCellStyle(cellStyle);
cell.setCellValue(stringIsEmpty(CalculateUtil.division(obj.getPersonInvigilationPay() ,100,2)+ ""));
cell = row.createCell(5);
cell.setCellStyle(cellStyle);
cell.setCellValue(stringIsEmpty(CalculateUtil.division(obj.getOutPay() ,100,2)+ ""));
}
} catch (Exception e) {
e.printStackTrace();
}
response.setCharacterEncoding("UTF-8");
response.setHeader("content-Type", "application/vnd.ms-excel");
response.setHeader("Content-Disposition",
"attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
workbook.write(response.getOutputStream());
}
java导出方法
于 2022-02-15 16:10:44 首次发布