본문 바로가기
dev/java

JAVA 엑셀 파일 읽기 & 쓰기

by NCJ 2019. 1. 9.
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