일부 데이터가있는 Excel 시트를 만드는 프로그램을 만들었습니다. 열 1 - 문자열 포함 열 2- 숫자를 포함합니다 (기본적으로 정수이지만 ?이 플로트로 변환하기 이유 읽는 동안)Apache POI를 사용하여 정수로 숫자 셀 값을 검색하는 방법
public class ApachePOI8 {
public static void main(String[] args) {
String file = "C:\\Eclipse workspace\\PoiExample\\Excel8.xlsx";
Workbook wb = new XSSFWorkbook();
Sheet sheet = wb.createSheet("Sheet1");
sheet.protectSheet("Pwd1"); //This statements locks the entire sheet with password: Pwd1
DataFormat fmt = wb.createDataFormat(); //created a textStyle, which prevents Excel to interprate data, in short it means it will be taken as String
CellStyle textStyle = wb.createCellStyle();
textStyle.setDataFormat(fmt.getFormat("@"));
textStyle.setLocked(false);
CellStyle numerStyle = wb.createCellStyle();
numerStyle.setDataFormat(fmt.getFormat("0"));
numerStyle.setLocked(false);
Row row0 = CellUtil.getRow(0, sheet);
for (int columnIndex = 0; columnIndex<4; columnIndex++){
Cell cell = CellUtil.getCell(row0, columnIndex);
if(columnIndex == 0) {
cell.setCellStyle(textStyle);
cell.setCellValue("Product 1");
}else if(columnIndex == 1) {
cell.setCellStyle(numerStyle);
cell.setCellValue(1);
}
}
else{
cell.setCellStyle(textStyle);
}
} //end of for Loop
try {
FileOutputStream outputStream = null;
outputStream = new FileOutputStream(file);
wb.write(outputStream);
outputStream.close();
System.out.println("Done!");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
이제
public class ApachePOIExcelRead {
private static final String FILE_NAME = "C:\\Eclipse workspace\\PoiExample\\Excel8.xlsx";
public static void main(String[] args) {
try {
FileInputStream excelFile = new FileInputStream(new File(FILE_NAME));
Workbook workbook = new XSSFWorkbook(excelFile);
Sheet datatypeSheet = workbook.getSheetAt(0);
Iterator<Row> iterator = datatypeSheet.iterator();
while (iterator.hasNext()) {
Row currentRow = iterator.next();
Iterator<Cell> cellIterator = currentRow.iterator();
while (cellIterator.hasNext()) {
Cell currentCell = cellIterator.next();
//getCellTypeEnum shown as deprecated for version 3.15
//getCellTypeEnum ill be renamed to getCellType starting from version 4.0
if (currentCell.getCellTypeEnum() == CellType.STRING) {
System.out.print("String: "+currentCell.getStringCellValue() + "\n");
} else if (currentCell.getCellTypeEnum() == CellType.NUMERIC) {
System.out.print("NUMERIC "+currentCell.getNumericCellValue() + "\n");
}
}
System.out.println();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
생성되었다 엑셀 시트를 읽어하지만 문자열로 출력을 얻고있다 : 제품 1을숫자 1.0 문자열 : 12-12-12
왜 1 즉 입력 된 번호가 부동 오고 있음을 숫자 1.0 그래서?
친절히 제안합니다.
감사합니다. Jim에게 귀중한 조언을드립니다. :) –