SpringBoot:SpringBoot 에서 Excel 내 보 내기 인터페이스 튜 토리 얼 을 만 듭 니 다.
7163 단어 SpringBootExcel인터페이스
우선 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 에서 엑셀 을 내 보 내 는 인터페이스 튜 토리 얼 을 만 드 는 것 이 바로 편집장 이 여러분 에 게 공유 하 는 모든 내용 입 니 다.참고 하 시기 바 랍 니 다.여러분 들 도 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
【Java・SpringBoot・Thymeleaf】 에러 메세지를 구현(SpringBoot 어플리케이션 실천편 3)로그인하여 사용자 목록을 표시하는 응용 프로그램을 만들고, Spring에서의 개발에 대해 공부하겠습니다 🌟 마지막 데이터 바인딩에 계속 바인딩 실패 시 오류 메시지를 구현합니다. 마지막 기사🌟 src/main/res...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.