业主有个需求说是能把数据导出到excel来看,这里我用到了poi。

其中poi是一套用于访问微软格式文档的Java API。poi有很多组件组成,其中有用于操作excel格式文件的HSSF和用于操作Word的HWPF。

下面我们介绍poi中的HSSF的各组件和相关实例。

HSSF(用于操作excel的组件):

HSSFWorkbook    excel的文件对像。就是我们桌面右键新建excel操作一样。

HSSFSheet           excel的表单。一个excel工作簿有很多sheet表单的。

HSSFRow            excel的行。

HSSFCell          excel的格子单元

HSSFFont       excel字体

HSSFCellStyle   cell样式

其中我们都知道了一个excel文件就对应一个workbook,一个workbook中有多个sheet,一个sheet有很多行(row)和很多(cell)组成。那么下面继续我们的实例。

首先我们都知道建表顺序吧,其中poi也一样。

1、HSSFWorkBook打开或新建excel文件

2、HSSFWorkBook返回或创建sheet对象

3、sheet对象返回行对象,然后行对象得到列对象

4、对列进行读写操作

5、将生成的HSSFWorkBook放入Response中返回给前端。

首先导入poi包:这里用maven,所以直接在pom.xml中进行包配置

POI导出excel

然后代码实现:这里就只实现简单的excel就好。

service层:

public class POI2ExcelTest {

SimpleDateFormat df = new SimpleDateFormat("yyyy.MM.dd");// 设置日期格式

