java下载excel文件源码(可选择保存文件路径)

{
        // 创建Excel的工作书册 Workbook,对应到一个excel文档
        HSSFWorkbook wb = new HSSFWorkbook();
        try {
            request.setCharacterEncoding("UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        response.setHeader("Cache-Control", "private");
        response.setContentType("application/vnd.ms-excel;charset=utf-8");
        response.setHeader("Content-Type", "application/force-download");
        String title = "XXX";
        title = this.processFileName(request, title);
        response.setHeader("Content-disposition", "attachment;filename=" + title + ".xls");




        // 创建Excel的工作sheet,对应到一个excel文档的tab
        HSSFSheet sheet = wb.createSheet("sheet1");
        //设置列宽
        sheet.setColumnWidth(0, 13 * 256);
        sheet.setColumnWidth(1, 13 * 256);
        sheet.setColumnWidth(2, 40 * 256);
        sheet.setColumnWidth(3, 13 * 256);
        sheet.setColumnWidth(4, 13 * 256);
        sheet.setColumnWidth(5, 13 * 256);
        sheet.setColumnWidth(6, 13 * 256);
        sheet.setColumnWidth(7, 13 * 256);
        sheet.setColumnWidth(8, 13 * 256);


        //加粗字体
        org.apache.poi.ss.usermodel.Font boldFont =wb.createFont();
       // boldFont.setItalic(true);
        boldFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);//粗体显示


        HSSFCellStyle headerStyle = wb.createCellStyle();
        headerStyle.setFont(boldFont);
        headerStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        headerStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
        headerStyle.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);
        headerStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
        headerStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);


        HSSFCellStyle headerStyle1 = wb.createCellStyle();
        headerStyle1.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        headerStyle1.setBorderTop(HSSFCellStyle.BORDER_THIN);
        headerStyle1.setBorderLeft(HSSFCellStyle.BORDER_THIN);
        //学校不居中
        HSSFCellStyle headerStyle2 = wb.createCellStyle();
        headerStyle1.setBorderTop(HSSFCellStyle.BORDER_THIN);
        headerStyle1.setBorderLeft(HSSFCellStyle.BORDER_THIN);


        // 创建Excel的sheet的一行
        HSSFRow row = sheet.createRow(0);
        //设置行高
        sheet.createRow(0).setHeightInPoints(22);


       // row.setHeight((short) 15.625);
        row.createCell(0).setCellValue("A");
        row.getCell(0).setCellStyle(headerStyle);
        row.createCell(1, HSSFCell.CELL_TYPE_STRING).setCellValue("B");
        row.getCell(1).setCellStyle(headerStyle);
        row.createCell(2, HSSFCell.CELL_TYPE_STRING).setCellValue("C");
        int count = list.size();
        LOGGER.info("count------------->"+ count);


        for(int i=0;i<count;i++){
            SchoolScore vo =  list.get(i) ;
            row = sheet.createRow(i + 1);
            sheet.createRow(i+1).setHeightInPoints(22);
          //  row.setHeight((short) 15.625);
            row.createCell(0, HSSFCell.CELL_TYPE_STRING).setCellValue(vo.getSsID().getSubject().getName());
            row.getCell(0).setCellStyle(headerStyle1);
            row.createCell(1, HSSFCell.CELL_TYPE_STRING).setCellValue(vo.getSchoolCode());
            row.getCell(1).setCellStyle(headerStyle1);
            row.createCell(2, HSSFCell.CELL_TYPE_STRING).setCellValue(vo.getSchoolName());
            row.getCell(2).setCellStyle(headerStyle2);
        }
        try {
            OutputStream out = response.getOutputStream();
            wb.write(out);
            out.flush();
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
       return;
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值