반응형

# Naver Search API 활용 (Excel파일 읽기)

## 준비물

  • https://mvnrepository.com/ 에서 필요한 API 다운로드
  • Apache POI API : 마이크로소프트에서 만든 파일의 데이터를 핸들링 할 수 있는 API.
  • commons-codec API, commons-collections API

poi-4.0.1.jar
2.59MB
commons-codec-1.15.jar
0.34MB
commons-collections4-4.4.jar
0.72MB

  • Excel 파일 생성 (title, author, company, isbn, imageUrl 항목으로 데이터 입력)

## 작업소스

  • ExcelVO
package kr.inflearn;

public class ExcelVO {
	String title;
	String author;
	String company;
	String isbn;
	String imageUrl;
	public ExcelVO() {}
	public ExcelVO(String title, String author, String company, String isbn, String imageUrl) {
		super();
		this.title = title;
		this.author = author;
		this.company = company;
		this.isbn = isbn;
		this.imageUrl = imageUrl;
	}
	public ExcelVO(String title, String author, String company) {
		super();
		this.title = title;
		this.author = author;
		this.company = company;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public String getAuthor() {
		return author;
	}
	public void setAuthor(String author) {
		this.author = author;
	}
	public String getCompany() {
		return company;
	}
	public void setCompany(String company) {
		this.company = company;
	}
	public String getIsbn() {
		return isbn;
	}
	public void setIsbn(String isbn) {
		this.isbn = isbn;
	}
	public String getImageUrl() {
		return imageUrl;
	}
	public void setImageUrl(String imageUrl) {
		this.imageUrl = imageUrl;
	}
	@Override
	public String toString() {
		return "ExcelVO [title=" + title + ", author=" + author + ", company=" + company + ", isbn=" + isbn
				+ ", imageUrl=" + imageUrl + "]";
	}
}
  • Project03_A
import java.io.FileInputStream;
import java.io.IOException;
import java.util.*;

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.Row;

import kr.inflearn.ExcelVO;

public class Project03_A {
	public static void main(String[] args) {
		String filename = "bookList.xls";
		
		List<ExcelVO> data = new ArrayList<ExcelVO>();
		
		try (FileInputStream fis = new FileInputStream(filename)) {
			HSSFWorkbook workbook = new HSSFWorkbook(fis);	// bookList.xls 파일을 가리킴.
			HSSFSheet sheet = workbook.getSheetAt(0);		// Excel 파일 첫번째 시트.
			Iterator<Row> rows = sheet.rowIterator();		// Excel 파일 첫번째 시트 내 row.
			rows.next();									// 두번째 row부터 시작되도록 함.
			
			String[] imsi = new String[5];
			
			while (rows.hasNext()) {
				HSSFRow row = (HSSFRow) rows.next();
				Iterator<Cell> cells = row.cellIterator();
				
				int i = 0;
				while (cells.hasNext()) {
					HSSFCell cell = (HSSFCell) cells.next();
					imsi[i] = cell.toString();
					i++;
				}
				
				// 데이터 저장 시 : 묶음(VO) -> 담기(List)
				ExcelVO vo = new ExcelVO(imsi[0], imsi[1], imsi[2], imsi[3], imsi[4]);
				data.add(vo);
			}
			showExcelData(data);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private static void showExcelData(List<ExcelVO> data) {
		for (ExcelVO vo : data) {
			System.out.println(vo);
		}
	}
}
  • 실행 시 아래와 같이 출력

반응형

+ Recent posts