您的位置:首页 > 编程语言 > Java开发

java poi生成excel 有下载提示

2014-08-07 11:32 351 查看
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("司机");

  }

  

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  poi excel 有提示