1.导出excel 带合计
如:
public void export(DriverAndGuestMealQueryVO vo) {
DriverAndGuestMealListDTO riceLiquidationPage = page(vo);
// 创建一个Excel工作簿
Workbook workbook = new XSSFWorkbook();
// 创建一个工作表sheet
Sheet sheet = workbook.createSheet("司机补贴表");
// 创建表头行
Row headerRow = sheet.createRow(0);
String[] headers = {
"日期", "餐厅","餐段", "餐别", "消费金额", "客数", "调整金额", "结算金额"
};
int defaultColumnWidth = 20 * 256;
for (int i = 0; i < headers.length; i++) {
sheet.setColumnWidth(i, defaultColumnWidth);
Cell cell = headerRow.createCell(i);
cell.setCellValue(headers[i]);
}
try {
List<DriverAndGuestMealQueryDTO> data = riceLiquidationPage.getPageInfo().getList();
// 填充数据
int rowNum = 1;
if (data.size() > 0) {
for (DriverAndGuestMealQueryDTO dto : data) {
Row dataRow = sheet.createRow(rowNum++);
int colNum = 0;
dataRow.createCell(colNum++).setCellValue(dto.getDriverDate());
dataRow.createCell(colNum++).setCellValue(dto.getRestaurant());
dataRow.createCell(colNum++).setCellValue(dto.getMealPeriod());
dataRow.createCell(colNum++).setCellValue(dto.getMealType());
dataRow.createCell(colNum++).setCellValue(String.valueOf(dto.getAmountSpent()));
dataRow.createCell(colNum++).setCellValue(dto.getNumberOfGuests());
dataRow.createCell(colNum++).setCellValue(String.valueOf(dto.getAdjustmentAmount()));
dataRow.createCell(colNum++).setCellValue(String.valueOf(dto.getSettlementAmount()));
// 注意:原始数据中可能没有“日期”字段,这里假设你有一个方法来获取它,或者你可以省略这一列
// dataRow.createCell(colNum++).setCellValue(dto.getDate()); // 假设有一个getDate()方法
}
}
Row summaryRow = sheet.createRow(rowNum);
Cell totalCells[] = new Cell[headers.length];
totalCells[0] = summaryRow.createCell(0);
totalCells[0].setCellValue("合计 ");
totalCells[headers.length - 4] = summaryRow.createCell(headers.length - 4);
totalCells[headers.length - 4].setCellValue(String.valueOf(riceLiquidationPage.getAmountSpentSum()));
totalCells[headers.length - 3] = summaryRow.createCell(headers.length - 3);
totalCells[headers.length - 3].setCellValue(String.valueOf(riceLiquidationPage.getNumberOfGuestsSum()));
totalCells[headers.length - 2] = summaryRow.createCell(headers.length - 2);
totalCells[headers.length - 2].setCellValue(String.valueOf(riceLiquidationPage.getAdjustmentAmountSum()));
totalCells[headers.length-1] = summaryRow.createCell(headers.length-1);
totalCells[headers.length-1].setCellValue(String.valueOf(riceLiquidationPage.getSettlementAmountSum()));
// 设置响应内容类型
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
// 设置响应头,告诉浏览器下载的文件名
response.setHeader("Content-Disposition", "attachment; filename=司机补贴表.xlsx");
} catch (Exception e) {
e.getMessage();
}
// 将工作簿写入响应输出流
try {
workbook.write(response.getOutputStream());
// 关闭工作簿释放资源
workbook.close();
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
// 关闭工作簿释放资源
try {
workbook.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}