页面数据
后端代码
@RequestMapping(value = "/exportExcel",method = RequestMethod.GET)
public void exportExcel(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {
//一、从后台拿数据
List<Stuinfo> list = stuinfoService.findStuinfo();
//二、 数据转成excel
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
response.setContentType("application/x-download");
String fileName = "学生信息.xlsx";
fileName = URLEncoder.encode(fileName, "UTF-8");
response.addHeader("Content-Disposition", "attachment;filename=" + fileName);
// 第一步:定义一个新的工作簿
XSSFWorkbook wb = new XSSFWorkbook();
// 第二步:创建一个Sheet页
XSSFSheet sheet = wb.createSheet("学生信息");
sheet.setDefaultRowHeight((short) (2 * 256));//设置行高
sheet.setColumnWidth(0,3000);//设置列宽
sheet.setColumnWidth(1,3000);
sheet.setColumnWidth(2,3000);
sheet.setColumnWidth(3,8000);
sheet.setColumnWidth(4,7000);
sheet.setColumnWidth(5,7000);
sheet.setColumnWidth(6,7000);
//表头样式
XSSFFont headFont = wb.createFont();
headFont.setFontName("微软雅黑");
headFont.setBold(true);
headFont.setFontHeightInPoints((short) 11);
XSSFCellStyle headStyle = wb.createCellStyle();
headStyle.setFont(headFont);// 设置字体css
headStyle.setVerticalAlignment(VerticalAlignment.CENTER); // 竖向居中
headStyle.setAlignment(HorizontalAlignment.CENTER); // 横向居中
headStyle.setFillForegroundColor(IndexedColors.PALE_BLUE.getIndex());
headStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
//数据样式
XSSFFont font = wb.createFont();
font.setFontName("微软雅黑");
font.setFontHeightInPoints((short) 11);
XSSFCellStyle style = wb.createCellStyle();
style.setFont(font); // 设置字体css
style.setVerticalAlignment(VerticalAlignment.CENTER);// 竖向居中
style.setAlignment(HorizontalAlignment.CENTER); // 横向居中
XSSFRow row = sheet.createRow(0);
XSSFCell cell = row.createCell(0);
String[] heads={"姓名 ","性别","年龄","身份证","手机号 ","邮箱 ","地址"};
for(int i=0;i<heads.length;i++){
cell = row.createCell(i);
cell.setCellValue(heads[i]);
cell.setCellStyle(headStyle);
}
XSSFRow rows;
XSSFCell cells;
for (int i = 0; i < list.size(); i++) {
// 第三步:在这个sheet页里创建一行
rows = sheet.createRow(i+1);
// 第四步:在该行创建一个单元格
cells = rows.createCell(0);
// 第五步:在该单元格里设置值
cells.setCellValue(list.get(i).getName());
cells.setCellStyle(style);
cells = rows.createCell(1);
cells.setCellValue(list.get(i).getSex());
cells.setCellStyle(style);
cells = rows.createCell(2);
cells.setCellValue(list.get(i).getAge());
cells.setCellStyle(style);
cells = rows.createCell(3);
cells.setCellValue(list.get(i).getCard());
cells.setCellStyle(style);
cells = rows.createCell(4);
cells.setCellValue(list.get(i).getTel());
cells.setCellStyle(style);
cells = rows.createCell(5);
cells.setCellValue(list.get(i).getEmail());
cells.setCellStyle(style);
cells = rows.createCell(6);
cells.setCellValue(list.get(i).getAddress());
cells.setCellStyle(style);
}
try {
OutputStream out = response.getOutputStream();
wb.write(out);
out.close();
wb.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
POM依赖
<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>
Excel表格