本文不建议阅读。作者已有更好的解决方案:戳这里
前言
最近做公司项目,用到了POI实现Excel导入导出的功能。
整个功能做下来,发现很多大牛的文章都已经过时,会报类似The method setBorderBottom(short) from the type HSSFCellStyle is deprecated
的警告,直接使用@SupressWarnings
忽略警告显然是不负责任的。因此我直接去翻了Apache POI的官方文档,使用了目前官方推荐的新实现方法。
顺便发出来,方便没有翻阅英文官方文档习惯的大家。
过两天发实现导入Excel的示例文章。
项目结构简介
- Student.java:学生实体。
- StudentService.java:业务类。
- StudentController.java: web层。
(具体每一步的介绍在代码中都有详细的解释,此处不再赘述。如果疑惑的地方欢迎留言讨论。)
Excel导出代码展示
Student.java
@Entity
@NoArgsConstructor
@Getter
@Setter
public class Student implements Serializable{
/**
* @fields serialVersionUID
*/
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(generator = "idGenerator")
@GenericGenerator(name = "idGenerator", strategy = "uuid")
private String id;
private String sName;
}
StudentService.java(仅贴出实现导出功能的方法)
public void exportExcel(String[] ids, OutputStream out) {
// 根据传入的id将要导出的Student对象放入集合中
List list = new ArrayList<>();
for (int i = 0; i < ids.length; i++) {
Student student = repository.findOne(ids[i]);
list.add(student);
}
// 设置excel表头数据
String[] headers = { "学生ID", "学生姓名"};
// 创建excel工作薄
HSSFWorkbook workbook = new HSSFWorkbook();
// 创建sheet
HSSFSheet sheet = workbook.createSheet("数据导出");
// 设置默认列宽为15
sheet.setDefaultColumnWidth(15);
// 合并标题栏单元格
sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, headers.length - 1));
// 定义标题栏样式
HSSFCellStyle style = workbook.createCellStyle();
style.setFillForegroundColor(HSSFColorPredefined.SKY_BLUE.getIndex());
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
style.setBorderBottom(BorderStyle.THIN);
style.setBorderLeft(BorderStyle.THIN);
style.setBorderRight(BorderStyle.THIN);
style.setBorderTop(BorderStyle.THIN);
style.setAlignment(HorizontalAlignment.CENTER);
style.setVerticalAlignment(VerticalAlignment.CENTER);
HSSFFont font = workbook.createFont();
font.setColor(HSSFColorPredefined.VIOLET.getIndex());
font.setFontHeightInPoints((short) 18);
style.setFont(font);
// 定义表头样式
HSSFCellStyle style2 = workbook.createCellStyle();
style2.setFillForegroundColor(HSSFColorPredefined.GREEN.getIndex());
style2.setFillPattern(FillPatternType.SOLID_FOREGROUND);
style2.setBorderBottom(BorderStyle.THIN);
style2.setBorderLeft(BorderStyle.THIN);
style2.setBorderRight(BorderStyle.THIN);
style2.setBorderTop(BorderStyle.THIN);
style2.setAlignment(HorizontalAlignment.CENTER);
style2.setVerticalAlignment(VerticalAlignment.CENTER);
HSSFFont font2 = workbook.createFont();
font2.setFontHeightInPoints((short) 12);
style2.setFont(font2);
// 定义数据行样式
HSSFCellStyle style3 = workbook.createCellStyle();
style3.setFillForegroundColor(HSSFColorPredefined.LIGHT_YELLOW.getIndex());
style3.setFillPattern(FillPatternType.SOLID_FOREGROUND);
style3.setBorderBottom(BorderStyle.THIN);
style3.setBorderLeft(BorderStyle.THIN);
style3.setBorderRight(BorderStyle.THIN);
style3.setBorderTop(BorderStyle.THIN);
style3.setAlignment(HorizontalAlignment.CENTER);
style3.setVerticalAlignment(VerticalAlignment.CENTER);
// 创建行、单元格对象
HSSFRow row = null;
HSSFCell cell = null;
// 写入标题行
row = sheet.createRow(0);
row.setHeightInPoints(25);
cell = row.createCell(0);
cell.setCellStyle(style);
HSSFRichTextString textTitle = new HSSFRichTextString("数据导出");
cell.setCellValue(textTitle);
// 写入表头
row = sheet.createRow(1);
row.setHeightInPoints(17);
for (int i = 0; i < headers.length; i++) {
cell = row.createCell(i);
cell.setCellStyle(style2);
HSSFRichTextString text = new HSSFRichTextString(headers[i]);
cell.setCellValue(text);
}
// 写入数据行
Iterator it = list.iterator();
int index = 2;
while (it.hasNext()) {
row = sheet.createRow(index++);
Student student = (Student) it.next();
cell = row.createCell(0);
cell.setCellStyle(style3);
cell.setCellValue(student.getId());
cell = row.createCell(1);
cell.setCellStyle(style3);
cell.setCellValue(student.getSName());
}
// 获取输出流写文件。
try {
workbook.write(out);
out.flush();
workbook.close();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
StudentController.java(仅贴出实现导出功能的方法)
@RequestMapping(value = "/student/export" , method = RequestMethod.GET)
public void exporMemberFormExcel(@RequestParam(value = "ids", defaultValue = "", required = false) String[] ids, HttpServletResponse res) throws IOException {
logger.info("---------------导出列表到Excel--------------------");
res.setContentType("application/msexcel;charset=UTF-8");
res.addHeader("Content-Disposition", "attachment;filename=members.xls");
OutputStream out = res.getOutputStream();
studentService.exportExcel(ids, out);
}