SpringBoot:SpringBoot 에서 Excel 내 보 내기 인터페이스 튜 토리 얼 을 만 듭 니 다.

웹 프로젝트 에서 Excel 과 같은 기능 을 내 보 내야 합 니 다.백 엔 드 인 터 페 이 스 는 어떻게 이 루어 집 니까?Controller 코드 는 아래 에 있 습 니 다.프로젝트 의 Controller 에 복사 하면 사용 할 수 있 습 니 다.
우선 Excel 의존 도 를 추가 합 니 다.이 예 에서 저 희 는 apache 의 poi 를 사용 합 니 다.

<dependency>
 <groupId>org.apache.poi</groupId>
 <artifactId>poi</artifactId>
 <version>3.17</version>
</dependency>
배경 에서 Excel 컨트롤 러 인터페이스 코드 내 보 내기:

import org.apache.poi.hssf.usermodel.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Controller
public class ExcelController {

 /**
 * Excel      
 * http://localhost:8080/ExcelDownload
 * @param response response  
 * @throws IOException  IO  
 */
 @RequestMapping("/ExcelDownload")
 public void excelDownload(HttpServletResponse response) throws IOException {
 //    
 String[] header = {"ID", "  ", "  ", "  ", "  ", "  "};

 //    
 String[] student1 = {"1", "  ", " ", "23", "     ", "96"};
 String[] student2 = {"2", "  ", " ", "26", "     ", "91"};
 String[] student3 = {"3", "  ", " ", "28", "     ", "90"};

 //       
 HSSFWorkbook workbook = new HSSFWorkbook();

 //      ,       "   "
 HSSFSheet sheet = workbook.createSheet("   ");

 //        10   
 sheet.setDefaultColumnWidth(10);

 //       
 HSSFRow headrow = sheet.createRow(0);

 //      (        ,         )
 for (int i = 0; i < header.length; i++) {
  //       
  HSSFCell cell = headrow.createCell(i);

  //        
  HSSFRichTextString text = new HSSFRichTextString(header[i]);

  //                 
  cell.setCellValue(text);
 }

 //       ,       
 //         
 HSSFRow row1 = sheet.createRow(1);
 for (int i = 0; i < student1.length; i++) {
  HSSFCell cell = row1.createCell(i);
  HSSFRichTextString text = new HSSFRichTextString(student1[i]);
  cell.setCellValue(text);
 }

 //         
 HSSFRow row2 = sheet.createRow(2);
 for (int i = 0; i < student2.length; i++) {
  HSSFCell cell = row2.createCell(i);
  HSSFRichTextString text = new HSSFRichTextString(student2[i]);
  cell.setCellValue(text);
 }

 //         
 HSSFRow row3 = sheet.createRow(3);
 for (int i = 0; i < student3.length; i++) {
  HSSFCell cell = row3.createCell(i);
  HSSFRichTextString text = new HSSFRichTextString(student3[i]);
  cell.setCellValue(text);
 }

 //   Excel      response       
 //      
 response.setContentType("application/octet-stream");

 //         Excel   ,     student.xls
 response.setHeader("Content-disposition", "attachment;filename=student.xls");

 //    
 response.flushBuffer();

 //workbook Excel   response     ,     
 workbook.write(response.getOutputStream());
 }
}
그리고 인터페이스 에 접근 하여 페이지 팝 업:

이 Excel 을 다운로드 하고 다음 그림 을 엽 니 다.

지금까지 SpringBoot 의 백 스테이지 Controller 인터페이스 에서 Excel 데이터 시트 를 내 보 내 는 데 성 공 했 습 니 다!
나중에 나 는 프로젝트 에서 도구 류 로 사용 할 수 있 는 정적 방법 을 봉 했다.

import org.apache.poi.hssf.usermodel.*;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * Excel   
 */
public class ExcelUtil {

 /**
 * Excel    
 * @param response HttpServletResponse  
 * @param excelData Excel     ,   List<List<String>>
 * @param sheetName sheet   
 * @param fileName   Excel    
 * @param columnWidth Excel     ,   15
 * @throws IOException  IO  
 */
 public static void exportExcel(HttpServletResponse response,
     List<List<String>> excelData, 
     String sheetName, 
     String fileName, 
     int columnWidth) throws IOException {

 //       
 HSSFWorkbook workbook = new HSSFWorkbook();

 //      ,      
 HSSFSheet sheet = workbook.createSheet(sheetName);

 //       
 sheet.setDefaultColumnWidth(columnWidth);

 //  List<List<String>>    
 int rowIndex = 0;
 for(List<String> data : excelData){
  //    row ,    1
  HSSFRow row = sheet.createRow(rowIndex++);

  //        
  for (int i = 0; i < data.size(); i++) {
  //       
  HSSFCell cell = row.createCell(i);

  //        
  HSSFRichTextString text = new HSSFRichTextString(data.get(i));

  //                 
  cell.setCellValue(text);
  }
 }

 //   Excel      response       
 //      
 response.setContentType("application/octet-stream");

 //    Excel   
 response.setHeader("Content-disposition", "attachment;filename=" + fileName);

 //    
 response.flushBuffer();

 //workbook Excel   response     ,      Excel  
 workbook.write(response.getOutputStream());

 //  workbook
 workbook.close();
 }
}
상기 방법 호출 예시:

/**
 * Excel      
 * http://localhost:8080/ExcelDownload
 * @param response response  
 * @throws IOException  IO  
 */
@RequestMapping("/ExcelDownload")
public void excelDownload(HttpServletResponse response) throws IOException {

 List<List<String>> excelData = new ArrayList<>();

 List<String> head = new ArrayList<>();
 head.add("   ");
 head.add("   ");
 head.add("   ");

 List<String> data1 = new ArrayList<>();
 data1.add("123");
 data1.add("234");
 data1.add("345");

 List<String> data2 = new ArrayList<>();
 data2.add("abc");
 data2.add("bcd");
 data2.add("cde");

 excelData.add(head);
 excelData.add(data1);
 excelData.add(data2);

 String sheetName = "  ";
 String fileName = "ExcelTest.xls";

 ExcelUtil.exportExcel(response, excelData, sheetName, fileName, 15);
}
이상 의 이 편 은 SpringBoot:SpringBoot 에서 엑셀 을 내 보 내 는 인터페이스 튜 토리 얼 을 만 드 는 것 이 바로 편집장 이 여러분 에 게 공유 하 는 모든 내용 입 니 다.참고 하 시기 바 랍 니 다.여러분 들 도 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.

좋은 웹페이지 즐겨찾기