这里是导出一个excel文件,里面有两个sheet
分别是税赋预测表和发票汇总表
controller层
@requestMapping(value = "/exportExcel/taxForecast", method = Method.GET)
public Result exportExcel(String uid, String accountCodeList, Date startDate, Date endDate, HttpServletResponse response) {
log.info("帐套查询参数{},开始时间{},结束时间{},uid:{}",, accountCodeList, startDate, endDate, uid);
try {
List taxFore = iTaxForeCast.getTaxFore(accountCodeList, uid, startDate, endDate);
getVO(taxFore);
stripTrailingZeros(taxFore);
List taxForeEOList = new ArrayList<>();
List invoiceTaxForeEOS = new ArrayList<>();
taxFore.forEach(taxForeDTO -> {
InvoiceTaxForeEO invoiceTaxForeEO = new InvoiceTaxForeEO();
BeanCopyUtils.copy(taxForeDTO, invoiceTaxForeEO);
invoiceTaxForeEOS.add(invoiceTaxForeEO);
TaxForeEO taxForeEO = new TaxForeEO();
BeanCopyUtils.copy(taxForeDTO, taxForeEO);
taxForeEOList.add(taxForeEO);
});
Map> map = new HashMap<>();
map.put("税赋预测表", taxForeEOList);
map.put("发票汇总表", invoiceTaxForeEOS);
EasyExcelUtils.createExcelStreamMutilByEaysExcel(response, map, ExcelTypeEnum.XLSX);
} catch (Exception e) {
e.printStackTrace();
return Result.fail("500", "导出失败");
}
return Result.success("导出成功");
}
数据模型 excelDate
/**
* @ClassName TaxForeCastDTO
* @Author laixiaoxing
* @Date 2019/1/26 下午2:09
* @Description 发票汇总表
* @Version 1.0
*/
@Data
@ToString
public class InvoiceTaxForeEO extends BaseRowModel {
@ExcelProperty(value = "账套编码")
private String organizeId;
@ExcelProperty(value = "账套名称")
private String organizeName;
@ExcelProperty(value = "期初留抵税额")
private BigDecimal invoicePeriodTax;
@ExcelProperty(value = "销项不含税金额")
private BigDecimal deductionFullTax;
@ExcelProperty(value = "销项税额")
private BigDecimal deductionTax;
@ExcelProperty(value = "进项税额")
private BigDecimal inputVATTax;
@ExcelProperty(value = "进项转出税额")
private BigDecimal inputToOutputTax;
@ExcelProperty(value = "期末留抵税额")
private BigDecimal invoiceEndTax;
}
/**
* @ClassName TaxForeCastDTO
* @Author laixiaoxing
* @Date 2019/1/26 下午2:09
* @Description 税赋预测表dto
* @Version 1.0
*/
@Data
@ToString
public class TaxForeEO extends BaseRowModel{
@ExcelProperty(value = "账套编码",index = 0)
private String organizeId;
@ExcelProperty(value = "账套名称",index = 1)
private String organizeName;
@ExcelProperty(value = "期初留抵税额",index = 2)
private BigDecimal periodTax;
@ExcelProperty(value = "进项税额",index = 3)
private BigDecimal inputVATTax;
@ExcelProperty(value = "进项转出税额",index = 4)
private BigDecimal inputToOutputTax;
@ExcelProperty(value = "销项不含税金额",index = 5)
private BigDecimal deductionFullTax;
@ExcelProperty(value = "销项税额",index = 6)
private BigDecimal deductionTax;
@ExcelProperty(value = "简易征收销项不含税金额",index = 7)
private BigDecimal simpleFullTax;
@ExcelProperty(value = "简易征收销项税额",index = 8)
private BigDecimal simpleTax;
@ExcelProperty(value = "未开票直租不含税金额",index = 9)
private BigDecimal uninvoicedDirectRentFullTax;
@ExcelProperty(value = "未开票直租税额",index = 10)
private BigDecimal uninvoicedDirectRentTax;
@ExcelProperty(value = "未开票回租不含税金额",index = 11)
private BigDecimal uninvoicedLeasebackRentFullTax;
@ExcelProperty(value = "未开票回租税额",index = 12)
private BigDecimal uninvoicedLeasebackRentTax;
@ExcelProperty(value = "购置税扣除额",index = 13)
private BigDecimal purchaseTaxDeduction;
@ExcelProperty(value = "期末留抵税额",index = 14)
private BigDecimal endTax;
@ExcelProperty(value = "应缴增值税",index = 15)
private BigDecimal VATpayable;
}
封装的工具类
/**
* @ClassName EasyExcelUtils
* @Author laixiaoxing
* @Date 2019/1/30 下午11:59
* @Description 封装的EasyExcel导出工具类
* @Version 1.0
*/
@Slf4j
public class EasyExcelUtils {
/**
* @Author laixiaoxing
* @Description 导出excel 支持一张表导出多个sheet
* @Param OutputStream 输出流
* Map sheetName和每个sheet的数据
* ExcelTypeEnum 要导出的excel的类型 有ExcelTypeEnum.xls 和有ExcelTypeEnum.xlsx
* @Date 上午12:16 2019/1/31
*/
public static void createExcelStreamMutilByEaysExcel(HttpServletResponse response, Map> SheetNameAndDateList, ExcelTypeEnum type) throws UnsupportedEncodingException {
if (checkParam(SheetNameAndDateList, type)) return;
try {
response.setContentType("multipart/form-data");
response.setCharacterEncoding("utf-8");
response.setHeader("Content-disposition", "attachment;filename=" + "default" + type.getValue());
ServletOutputStream out = response.getOutputStream();
ExcelWriter writer = new ExcelWriter(out, type, true);
setSheet(SheetNameAndDateList, writer);
writer.finish();
out.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* @Author laixiaoxing
* @Description //setSheet数据
* @Date 上午12:39 2019/1/31
*/
private static void setSheet(Map> SheetNameAndDateList, ExcelWriter writer) {
int sheetNum = 1;
for (Map.Entry> stringListEntry : SheetNameAndDateList.entrySet()) {
Sheet sheet = new Sheet(sheetNum, 0, stringListEntry.getValue().get(0).getClass());
sheet.setSheetName(stringListEntry.getKey());
writer.write(stringListEntry.getValue(), sheet);
sheetNum++;
}
}
/**
* @Author laixiaoxing
* @Description 校验参数
* @Date 上午12:39 2019/1/31
*/
private static boolean checkParam(Map> SheetNameAndDateList, ExcelTypeEnum type) {
if (CollectionUtils.isEmpty(SheetNameAndDateList)) {
log.error("SheetNameAndDateList不能为空");
return true;
} else if (type == null) {
log.error("导出的excel类型不能为空");
return true;
}
return false;
}
}