一、导出Excel
添加maven依赖
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.10-FINAL</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>3.10-FINAL</version>
</dependency>
exportExcel()这个方法有两个参数,第一个是导出文件的保存全路径,比如/Users/admin/Desktop/result.xlsx,第二个是从sql查询出来的结果集
public void exportExcel(String excelFileName,List<DataBean> result) {
try {
String sheetName = "统计报表";// 工作表名
XSSFWorkbook wb = new XSSFWorkbook();
XSSFSheet sheet = wb.createSheet(sheetName);
// 生成一个样式
XSSFCellStyle style = wb.createCellStyle();
//样式字体居中
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
// style.setWrapText(true);//自动换行
XSSFRow first = sheet.createRow(0);
XSSFCell headCell = first.createCell((short) 0);
headCell.setCellValue("序号");
headCell.setCellStyle(style);
headCell = first.createCell( (short) 1);
headCell.setCellValue("姓名");
headCell.setCellStyle(style);
headCell = first.createCell((short) 2);
headCell.setCellValue("年龄");
headCell.setCellStyle(style);
headCell = first.createCell((short) 3);
headCell.setCellValue("性别");
headCell.setCellStyle(style);
headCell = first.createCell(4);
headCell.setCellValue("参加次数");
headCell.setCellStyle(style);
headCell = first.createCell((short) 5);
headCell.setCellValue("pb");
headCell.setCellStyle(style);
// 循环写入每条收入记录的主码和盈利额
for (int r = 1; r < result.size()+1; r++) {
XSSFRow row = sheet.createRow(r);
XSSFCell contentCell = row.c