java导出excel文件

导入导出 专栏收录该内容
3 篇文章 0 订阅

前端调用:location.href = baseUrl + ‘rwgl/jxrl/exportExcel’;

public void exportExcel(HttpServletRequest request, HttpServletResponse response, HttpSession session) {

        //数据
        List<JxrlwhEntity> jxrlwhData = (List<JxrlwhEntity>) session.getAttribute("jxrlwhData");
        //创建HSSFWorkbook对象(excel的文档对象)  
        HSSFWorkbook wb = new HSSFWorkbook();
        // 建立新的sheet对象(excel的表单)
        HSSFSheet sheet = wb.createSheet("sheet1");

        sheet.setColumnWidth(0, 4500);
        sheet.setColumnWidth(1, 2000);
        sheet.setColumnWidth(2, 4500);
        sheet.setColumnWidth(3, 4500);
        sheet.setColumnWidth(4, 4500);

        //设置表头样式
        HSSFCellStyle cellStyle0 = wb.createCellStyle();
        cellStyle0.setVerticalAlignment(VerticalAlignment.CENTER);    //设置垂直居中
        cellStyle0.setAlignment(HorizontalAlignment.CENTER);    //设置水平居中
        cellStyle0.setBorderBottom(BorderStyle.THIN); // 下边框
        cellStyle0.setBorderLeft(BorderStyle.THIN);// 左边框
        cellStyle0.setBorderTop(BorderStyle.THIN);// 上边框
        cellStyle0.setBorderRight(BorderStyle.THIN);// 右边框
        HSSFFont font = wb.createFont();
        font.setBold(true); // 加粗显示
        cellStyle0.setFont(font);
        //设置内容样式
        HSSFCellStyle cellStyle = wb.createCellStyle();
        cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);    //设置垂直居中
        cellStyle.setAlignment(HorizontalAlignment.CENTER);    //设置水平居中
        cellStyle.setBorderBottom(BorderStyle.THIN); // 下边框
        cellStyle.setBorderLeft(BorderStyle.THIN);// 左边框
        cellStyle.setBorderTop(BorderStyle.THIN);// 上边框
        cellStyle.setBorderRight(BorderStyle.THIN);// 右边框
        cellStyle.setWrapText(true); // 自动换行

        //创建第一行
        HSSFRow row = sheet.createRow(0);
        row.setHeightInPoints(30); // 行高
        //插入第一行数据的表头
        HSSFCell cell0 = row.createCell(0);
        cell0.setCellStyle(cellStyle0);
        cell0.setCellValue("课程名称");
        HSSFCell cell1 = row.createCell(1);
        cell1.setCellStyle(cellStyle0);
        cell1.setCellValue("简称");
        HSSFCell cell2 = row.createCell(2);
        cell2.setCellStyle(cellStyle0);
        cell2.setCellValue("考核方式");
        HSSFCell cell3 = row.createCell(3);
        cell3.setCellStyle(cellStyle0);
        cell3.setCellValue("理论课时");
        HSSFCell cell4 = row.createCell(4);
        cell4.setCellStyle(cellStyle0);
        cell4.setCellValue("实践课时");

        //写入数据
        for (int i=0;i<jxrlwhData.size();i++){
            HSSFRow nrow=sheet.createRow(i+1);
            nrow.setHeightInPoints(25);
            // 课程名称
            HSSFCell ncell0 = nrow.createCell(0);
            ncell0.setCellStyle(cellStyle);
            ncell0.setCellValue(jxrlwhData.get(i).getKcmc());
            sheet.autoSizeColumn(0, true);
            //简称
            HSSFCell ncell1 = nrow.createCell(1);
            ncell1.setCellStyle(cellStyle);
            ncell1.setCellValue(jxrlwhData.get(i).getKcjc());
            //考核方式
            HSSFCell ncell2 = nrow.createCell(2);
            ncell2.setCellStyle(cellStyle);
            ncell2.setCellValue(jxrlwhData.get(i).getKhfs());
            //理论课时
            HSSFCell ncell3 = nrow.createCell(3);
            ncell3.setCellStyle(cellStyle);
            ncell3.setCellValue(jxrlwhData.get(i).getLlxs());
            //实践课时
            HSSFCell ncell4 = nrow.createCell(4);
            ncell4.setCellStyle(cellStyle);
            ncell4.setCellValue(jxrlwhData.get(i).getSjxs());
        }
        OutputStream output;
        try {
            output = response.getOutputStream();
            //清空缓存
            response.reset();
            //定义浏览器响应表头,顺带定义下载名,比如students(中文名需要转义)
            response.setHeader("Content-disposition", "attachment;filename="+new String("教学实施计划清单".getBytes(),"iso-8859-1")+".xls");
            //定义下载的类型,标明是excel文件
            response.setContentType("application/vnd.ms-excel");
            request.setCharacterEncoding("UTF-8");
            //这时候把创建好的excel写入到输出流
            wb.write(output);
            //养成好习惯,出门记得随手关门
            output.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
  • 0
    点赞
  • 0
    评论
  • 0
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

©️2021 CSDN 皮肤主题: 数字20 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值