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

来源:互联网 发布:免费cf手游刷枪软件 编辑:程序博客网 时间:2024/06/08 09:37
{
        // 创建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 0
原创粉丝点击