Java报表导出,有导出模板与没有模板

一、添加依赖

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

二、后端主要代码

(1)、有Excel模板

// 获取Excel模板
File file = new File("E:\\down\\file\\" + "学生成绩表.xlsx");            
FileInputStream fileInputStream = new FileInputStream(file);           
XSSFWorkbook xssfWorkbook = new XSSFWorkbook(excelFileInputStream);
fileInputStream.close();
XSSFSheet worksheet = xssfWorkbook.getSheet("Sheet");
int rowNo = 1;
int columnNo;
XSSFRow workrow;
// 获取数据列表
List<Student> studentList = getStudentList();
// 列值
for (Student student: studentList) {
    columnNo = 0;
    workrow = worksheet.createRow(rowNo++);
    workrow.createCell(columnNo++).setCellValue(student.getNo());
    workrow.createCell(columnNo++).setCellValue(student.geName());
    workrow.createCell(columnNo++).setCellValue(student.getSex());
    workrow.createCell(columnNo++).setCellValue(student.getScore());
    workrow.createCell(columnNo++).setCellValue(student.getSubject());
}
String fileName = "学生成绩表";
final String userAgent = request.getHeader("user-agent");
if (userAgent != null && userAgent.indexOf("Firefox") >= 0) {
    fileName = new String(fileName.getBytes(), "ISO8859-1");
} else {
    fileName = URLEncoder.encode(fileName, "UTF8");
}
// 下载
response.reset();
response.setHeader("Content-disposition", "attachment;filename=" + fileName);
response.flushBuffer();
OutputStream output = response.getOutputStream();
xssfWorkbook.write(output);
output.flush();
output.close();

 (2)、没有模板

XSSFWorkbook xssfworkbook = new XSSFWorkbook();
XSSFSheet worksheet = xssfworkbook.createSheet("学生成绩表");
int rowNo = 0;
int columnNo = 0;
XSSFRow workrow = worksheet.createRow(rowIdx++);
// 列名
String[] colNames = {"编号", "名称", "性别", "分数", "科目"};
for (String columnName : columnNames) {
    workrow.createCell(columnNo).setCellValue(columnName);
    columnNo++;
}
// 查询的数据列表
List<Student> studentList = getStudentList();
// 列值
for (Student student: studentList) {
    columnNo = 0;
    workrow = worksheet.createRow(rowNo++);
    workrow.createCell(columnNo++).setCellValue(student.getNo());
    workrow.createCell(columnNo++).setCellValue(student.geName());
    workrow.createCell(columnNo++).setCellValue(student.getSex());
    workrow.createCell(columnNo++).setCellValue(student.getScore());
    workrow.createCell(columnNo++).setCellValue(student.getSubject());
}
String fileName = "学生成绩表.xlsx";
final String userAgent = request.getHeader("user-agent");
if (userAgent != null && userAgent.indexOf("Firefox") >= 0) {
    fileName = new String(fileName.getBytes(), "ISO8859-1");
} else {
    fileName = URLEncoder.encode(fileName, "UTF8");
}
// 下载
response.reset();
response.setHeader("Content-disposition", "attachment;filename=" + fileName);
response.flushBuffer();
OutputStream output = response.getOutputStream();
xssfworkbook.write(output);
output.flush();
output.close();

基础类

public class Student {

    /**
     * 编号
     */
    private String no;

    /**
     * 名称
     */
    private String name;

    /**
     * 性别
     */
    private String sex;

    /**
     * 分数
     */
    private String score;

    /**
     * 科目
     */
    private String subject;

    public Student() {
    }

    public Student(String no, String name, String sex, String score, String subject) {
        this.no = no;
        this.name = name;
        this.sex = sex;
        this.score = score;
        this.subject = subject;
    }

    public String getNo() {
        return no;
    }

    public void setNo(String no) {
        this.no = no;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public String getScore() {
        return score;
    }

    public void setScore(String score) {
        this.score = score;
    }

    public String getSubject() {
        return subject;
    }

    public void setSubject(String subject) {
        this.subject = subject;
    }
}

工具类

public List<Student> getStudentList() {
    List<Student> list = new ArrayList<Student>();
    list.add(new Student("001", "李明", "男", "98", "数学"));
    list.add(new Student("002", "李磊", "男", "90", "数学"));
    list.add(new Student("003", "张涛", "男", "58", "数学"));
    list.add(new Student("005", "陈芳", "女", "100", "数学"));
    list.add(new Student("004", "王帅", "男", "80", "数学"));
    list.add(new Student("006", "王艳", "女", "98", "数学"));
    return list;
}

 

posted @ 2022-03-17 16:03  北国浪子  阅读(131)  评论(0编辑  收藏  举报