• 创建一个工作簿
  • 创建一个sheet
  • 创建一个Row对象
  • 创建一个cell对象(1个row+1个cell构成一个单元格)
  • 设置单元格内容
  • 设置单元格样式. 字体 字体大小 是否加粗
  • 保存
  • 关闭流对象

2010以上格式使用XSSFWorkBook对象, 2003格式使用HSSFWorkBook对象, 其他对象操作基本一样.

  1. public void test1() {
  2. HSSFWorkbook workbook = new HSSFWorkbook();
  3. CellStyle cellStyle = workbook.createCellStyle();
  4. cellStyle.setBorderBottom(BorderStyle.THIN);
  5. cellStyle.setBorderLeft(BorderStyle.THIN);
  6. cellStyle.setBorderRight(BorderStyle.THIN);
  7. cellStyle.setBorderTop(BorderStyle.THIN);
  8. Font font = workbook.createFont();
  9. font.setFontName("宋体");
  10. font.setFontHeightInPoints((short) 12);
  11. cellStyle.setFont(font);
  12. HSSFSheet sheet = workbook.createSheet("Sheet1");
  13. //设置单元格宽度
  14. sheet.setColumnWidth(0, 30 * 256);
  15. sheet.setColumnWidth(1, 30 * 256);
  16. sheet.setColumnWidth(2, 30 * 256);
  17. Row row0 = sheet.createRow(0);
  18. Cell cell0 = row0.createCell(0);
  19. cell0.setCellValue("序号");
  20. cell0.setCellStyle(cellStyle);
  21. Cell cell1 = row0.createCell(1);
  22. cell1.setCellValue("姓名");
  23. Cell cell2 = row0.createCell(2);
  24. cell2.setCellValue("成绩");
  25. OutputStream os = null;
  26. try {
  27. os = new FileOutputStream("d:\\测试生成2003.xls");
  28. workbook.write(os);
  29. os.close();
  30. } catch (Exception e) {
  31. e.printStackTrace();
  32. }
  33. }
  1. @Test
  2. public void test2() {
  3. XSSFWorkbook workbook = new XSSFWorkbook();
  4. CellStyle cellStyle = workbook.createCellStyle();
  5. cellStyle.setBorderBottom(BorderStyle.THIN);
  6. cellStyle.setBorderLeft(BorderStyle.THIN);
  7. cellStyle.setBorderRight(BorderStyle.THIN);
  8. cellStyle.setBorderTop(BorderStyle.THIN);
  9. Font font = workbook.createFont();
  10. font.setFontName("宋体");
  11. font.setFontHeightInPoints((short) 12);
  12. cellStyle.setFont(font);
  13. XSSFSheet sheet = workbook.createSheet("Sheet1");
  14. Row row0 = sheet.createRow(0);
  15. Cell cell0 = row0.createCell(0);
  16. cell0.setCellValue("序号");
  17. cell0.setCellStyle(cellStyle);
  18. Cell cell1 = row0.createCell(1);
  19. cell1.setCellValue("姓名");
  20. Cell cell2 = row0.createCell(2);
  21. cell2.setCellValue("成绩");
  22. OutputStream os = null;
  23. try {
  24. os = new FileOutputStream("d:\\测试生成2010.xlsx");
  25. workbook.write(os);
  26. os.close();
  27. } catch (Exception e) {
  28. e.printStackTrace();
  29. }
  30. }

合并单元格在生成excel中算常见的一个场景, 通常先合并单元, 单元格内容居中,并设置单元格边框.
poi合并单元格使用CellRangeAddress类, 构造函数包括4个参数firstRow, lastRow, firstCol, lastCol根据自己需要传入行和列.

  1. public CellRangeAddress(int firstRow, int lastRow, int firstCol, int lastCol) {
  2. }

合并单元格后设置边框poi已提供了RegionUtil静态类, 可直接使用.

  1. CellRangeAddress region = new CellRangeAddress(0, 0, 0, 2);
  2. sheet.addMergedRegion(region);
  3. RegionUtil.setBorderBottom(BorderStyle.THIN, region, sheet);
  4. RegionUtil.setBorderLeft(BorderStyle.THIN, region, sheet);
  5. RegionUtil.setBorderTop(BorderStyle.THIN, region, sheet);
  6. RegionUtil.setBorderRight(BorderStyle.THIN, region, sheet);

左右居中 上下居中 自动换行

  1. cellStyle.setAlignment(HorizontalAlignment.CENTER);
  2. cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
  3. cellStyle.setWrapText(true);
  1. @Controller
  2. @GetMapping("/excel2003")
  3. public void excel2003(HttpServletResponse httpServletResponse){
  4. try {
  5. //2010格式设置
  6. //response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
  7. //2003格式设置
  8. response.setContentType("application/vnd.ms-excel");
  9. httpServletResponse.addHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode("学生成绩单.xls", "utf-8"));
  10. ServletOutputStream outputStream = httpServletResponse.getOutputStream();
  11. HSSFWorkbook workbook = new HSSFWorkbook();
  12. CellStyle cellStyle = workbook.createCellStyle();
  13. cellStyle.setBorderBottom(BorderStyle.THIN);
  14. cellStyle.setBorderLeft(BorderStyle.THIN);
  15. cellStyle.setBorderRight(BorderStyle.THIN);
  16. cellStyle.setBorderTop(BorderStyle.THIN);
  17. Font font = workbook.createFont();
  18. font.setFontName("宋体");
  19. font.setFontHeightInPoints((short) 12);
  20. cellStyle.setFont(font);
  21. HSSFSheet sheet = workbook.createSheet("Sheet1");
  22. Row row0 = sheet.createRow(0);
  23. Cell cell0 = row0.createCell(0);
  24. cell0.setCellValue("序号");
  25. cell0.setCellStyle(cellStyle);
  26. Cell cell1 = row0.createCell(1);
  27. cell1.setCellValue("姓名");
  28. Cell cell2 = row0.createCell(2);
  29. cell2.setCellValue("成绩");
  30. workbook.write(outputStream);
  31. } catch (Exception e) {
  32. e.printStackTrace();
  33. }
  34. }

如果你在web项目中导出excel后,打开excel文件时提示文件已损坏,但是文件还可以打开, 则需要在HttpServletResponse上设置响应头, 2003和2010设置方式不同
2003
response.setContentType(“application/vnd.ms-excel”);
2010
response.setContentType(“application/vnd.openxmlformats-officedocument.spreadsheetml.sheet”);

下载文件名如果包含中文的话需要编码
httpServletResponse.addHeader(“Content-Disposition”, “attachment;fileName=” + URLEncoder.encode(“学生成绩单.xls”, “utf-8”));
这种设置在浏览器里下载文件中文是没问题的, 只是如果你使用Swagger或者Postman测试的话,文件名还是经过编码的, 这个没问题说明文件下载已经没问题.