poi是阿帕奇组件开源的poi项目,以基于Office Open XML标准(OOXML)和Microsoft的OLE 2复合文档格式(OLE2)来处理各种文件格式。Apache POI api的主要用途是用于文本提取应用程序,例如网络蜘蛛,索引构建器和内容管理系统。下面我们就来从mysql中导出一个Excel。
<!-- poi -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.1.0</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.0</version>
</dependency>
<!--mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.21</version>
</dependency>
因为Excle有03版和07版,也就是xls和xlsx这两个版本所以我们分别导入各自的poi依赖。
pojo
package com.example.demo.pojo;
public class Student {
private int id;
private String name;
private int age;
private String phone;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
}
dao
package com.example.demo.dao;
import com.example.demo.pojo.Student;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@Mapper
public interface StudentMapper {
List<Student> findAll();
}
*.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.demo.dao.StudentMapper">
<select id="findAll" resultType="com.example.demo.pojo.Student">
SELECT * FROM student
</select>
</mapper>
service
package com.example.demo.service;
import com.example.demo.pojo.Student;
import java.util.List;
public interface StudentService {
List<Student> findAll();
}
Impl
package com.example.demo.service.Impl;
import com.example.demo.dao.StudentMapper;
import com.example.demo.pojo.Student;
import com.example.demo.service.StudentService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
@Service
public class StudentServiceImpl implements StudentService{
@Resource
private StudentMapper studentMapper;
@Override
public List<Student> findAll() {
return studentMapper.findAll();
}
}
util
package com.example.demo.util;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
public class ExcelUtil {
public static void createFile(HttpServletResponse response, HSSFWorkbook workbook) {
//设置文件名
String fileName = "学生信息";
try {
//捕获内存缓冲区的数据,装换成字符数组
ByteArrayOutputStream out = new ByteArrayOutputStream();
workbook.write(out);
// 获取内存缓冲中的数据
byte[] content = out.toByteArray();
// 将字节数组转化为输入流
InputStream in = new ByteArrayInputStream(content);
//通过调用reset()方法可以重新定位。
response.reset();
// 如果文件名是英文名不需要加编码格式,如果是中文名需要添加"iso-8859-1"防止乱码
response.setHeader("Content-Disposition", "attachment; filename=" + new String((fileName + ".xls").getBytes(), "iso-8859-1"));
response.addHeader("Content-Length", "" + content.length);
response.setContentType("application/vnd.ms-excel;charset=UTF-8");
ServletOutputStream outputStream = response.getOutputStream();
BufferedInputStream bis = new BufferedInputStream(in);
BufferedOutputStream bos = new BufferedOutputStream(outputStream);
byte[] buff = new byte[8192];
int bytesRead;
while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
bos.write(buff, 0, bytesRead);
}
bis.close();
bos.close();
outputStream.flush();
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
controller
package com.example.demo.controller;
import com.example.demo.pojo.Student;
import com.example.demo.service.StudentService;
import com.example.demo.util.ExcelUtil;
import org.apache.poi.hssf.usermodel.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
@Controller
public class StudentController {
@Resource
private StudentService studentService;
@RequestMapping("/exportExcle")
public void exportExcle(HttpServletRequest request, HttpServletResponse response){
List<Student> list=studentService.findAll();
//创建工作薄
HSSFWorkbook workbook=new HSSFWorkbook();
//创建表
HSSFSheet sheet=workbook.createSheet("学生信息");
//创建行
HSSFRow row=sheet.createRow(0);
//创建单元格式
HSSFCellStyle cellStyle=workbook.createCellStyle();
//表头
String[] head={
"序号","姓名","年龄","手机号码"};
HSSFCell cell;
//设置表头
for(int iHead=0;iHead<head.length;iHead++){
cell=row.createCell(iHead);
cell.setCellValue(head[iHead]);
cell.setCellStyle(cellStyle);
}
//设置表格内容
for (int iBody=0;iBody<list.size();iBody++){
row=sheet.createRow(iBody+1);
Student student=list.get(iBody);
String[] studentArray =new String[4];
studentArray[0]= String.valueOf(student.getId());
studentArray[1]=student.getName()+ "";
studentArray[2]=student.getAge() + "";
studentArray[3]=student.getPhone()+ "";
for(int iArray=0; iArray<studentArray.length; iArray++) {
row.createCell(iArray).setCellValue(studentArray[iArray]);
}
}
// 生成Excel文件
ExcelUtil.createFile(response, workbook);
}
}
导出Excel效果图
启动项目之后,在浏览器输入http://localhost:8080/exportExcel就可以进入,帮你导出Excel