javaSpringMVC导出Excel

还是和以前一样先说明一下我的开发工具:IDEA+mySQL
Excel导出在项目上有使用,当时还搜集了一些关于Excel导出。主流的Excel导出有:(都是开源的)
(1)JDBC-ODBC Excel Driver
(2)jcom
(3) jxl
(4)POI
这里就不一一介绍了,有兴趣度一下就知道了。我使用的是apache的POI,
首先得先引入一个jar文件:

<dependency>
      <groupId>org.apache.poi</groupId>
      <artifactId>poi-ooxml</artifactId>
      <version>3.9</version>
    </dependency>

下来开搞:
list:是查询结果集。

/**
     * 创建Excel
     *
     * @param list 数据
     */
     static String createExcel(List<StudentEntity> list) {
         //Excel文件储存位置和名称。
         String fileName="D:/学生信息表.xls";
         //新建一个工作空间
         HSSFWorkbook workbook = new HSSFWorkbook();
         // 生成一个表格
         HSSFSheet sheet = workbook.createSheet("学生信息表");
         // 设置表格默认列宽度为20个字节
         sheet.setDefaultColumnWidth(20);
         // 生成一个样式
         HSSFCellStyle style = workbook.createCellStyle();
         // 设置这些样式
         style.setFillForegroundColor(HSSFColor.SKY_BLUE.index);
         style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
         style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
         style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
         style.setBorderRight(HSSFCellStyle.BORDER_THIN);
         style.setBorderTop(HSSFCellStyle.BORDER_THIN);
         style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
         // 生成一个字体
         HSSFFont font = workbook.createFont();
         font.setColor(HSSFColor.VIOLET.index);
         font.setFontHeightInPoints((short) 15);
         font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
         // 把字体应用到当前的样式
         style.setFont(font);
         // 生成并设置另一个样式
         HSSFCellStyle style2 = workbook.createCellStyle();
         style2.setFillForegroundColor(HSSFColor.LIGHT_YELLOW.index);
         style2.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
         style2.setBorderBottom(HSSFCellStyle.BORDER_THIN);
         style2.setBorderLeft(HSSFCellStyle.BORDER_THIN);
         style2.setBorderRight(HSSFCellStyle.BORDER_THIN);
         style2.setBorderTop(HSSFCellStyle.BORDER_THIN);
         style2.setAlignment(HSSFCellStyle.ALIGN_CENTER);
         style2.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
         // 生成另一个字体
         HSSFFont font2 = workbook.createFont();
         font2.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);
         // 把字体应用到当前的样式
         style2.setFont(font2);
        // 添加表头行
        HSSFRow hssfRow = sheet.createRow(0);
        // 设置单元格格式居中
        HSSFCellStyle cellStyle = workbook.createCellStyle();
        cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);

        // 添加表头内容
        HSSFCell headCell = hssfRow.createCell(0);
        headCell.setCellValue("id");
        headCell.setCellStyle(cellStyle);

         headCell = hssfRow.createCell(1);
        headCell.setCellValue("学生姓名");
        headCell.setCellStyle(cellStyle);

        headCell = hssfRow.createCell(2);
        headCell.setCellValue("班级");
        headCell.setCellStyle(cellStyle);

        headCell = hssfRow.createCell(3);
        headCell.setCellValue("班主任");
        headCell.setCellStyle(cellStyle);
        // 添加数据内容,循环取出list中的数据添加到指定的
        for (int i = 0; i < list.size(); i++) {
            hssfRow = sheet.createRow((int) i + 1);
            StudentEntity student= list.get(i);
            //System.out.println("创建Excel:"+student);

            //创建单元格,并设置获取到的学生id
            HSSFCell cell = hssfRow.createCell(0);
            cell.setCellValue(student.getId());
            cell.setCellStyle(cellStyle);
            //创建单元格,并设置获取到的学生姓名
            cell = hssfRow.createCell(1);
            cell.setCellValue(student.getStudentName());
            cell.setCellStyle(cellStyle);
            //创建单元格,并设置获取到的学生班级
            cell = hssfRow.createCell(2);
            cell.setCellValue(student.getStudentClass());
            cell.setCellStyle(cellStyle);
            //创建单元格,并设置获取到的学生班主任
            cell = hssfRow.createCell(3);
            cell.setCellValue(student.getHeadTeacher());
            cell.setCellStyle(cellStyle);
        }
        // 保存Excel文件
        try {
            OutputStream outputStream = new FileOutputStream(fileName);
            workbook.write(outputStream);
            outputStream.close();
            return fileName;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

   /* *
     * 读取Excel
     *
     * @return 数据集合*/
    private static List<StudentEntity> readExcel() {
        List<StudentEntity> list = new ArrayList<StudentEntity>();
        HSSFWorkbook workbook = null;

        try {
            // 读取Excel文件
            InputStream inputStream = new FileInputStream("D:/学生信息表.xls");
            workbook = new HSSFWorkbook(inputStream);
            inputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

        // 循环工作表
        for (int numSheet = 0; numSheet < workbook.getNumberOfSheets(); numSheet++) {
            HSSFSheet hssfSheet = workbook.getSheetAt(numSheet);
            if (hssfSheet == null) {
                continue;
            }
            // 循环行
            for (int rowNum = 1; rowNum <= hssfSheet.getLastRowNum(); rowNum++) {
                HSSFRow hssfRow = hssfSheet.getRow(rowNum);
                if (hssfRow == null) {
                    continue;
                }

                // 将单元格中的内容存入集合
               StudentEntity student= new StudentEntity();
                //id
                HSSFCell cell = hssfRow.getCell(0);
                if (cell == null) {
                    continue;
                }
                student.setId(cell.getStringCellValue());
                //学生姓名
                cell = hssfRow.getCell(1);
                if (cell == null) {
                    continue;
                }                                                                            student.setStudentName(cell.getStringCellValue());
                //学生班级
                cell = hssfRow.getCell(2);
                if (cell == null) {
                    continue;
                }
                student.setStudentClass(cell.getStringCellValue());
                //班主任
                cell = hssfRow.getCell(3);
                if (cell == null) {
                    continue;
                }
                student.setHeadTeacher(cell.getStringCellValue());
                 //添加到list中
                list.add(student);
            }
        }
        return list;
    }
}

这是StudentEntity实体类

public class StudentEntity {

    //id
    private Integer id;
    //学生姓名
    private String studentName;
    //学生班级
    private String studentClass;
    //班主任
    private String headTeacher;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getStudentName() {
        return studentName;
    }

    public void setStudentName(String studentName) {
        this.studentName = studentName;
    }

    public String getStudentClass() {
        return studentClass;
    }

    public void setStudentClass(String studentClass) {
        this.studentClass = studentClass;
    }

    public String getHeadTeacher() {
        return headTeacher;
    }

    public void setHeadTeacher(String headTeacher) {
        this.headTeacher = headTeacher;
    }
}

这就完事了,练习一下,有问题留言。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值