java批量导入数据到excel

1.查询并返回结果list集合

private List<TbTaxOrder> tbTaxOrderList = null;
@RestController
@RequestMapping("/core/order")
public class TbTaxOrderController {
    public static final Logger LOGGER = Logger.getLogger("TbTaxOrderController");
    @Autowired
    private ITbTaxOrderService tbTaxOrderService;
    @Autowired
    private ITbTaxOilStationService stationService;

    private List<TbTaxOrder> tbTaxOrderList = null;
    
    /**
     * 根据取票方式和取票状态查询
     * @param params
     * @return
     */
    @RequestMapping(value = "/getByMethodAndStatus", method = RequestMethod.POST)
    public ResponseVO getByReceiveMethodAndOrderStatus(@RequestBody Map<String, Object> params) {

        try{
            String receiveMethod = (String) params.get("receiveMethod");
            String orderStatus = (String) params.get("orderStatus");
            String stationStaffId = (String) params.get("stationStaffId");
            if(StringUtil.isNotEmpty(orderStatus)){
                String[] list = orderStatus.split(",");
                params.put("list",list);
            }
            if (StringUtil.isEmpty(receiveMethod) || StringUtil.isEmpty(stationStaffId)) {
                return ResultUtil.error("receiveMethod || stationStaffId 为空");
            }
            tbTaxOrderList = tbTaxOrderService.getByReceiveMethodAndOrderStatus(params);
            return ResultUtil.success(tbTaxOrderList);
        }catch (Exception e){
            e.printStackTrace();
            if(e instanceof TaxException){
                return ResultUtil.error(e.getMessage());
            }
            return ResultUtil.error();
        }
    }

    /**
     * 导出开票记录到excel
     * @return
     */
    @RequestMapping(value = "/DownLoadExcel", method = RequestMethod.POST)
    public ResponseVO DownLoadExcel() {

        try{
            //格式化时间
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

            //1、创建工作簿
            Workbook wb = new XSSFWorkbook();
            //1.1、设置表格的格式----居中
            CellStyle cs = wb.createCellStyle();
            //   cs.setAlignment(HorizontalAlignment.CENTER);
            //2.1、创建工作表
            Sheet sheet = wb.createSheet("Excel download");
            //2.2、合并单元格
//            sheet.addMergedRegion(new CellRangeAddress(4, 8, 5, 9));
            //3.1、创建行----表头行
            Row row = sheet.createRow(0);
            //4、创建格
            Cell cell = row.createCell(0);
            cell.setCellValue("油卡号");
            cell.setCellStyle(cs);
            cell = row.createCell(1);
            cell.setCellValue("油卡手机号");
            cell.setCellStyle(cs);
            cell = row.createCell(2);
            cell.setCellValue("开票金额");
            cell.setCellStyle(cs);
            cell = row.createCell(3);
            cell.setCellValue("截止时间");
            cell.setCellStyle(cs);
            cell = row.createCell(4);
            cell.setCellValue("公司名称");
            cell.setCellStyle(cs);
            cell = row.createCell(5);
            cell.setCellValue("开票申请时间");
            cell.setCellStyle(cs);
            cell = row.createCell(6);
            cell.setCellValue("取票状态");
            cell.setCellStyle(cs);
            cell = row.createCell(7);
            cell.setCellValue("取票方式");
            cell.setCellStyle(cs);
            //5、写入实体数据
            if(null != tbTaxOrderList && tbTaxOrderList.size() > 0){
                for (int i = 0; i < tbTaxOrderList.size(); i++) {
                    //3.2、创建行----内容行
                    row = sheet.createRow(i+1);
                    TbTaxOrder order = (TbTaxOrder)tbTaxOrderList.get(i);
                    //第几行第几格  第一行第一格为“code”
                    row.createCell(0).setCellValue(order.getCardCode());
                    row.createCell(1).setCellValue(order.getBindPhone());
                    if(null != order.getBillAmount()){
                        row.createCell(2).setCellValue(order.getBillAmount().toString());
                    }else{
                        row.createCell(2).setCellValue("");
                    }

                    if(null != order.getEndDate()){
                        String endDate = formatter.format(order.getEndDate());
                        row.createCell(3).setCellValue(endDate);
                    }else{
                        row.createCell(3).setCellValue(order.getEndDate());
                    }
                    row.createCell(4).setCellValue(order.getCompany());
                    String insertDate = formatter.format(order.getInsertDate());
                    row.createCell(5).setCellValue(insertDate);
                    if("1".toString().equals(order.getReceiveMethod())){
                        row.createCell(6).setCellValue("未办理");
                    }else if("2".toString().equals(order.getReceiveMethod())){
                        row.createCell(6).setCellValue("开票中");
                    }else if("3".toString().equals(order.getReceiveMethod())){
                        row.createCell(6).setCellValue("开票完成");
                    }else if("4".toString().equals(order.getReceiveMethod())){
                        row.createCell(6).setCellValue("开票请求驳回");
                    }else {
                        row.createCell(6).setCellValue("已邮寄 或 已自提");
                    }
                    if("1".toString().equals(order.getReceiveMethod())){
                        row.createCell(7).setCellValue("默认不选择");
                    }else if("12".toString().equals(order.getReceiveMethod())){
                        row.createCell(7).setCellValue("场站自取");
                    }else{
                        row.createCell(7).setCellValue("邮寄");
                    }

                }

                //6、将文件储存到指定位置
                try {
                    FileOutputStream fout = new FileOutputStream("D:\\" + new Date().getTime() + ".xlsx");
                    wb.write(fout);
                    fout.close();
                    return ResultUtil.success("导出完成");
                } catch (IOException e) {
                    e.printStackTrace();
                    return ResultUtil.error("导出失败");
                }
            }else{
                return ResultUtil.error("tbTaxOrderList 不能为空");
            }

        }catch (Exception e){
            e.printStackTrace();
            if(e instanceof TaxException){
                return ResultUtil.error(e.getMessage());
            }
            return ResultUtil.error();
        }
    }

   
}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

iamlzjoco

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值