springboot使用poi进行报表导出小demo
2019-08-11 本文已影响0人
royalx
1.添加依赖
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.14</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.9</version>
</dependency>
2.案例描述:从数据库student1表中查询出来的数据进行报表导出(前台页面使用a标签进行链接就可以,没必要ajax)
3.报表导出工具类:
package com.ansheng.util;
import org.apache.poi.hssf.usermodel.*;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
public class ExcelUtils {
static final short borderpx = 1;
/**
* 导出excel表格
* @param head
* @param body
* @return
*/
public static HSSFWorkbook expExcel(List<String> head, List<List<String>> body) {
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet("Sheet1");
HSSFRow row = sheet.createRow(0);
HSSFCell cell= null;
HSSFCellStyle cellStyle = workbook.createCellStyle();
setBorderStyle(cellStyle, borderpx);
cellStyle.setFont(setFontStyle(workbook, "黑体", (short) 14));
cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
sheet.createFreezePane(0,1,0,1);
for (int i = 0; i<head.size(); i++) {
cell = row.createCell(i);
cell.setCellValue(head.get(i));
cell.setCellStyle(cellStyle);
}
HSSFCellStyle cellStyle2 = workbook.createCellStyle();
setBorderStyle(cellStyle2, borderpx);
cellStyle2.setFont(setFontStyle(workbook, "宋体", (short) 12));
cellStyle2.setAlignment(HSSFCellStyle.ALIGN_CENTER);
for (int i = 0; i < body.size(); i++) {
row = sheet.createRow(i + 1);
List<String> paramList = body.get(i);
for (int p = 0; p < paramList.size(); p++) {
cell = row.createCell(p);
cell.setCellValue(paramList.get(p));
cell.setCellStyle(cellStyle2);
}
}
for (int i = 0, isize = head.size(); i < isize; i++) {
sheet.autoSizeColumn(i);
}
return workbook;
}
/**
* 文件输出
* @author LiuYang
* @param workbook 填充好的workbook
* @param path 存放的位置
*/
public static void outFile(HSSFWorkbook workbook,String path,HttpServletResponse response) {
SimpleDateFormat fdate=new SimpleDateFormat("yyyyMMdd-HH点mm分");
path = path.substring(0, path.lastIndexOf(".")) + fdate.format(new Date()) + path.substring(path.lastIndexOf("."));
OutputStream os=null;
File file = null;
try {
file = new File(path);
String filename = file.getName();
os = new FileOutputStream(file);
response.addHeader("Content-Disposition", "attachment;filename="+ URLEncoder.encode(filename, "UTF-8"));
os= new BufferedOutputStream(response.getOutputStream());
response.setContentType("application/vnd.ms-excel;charset=utf-8");
workbook.write(os);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
os.flush();
os.close();
System.gc();
System.out.println(file.delete());
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 设置字体样式
* @author LiuYang
* @param workbook 工作簿
* @param name 字体类型
* @param height 字体大小
* @return HSSFFont
*/
private static HSSFFont setFontStyle(HSSFWorkbook workbook, String name, short height) {
HSSFFont font = workbook.createFont();
font.setFontHeightInPoints(height);
font.setFontName(name);
return font;
}
/**
* 设置单元格样式
* @author LiuYang
* @param cellStyle 工作簿
* @param border border样式
*/
private static void setBorderStyle(HSSFCellStyle cellStyle, short border) {
cellStyle.setBorderBottom(border); // 下边框
cellStyle.setBorderLeft(border);// 左边框
cellStyle.setBorderTop(border);// 上边框
cellStyle.setBorderRight(border);// 右边框
}
}
4.controller代码:
package com.ansheng.controller;
import com.ansheng.entity.Student1;
import com.ansheng.service.Excel2Service;
import com.ansheng.util.ExcelUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
@Controller
public class Excel2Controller {
@Autowired
Excel2Service excel2Service;
@RequestMapping("/excel2")
public void testExcel2(HttpServletResponse response){
//查询出需要导出的数据
List<Student1> lisx=excel2Service.queryFromTables();
//创建报表数据头
List<String> head = new ArrayList<>();
head.add("学号");
head.add("姓名");
head.add("性别");
head.add("年龄");
head.add("学校");
head.add("专业");
head.add("个人描述");
//创建报表体
List<List<String>> body = new ArrayList<>();
for (Student1 stu : lisx) {
List<String> bodyValue = new ArrayList<>();
bodyValue.add(String.valueOf(stu.getId()));
bodyValue.add(stu.getName());
if("0".equals(stu.getSex())){
bodyValue.add("女");
}else{
bodyValue.add("男");
}
bodyValue.add(String.valueOf(stu.getAge()));
bodyValue.add(stu.getSchool());
bodyValue.add(stu.getCollege());
bodyValue.add(stu.getDescr());
//将数据添加到报表体中
body.add(bodyValue);
}
String fileName = "学生信息统计.xls";
HSSFWorkbook excel = ExcelUtils.expExcel(head,body);
ExcelUtils.outFile(excel,"./"+fileName,response);
}
}
注意:只有一个查询方法,所有service和mapper就不写代码了
5.数据库中数据截图如下:
image.png