    @SuppressWarnings("deprecation")
    @Transactional
    public InputStream exportExcel() {

        // 要导出的数据,这里我造的假数据来的。
        List<Map<String, String>> datas = toMap(madeData());

        HSSFWorkbook wb = new HSSFWorkbook(); // 创建excel
        HSSFSheet sheet = wb.createSheet(df.format(new Date()) + "的数据"); // 创建子表名称

        // 表头样式
        HSSFCellStyle style = wb.createCellStyle();
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 水平居中
        style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 垂直居中
        HSSFFont font = wb.createFont();
        font.setFontName("宋体");// 设置字体名称
        font.setFontHeightInPoints((short) 24);// 设置字号
        font.setBold(true);// 加粗
        style.setFont(font);

        // 筛选条件样式
        HSSFCellStyle style_s = wb.createCellStyle();
        style_s.setAlignment(HSSFCellStyle.ALIGN_RIGHT);// 右对齐
        style_s.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 垂直居中
        HSSFFont font_s = wb.createFont();
        font_s.setFontName("宋体");// 设置字体名称
        font_s.setFontHeightInPoints((short) 12);// 设置字号
        font_s.setBold(true);// 加粗
        style_s.setFont(font_s);

        // 筛选条件值样式
        HSSFCellStyle style_V = wb.createCellStyle();
        style_V.setAlignment(HSSFCellStyle.ALIGN_LEFT);// 左对齐
        style_V.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 垂直居中
        HSSFFont font_V = wb.createFont();
        font_V.setFontName("宋体");// 设置字体名称
        font_V.setFontHeightInPoints((short) 12);// 设置字号
        font_V.setBold(true);// 加粗
        style_V.setFont(font_V);

        // 字段样式
        HSSFCellStyle style_C = wb.createCellStyle();
        style_C.setBorderTop(HSSFCellStyle.BORDER_THIN);// 上边框
        style_C.setBorderBottom(HSSFCellStyle.BORDER_THIN);// 下边框
        style_C.setBorderLeft(HSSFCellStyle.BORDER_THIN);// 左边框
        style_C.setBorderRight(HSSFCellStyle.BORDER_THIN);// 右边框
        style_C.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 水平居中
        style_C.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 垂直居中
        HSSFFont font_C = wb.createFont();
        font_C.setFontName("宋体");// 设置字体名称
        font_C.setFontHeightInPoints((short) 11);// 设置字号
        font_C.setBold(true);// 加粗
        style_C.setFont(font_C);

        // 数据样式
        HSSFCellStyle style_D = wb.createCellStyle();
        style_D.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 水平居中
        style_D.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 垂直居中
        style_D.setBorderTop(HSSFCellStyle.BORDER_THIN);// 上边框
        style_D.setBorderBottom(HSSFCellStyle.BORDER_THIN);// 下边框
        style_D.setBorderLeft(HSSFCellStyle.BORDER_THIN);// 左边框
        style_D.setBorderRight(HSSFCellStyle.BORDER_THIN);// 右边框
        HSSFFont font_D = wb.createFont();
        font_D.setFontName("宋体");// 设置字体名称
        font_D.setFontHeightInPoints((short) 11);// 设置字号
        style_D.setFont(font_D);

        // 创建表头标题
        int rowIndex = 0;
        HSSFRow headerRow = sheet.createRow(rowIndex);
        HSSFCell headercell = headerRow.createCell(0);
        // 合并单元格:起始行,终止行,起始列,终止列。
        sheet.addMergedRegion(new CellRangeAddress(0, 2, 0, 5));
        // 表标题
        headercell.setCellValue("学生信息");
        headercell.setCellStyle(style);
        rowIndex += 3;

        // 创建筛选数据

        HSSFRow searchRow = sheet.createRow(rowIndex);
        rowIndex += 2;
        HSSFCell timeCell = searchRow.createCell(0);
        timeCell.setCellValue("日期:");
        timeCell.setCellStyle(style_s);
        HSSFCell stimeCellV = searchRow.createCell(1);
        stimeCellV.setCellValue(df.format(new Date()));
        stimeCellV.setCellStyle(style_V);

        // 创建字段数据
        HSSFRow columRow = sheet.createRow(rowIndex);
        columRow.setRowStyle(style);
        String[] columns = { "学号", "姓名", "学院", "专业", "班级", "性别" };
        int colum_i = 0;
        for (String columName : columns) {
            HSSFCell columCell = columRow.createCell(colum_i);
            columCell.setCellValue(columName);
            columCell.setCellStyle(style_C);
            sheet.autoSizeColumn(colum_i);
            colum_i++;
        }
        rowIndex++;

        // 创建数据
        for (Map<String, String> data : datas) {
            HSSFRow dataRow = sheet.createRow(rowIndex);
            int data_i = 0;
            for (String colum : columns) {
                HSSFCell dataCell = dataRow.createCell(data_i);
                String val = data.get(colum);
                dataCell.setCellValue(val != null ? val : "");
                dataCell.setCellStyle(style_D);
                sheet.autoSizeColumn(data_i);
                data_i++;
            }
            rowIndex++;
        }
        ByteArrayOutputStream os = new ByteArrayOutputStream();

        try {
            FileOutputStream fis = new FileOutputStream("D:\\03.xls");
            wb.write(fis);
            fis.close();
            wb.write(os);
            os.close();
            wb.close();
            InputStream is = new ByteArrayInputStream(os.toByteArray());
            return is;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    private List<Map<String, String>> toMap(List<Student> students) {
        List<Map<String, String>> list = new ArrayList<>();
        for (Student student : students) {
            Map<String, String> map = new HashMap<>();
            map.put("学号", student.getNumber() != null ? student.getNumber() : "");
            map.put("姓名", student.getName() != null ? student.getName() : "");
            map.put("学院", student.getCollege() != null ? student.getCollege() : "");
            map.put("专业", student.getMajor() != null ? student.getMajor() : "");
            map.put("班级", student.getClasses() != null ? student.getClasses() : "");
            map.put("性别", student.getSex() != null ? student.getSex() : "");
            list.add(map);
        }
        return list;
    }

    private List<Student> madeData() {
        List<Student> list = new ArrayList<>();
        list.add(new Student("2014041743301", "saly", "计算机科学与工程学院", "计算机科学与技术", "计科143", "女"));
        list.add(new Student("2014041743301", "张三", "计算机科学与工程学院", "计算机科学与技术", "计科143", "男"));
        list.add(new Student("2014041743301", "李四", "计算机科学与工程学院", "计算机科学与技术", "计科143", "男"));
        list.add(new Student("2014041743301", "王五", "计算机科学与工程学院", "计算机科学与技术", "计科143", "男"));
        list.add(new Student("2014041743301", "赵六", "计算机科学与工程学院", "计算机科学与技术", "计科143", "男"));
        return list;

    }

}

然后:

POI导出excel

controller层:

POI导出excel

效果如图:

POI导出excel

相关文章:

  • 2021-09-26
  • 2021-08-04
  • 2021-08-29
  • 2021-12-31
  • 2021-12-15
  • 2021-05-26
  • 2021-10-18
  • 2022-02-15
猜你喜欢
  • 2021-11-23
  • 2021-05-01
  • 2022-03-04
  • 2021-06-06
  • 2021-11-07
相关资源
相似解决方案