java poi生成excel 有下载提示

java poi 两种export excel: (缺失包的,自行引入)

一:直接导出到制定的路径中。

@Service
public class ExportExcelServiceImpl  implements ExportExcelService  {

public static String filePrex = "/home/echo/桌面/";

public Result<String> exportToExcel(String ids, HttpServletResponse response){

      List<Integer> billIds = new ArrayList<>();
      String[] stringArr = ids.split(",");
      for (String id : stringArr){
        billIds.add(Integer.parseInt(id));
      }
      List<DispatchBill> bills = billDao.selectByIds(billIds);
      List<DispatchBillEntry> billEntries =  entryDao.selectByDispatchBill(billIds);
      Workbook wb = new HSSFWorkbook();
      makeSheet(wb, bills,billEntries);

      Date date=new Date();
      SimpleDateFormat sdf1=new SimpleDateFormat("YYYY-MM-dd");
      SimpleDateFormat sdf2=new SimpleDateFormat("hh-mm-ss");
      String time1=sdf1.format(date);
      String time2=sdf2.format(date);
      String filename = time2+".xls";
      String path = filePrex  +time1+ "/"+time2+".xls";


      try {
        // path是指欲下载的文件的路径。
        File file = new File(path);
        // 取得文件名。
        String filename = file.getName();
        if (!file.getParentFile().exists()) {
          file.getParentFile().mkdir();
        }
        //输出流形式 导出excel
        FileOutputStream fileOut = new FileOutputStream(file);
        wb.write(fileOut);

      } catch (Exception e) {
        e.printStackTrace();
      }
      return Result.wrapSuccessfulResult(null);

    }

}

二:页面下载方式导出。

@Service
public class ExportExcelServiceImpl  implements ExportExcelService  {

public static String filePrex = "/home/echo/桌面/";

public Result<String> exportToExcel(String ids, HttpServletResponse response){

      List<Integer> billIds = new ArrayList<>();
      String[] stringArr = ids.split(",");
      for (String id : stringArr){
        billIds.add(Integer.parseInt(id));
      }
      List<DispatchBill> bills = billDao.selectByIds(billIds);
      List<DispatchBillEntry> billEntries =  entryDao.selectByDispatchBill(billIds);
      Workbook wb = new HSSFWorkbook();
      makeSheet(wb, bills,billEntries);
      Date date=new Date();
      SimpleDateFormat sdf1=new SimpleDateFormat("YYYY-MM-dd");
      SimpleDateFormat sdf2=new SimpleDateFormat("hh-mm-ss");
      String time2=sdf2.format(date);
      String filename = time2+".xls";
      try {
        //页面 download
        // 清空response
        response.reset();
        // 设置response的Header
        response.setHeader("Content-Disposition", "attachment;filename=" + filename);
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        ServletOutputStream sos = null;
        sos = response.getOutputStream();
        wb.write(sos);
        sos.flush();
        sos.close();
      } catch (Exception e) {
        e.printStackTrace();
      }
      return Result.wrapSuccessfulResult(null);
    }

  public void makeSheet(Workbook wb,List<DispatchBill> bills,List<DispatchBillEntry> billEntries){
      Sheet sheet = wb.createSheet(" DispatchBill export to Excel Demo");
      Row firstRow = sheet.createRow(0);
      firstRow.createCell(0).setCellValue("序号");
      firstRow.createCell(2).setCellValue("车牌号");
      firstRow.createCell(3).setCellValue("司机");
  }

  

}


评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值