1. 다운로드 사이트 : http://mirror.apache-kr.org/poi/release/bin/
2. API : http://jakarta.apache.org/poi/apidocs/index.html
3. Guide : http://jakarta.apache.org/poi/hssf/quick-guide.html
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.poifs.filesystem.POIFSFileSystem;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
public class test{
public test(){}
public static void main(String[] args){
try{
//디비접속 및 쿼리문 생성
Class.forName("org.gjt.mm.mysql.Driver");
Connection conn=DriverManager.getConnection("URL", "ID", "PW");
PreparedStatement stat=conn.prepareStatement("insert into test values(?,?,?,?)");
//엑셀파일 로드
POIFSFileSystem fs=new POIFSFileSystem(new FileInputStream("C:/test.xls"));
HSSFWorkbook wb=new HSSFWorkbook(fs);
int sheetNum=wb.getNumberOfSheets(); //시트갯수 가져오기
for(int i=0; i<sheetNum; i++){
HSSFSheet sheet=wb.getSheetAt(i); //시트 가져오기
int rows=sheet.getPhysicalNumberOfRows(); //행 갯수 가져오기
for(int j=0; j<rows; j++){ //row 루프
HSSFRow row=sheet.getRow(j); //row 가져오기
if(row!=null){
int count=0;
int cells=row.getPhysicalNumberOfCells(); //cell 갯수 가져오기
System.out.println("row:"+row.getRowNum()+", cells:"+cells);
for(short c=0; c<cells; c++){ //cell 루프
HSSFCell cell=row.getCell(c); //cell 가져오기
if(cell!=null){
String value=null;
switch(cell.getCellType()){ //cell 타입에 따른 데이타 저장
case HSSFCell.CELL_TYPE_FORMULA:
value=cell.getCellFormula();
break;
case HSSFCell.CELL_TYPE_NUMERIC:
value=""+cell.getNumericCellValue();
break;
case HSSFCell.CELL_TYPE_STRING:
value=""+cell.getStringCellValue();
break;
case HSSFCell.CELL_TYPE_BLANK:
value=""+cell.getBooleanCellValue();
break;
case HSSFCell.CELL_TYPE_ERROR:
value=""+cell.getErrorCellValue();
break;
default:
}
System.out.println("cell:"+cell.getCellNum()+", value:"+value);
stat.setString(count, value); //쿼리문 값 대입
count++;
}
}
stat.executeUpdate(); //쿼리문 insert 실행
}
}
}
conn.close();