把数据写入xlsx中并在浏览器中下载下来
只介绍了其中四种,之后有时间再补上其他方法的
文章目录
提示:以下是本篇文章正文内容,下面案例可供参考
引言
##People 实体类
public class Person {
private Integer id;
private String name;
private Integer age;
private String gender;
}
PersonFactroy方法
@Service
public class PersonFactroy {
public List<Person> getPersonList() {
List<Person> list = new ArrayList<>();
list.add(new Person(1, "Howie", 20, "female"));
list.add(new Person(2, "Wade", 25, "male"));
list.add(new Person(3, "Duncan", 30, "male"));
list.add(new Person(4, "Kobe", 35, "male"));
list.add(new Person(5, "James", 40, "male"));
return list;
}
}
一、用SSXSSFWorkbook方法将数据导入表格
1.引入依赖
代码如下(示例):
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.17</version>
</dependency>
2.正文(贴注释)
代码如下(示例):
/**
* 将数据导入表格
* 把数据写入工作表中,然后就直接写入输出流了(本地就相当于只有在浏览器中下载的那个xlsx工作表)
*/
@RequestMapping(value = "/getExcel", method = RequestMethod.GET)
public void createBoxListExcel(HttpServletResponse response) throws Exception {
//获取定死的一些数据
List<Person> list = personFactroy.getPersonList();
//创建一个工作薄
SXSSFWorkbook workbook = new SXSSFWorkbook();
//该配置是配置xlsx表格的样式
CellStyle cellStyle0 = workbook.createCellStyle();
cellStyle0.setAlignment(HorizontalAlignment.CENTER);
cellStyle0.setVerticalAlignment(VerticalAlignment.CENTER);
cellStyle0.setBorderBottom(BorderStyle.THIN);
cellStyle0.setBorderTop(BorderStyle.THIN);
cellStyle0.setBorderLeft(BorderStyle.THIN);
cellStyle0.setBorderRight(BorderStyle.THIN);