-
[poi] excel파일 다운로드 받기잡동사니 2022. 10. 7. 10:10
라이브러리 설치
먼저 poi라이브러리를 다운받아야 한다.
poi 홈페이지에 접속한다.
왼쪽 Overview에서 download탭을 클릭
나는 3점대 버전을 사용할 것이므로(받아야할 라이브러리가 가장 간단하다.) 맨 마지막으로 내려가서 Binary Artifacts를 클릭한다.
3.17버전을 다운 받았다.
zip파일 압축을 풀면 가장 먼저 보이는 jar파일들을 이클립스 프로젝트의 라이브러리 폴더에 옮겨준다. 나는 WEB-INF폴더의 lib폴더에 옮겨줬다.
압축을 푼 파일에서 lib폴더에 있는 jar파일도 옮겨준다.
ooxml-lib폴더의 jar파일들도 옮겨준다.
다운받은 파일의 모든 jar파일을 옮겨준다고 생각하면 된다.
ExcelUtil
import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import com.spring.board.vo.BoardVo; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.util.List; public class ExcelUtil { public static String filePath = "C://poi_temp";//이미 로컬에 존재하는 폴더 public static String fileNm; //파일이름과 워크시트 이름은 인자로 받아준다. public static void makeWorkSheet(String fileNm,String title,List<BoardVo> boardList) { try (XSSFWorkbook workbook = new XSSFWorkbook()) { XSSFSheet sheet = workbook.createSheet(title); Row firstRow = sheet.createRow(0);//첫번째 행은 제목행 Cell boardNumCellf = firstRow.createCell(0); boardNumCellf.setCellValue("boardNum"); Cell boardTypeCellf = firstRow.createCell(1); boardTypeCellf.setCellValue("boardType"); Cell boardTitleCellf = firstRow.createCell(2); boardTitleCellf.setCellValue("boardTitle"); Cell boardCommentCellf = firstRow.createCell(3); boardCommentCellf.setCellValue("boardComment"); Cell boardCreatorCellf = firstRow.createCell(4); boardCreatorCellf.setCellValue("creator"); Cell boardModifierCellf = firstRow.createCell(5); boardModifierCellf.setCellValue("modifier"); for(int i = 1;i<boardList.size()+1; i++) {//첫번째 행 다음부터 board 내용 출력 Row row = sheet.createRow(i); Cell boardNumCell = row.createCell(0); boardNumCell.setCellValue(boardList.get(i-1).getBoardNum()); Cell boardTypeCell = row.createCell(1); boardTypeCell.setCellValue(boardList.get(i-1).getBoardType()); Cell boardTitleCell = row.createCell(2); boardTitleCell.setCellValue(boardList.get(i-1).getBoardTitle()); Cell boardCommentCell = row.createCell(3); boardCommentCell.setCellValue(boardList.get(i-1).getBoardComment()); Cell boardCreatorCell = row.createCell(4); boardCreatorCell.setCellValue(boardList.get(i-1).getCreator()); Cell boardModifierCell = row.createCell(5); boardModifierCell.setCellValue(boardList.get(i-1).getModifier()); } try { FileOutputStream out = new FileOutputStream(new File(filePath, fileNm)); workbook.write(out); out.close(); } catch (IOException e) { e.printStackTrace(); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
결과
'잡동사니' 카테고리의 다른 글
[백준][java] 이친수 2193번 (0) 2022.10.09 [백준][java] 쉬운 계단 수 10844번 (0) 2022.10.09 회원가입 유효성 검증: 한글만 추출하기 (1) 2022.10.05 [백준][java] 1,2,3 더하기2 15990번 (1) 2022.10.03 [백준][java] 카드 구매하기11052 (0) 2022.10.02