导出 Excel
Maven 依赖
1 2 3 4 5 6
   |  <dependency>     <groupId>org.apache.poi</groupId>     <artifactId>poi-ooxml</artifactId>     <version>5.0.0</version> </dependency>
 
  | 
 
编码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
   | import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.xssf.streaming.SXSSFWorkbook;
  import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.OutputStream; import java.io.UnsupportedEncodingException; import java.util.List;
  public class ExcelUtil {
      
 
 
 
 
 
 
      public static void exportExcel(HttpServletResponse response, String fileName, List<String> columnList, List<List<String>> dataList) {                  OutputStream os = null;                  setResponseHeader(response, fileName);         try {                          os = response.getOutputStream();                          SXSSFWorkbook wb = new SXSSFWorkbook(1000);                          Sheet sheet1 = wb.createSheet("sheet1");             int excelRow = 0;                          Row titleRow = sheet1.createRow(excelRow++);             for (int i = 0; i < columnList.size(); i++) {                                  Cell cell = titleRow.createCell(i);                 cell.setCellValue(columnList.get(i));             }                          if (dataList != null && dataList.size() > 0) {                                  int count = 1;                                  for (List<String> strings : dataList) {                     Row dataRow = sheet1.createRow(excelRow++);                                          for (int j = -1; j < dataList.get(0).size(); j++) {                         Cell cell = dataRow.createCell(j + 1);                         if (j == -1) {                             cell.setCellValue(count++);                         } else {                             cell.setCellValue(strings.get(j));                         }                     }                 }             }                          wb.write(os);         } catch (IOException e) {             e.printStackTrace();         } finally {             try {                                  if (os != null) {                     os.close();                 }             } catch (IOException e) {                 e.printStackTrace();             }         }     }
      
 
      private static void setResponseHeader(HttpServletResponse response, String fileName) {         try {             try {                 fileName = new String(fileName.getBytes(), "ISO8859-1");             } catch (UnsupportedEncodingException e) {                 e.printStackTrace();             }             response.setContentType("application/octet-stream;charset=UTF-8");             response.setHeader("Content-Disposition", "attachment;filename=" + fileName);             response.addHeader("Cache-Control", "no-cache");         } catch (Exception ex) {             ex.printStackTrace();         }     }
  }
 
  | 
 
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
   | import com.rainbowfish.health.physical.util.ExcelUtil; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import org.springframework.http.HttpEntity; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;
  import javax.servlet.http.HttpServletResponse; import java.util.ArrayList; import java.util.List;
  @Api(tags = "Excel 下载接口") @RestController @RequestMapping("/api/v1/excel") public class ExcelController {
      @GetMapping("/test")     @ApiOperation(value = "测试下载 Excel ")     public HttpEntity<ResultResponse> download(HttpServletResponse response) {         List<String> record = new ArrayList<>();         List<List<String>> recordList = new ArrayList<>();         record.add("A");         record.add("B");         record.add("C");         recordList.add(record);         List<String> columnNameList = new ArrayList<>();         columnNameList.add("编号");         columnNameList.add("姓名");         columnNameList.add("性别");         columnNameList.add("住址");         ExcelUtil.exportExcel(response, "download.xlsx", columnNameList, recordList);         return new HttpEntity<>(null);     }
  }
 
  | 
 
参考资料
官方文档
手把手教你springboot中导出数据到excel中