JAVA 엑셀 파일 읽기 & 쓰기

2019. 1. 9. 13:53·dev/java
728x90
반응형
SMALL


엑셀 파일 읽어서 쓰기



void WriteExcel() {


String path = excel_save_path; // 읽을 파일 경로


try {

File file = new File(path);

FileInputStream inputStream = new FileInputStream(file);

XSSFWorkbook xworkbook = new XSSFWorkbook(inputStream); // 2007 이후 버전(xlsx파일)


XSSFSheet curSheet; // 현재 sheet

HSSFCell curCell; // 현재 cell

XSSFRow curRow; // 현재 row


CellStyle cellStyle = xworkbook.createCellStyle();

cellStyle.setAlignment(HorizontalAlignment.CENTER);


cellStyle.setBorderRight(BorderStyle.THIN);

cellStyle.setBorderLeft(BorderStyle.THIN);

cellStyle.setBorderTop(BorderStyle.THIN);

cellStyle.setBorderBottom(BorderStyle.THIN);


curSheet = xworkbook.getSheetAt(0);


int row = curSheet.getPhysicalNumberOfRows();

System.out.println(row);

int k = 0;

for (int i = row; i < (result.size() + row); i++) {


curRow = curSheet.createRow(i);

if (result.size() > 0) {


if (curRow == null) {

System.out.println("null");

} else {


String Lgid = result.get(k).strLgId;


XSSFCell cell0 = curRow.createCell(0);

cell0.setCellValue(String.valueOf(GameType));

cell0.setCellStyle(cellStyle);


XSSFCell cell1 = curRow.createCell(1);

cell1.setCellValue(getleagueName(Integer.parseInt(Lgid)));

cell1.setCellStyle(cellStyle);


XSSFCell cell2 = curRow.createCell(2);

cell2.setCellValue(result.get(k).strLgId);

cell2.setCellStyle(cellStyle);


XSSFCell cell3 = curRow.createCell(3);

cell3.setCellValue(result.get(k).strLgTeam_en);

cell3.setCellStyle(cellStyle);


XSSFCell cell4 = curRow.createCell(4);

cell4.setCellValue(result.get(k).strLgTeam_kr);

cell4.setCellStyle(cellStyle);


// curRow.createCell(0).setCellValue("29");

// curRow.createCell(1).setCellValue(result.get(k).strLgId);

// curRow.createCell(2).setCellValue(result.get(k).strLgTeam_en);

// curRow.createCell(3).setCellValue(result.get(k).strLgTeam_kr);


k++;

}


}


}

xworkbook.setForceFormulaRecalculation(true); // 엑셀 수식 재계산

FileOutputStream fileOut = new FileOutputStream(excel_save_path, false); // 엑셀파일 저장경로

xworkbook.write(fileOut);// 파일에 저장

lableText.setText("저장 완료");

fileOut.close();


} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}


}

728x90
반응형
LIST

'dev > java' 카테고리의 다른 글

JAVA로 동적 logback 설정하기  (8) 2023.06.12
프로그래머스 숫자 문자열과 영단어 JAVA  (13) 2021.08.08
프로그래머스 완주하지 못한 선수- Java  (6) 2021.08.07
프로그래머스 서울에서 김서방 찾기 - Java  (4) 2021.01.31
JAVA 파일 가져오기  (0) 2019.01.09
'dev/java' 카테고리의 다른 글
  • 프로그래머스 숫자 문자열과 영단어 JAVA
  • 프로그래머스 완주하지 못한 선수- Java
  • 프로그래머스 서울에서 김서방 찾기 - Java
  • JAVA 파일 가져오기
NCJ
NCJ
일상과 개발을 공유하고 소통하는 블로그입니다.
    반응형
    250x250
  • NCJ
    NCJ 프로그래밍
    NCJ
  • 전체
    오늘
    어제
    • ALL
      • dev
        • android
        • java
        • Spring Boot
        • node.js
        • MFC
        • react-native
        • web
        • 기타
        • vue
        • react
      • 일상
        • news
        • 요리
        • 영화
        • 드라마
        • 제품
        • 게임
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    php
    던파 모바일 사전캐릭 생성
    갤럭시
    삼성
    사전캐릭
    게임
    던파 서버 오픈
    던파 출시일
    리니지w 이벤트 안내
    코딩문제
    리니지w
    Web
    리니지W 사전예약
    window webserver
    리니지w 오픈
    리니지w 이벤트
    리니지
    던파 사전캐릭
    리니지w 보상
    안드로이드
    던파 모바일 서버오픈
    apache
    java
    던파 캐릭생성
    던파 모바일 사전캐릭
    Android
    다이어트
    CSS
    갤럭시 워치 4
    갤럭시 버즈2
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
NCJ
JAVA 엑셀 파일 읽기 & 쓰기
상단으로

티스토리툴바