반응형
# Naver Search API 활용 Excel에서 Cell의 데이터타입 알아보기
## 작업소스
- 아래와 같이 엑셀 파일 생성하여 저장.
- 아래와 같이 작업하여 셀타입 확인 가능.
import java.io.FileInputStream;
import java.util.Iterator;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Row;
public class Project03_C {
public static void main(String[] args) {
String fileName = "cellDataType.xls";
try (FileInputStream fis = new FileInputStream(fileName)) {
HSSFWorkbook workbook = new HSSFWorkbook(fis);
HSSFSheet sheet = workbook.getSheetAt(0);
Iterator<Row> rows = sheet.rowIterator();
while (rows.hasNext()) {
HSSFRow row = (HSSFRow) rows.next();
Iterator<Cell> cells = row.cellIterator();
while (cells.hasNext()) {
HSSFCell cell = (HSSFCell) cells.next();
CellType type = cell.getCellType();
if (type == CellType.STRING) {
System.out.println("[" + cell.getRowIndex()
+ "," + cell.getColumnIndex() + "]"
+ " = STRING; Value : " + cell.getRichStringCellValue());
} else if (type == CellType.NUMERIC) {
System.out.println("[" + cell.getRowIndex()
+ "," + cell.getColumnIndex() + "]"
+ " = NUMERIC; Value : " + cell.getNumericCellValue());
} else if (type == CellType.BOOLEAN) {
System.out.println("[" + cell.getRowIndex()
+ "," + cell.getColumnIndex() + "]"
+ " = BOOLEAN; Value : " + cell.getBooleanCellValue());
} else if (type == CellType.BLANK) {
System.out.println("[" + cell.getRowIndex()
+ "," + cell.getColumnIndex() + "]"
+ " = BLANK; Value : BLANK CELL");
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
반응형
'인프런 강의 학습 > Java TPC 실전' 카테고리의 다른 글
Naver Search API 활용 도서정보를 입력하여 검출 후 Excel에 저장하기 (0) | 2022.07.22 |
---|---|
Naver Search API 활용 도서정보를 입력하여 ISBN, IMAGE 검출하기 (0) | 2022.07.20 |
API활용_Naver Search API 활용 Excel에 image 저장하기 (0) | 2022.07.11 |
API활용_Naver Search API 활용 Excel파일 읽기 (0) | 2022.07.04 |
API활용_Jsoup API 이용한 크롤링(GUI 버전) (0) | 2022.06.22 |