테이블 우클릭 - 익스포트
엑셀 파일 들어가서 데이터 입력 후 파일(book.xlsx)을 eclipse 프로젝트- excel 폴더에 저장
<ExcelRead.java>
public class ExcelRead {
public static void main(String[] args) {
ExcelRead er = new ExcelRead();
er.process();
}
public void process() {
try {
File file = new File("excel/book.xls");
FileInputStream fis = new FileInputStream(file);
Workbook workbook = null;
if(file.getName().endsWith(".xls")) workbook = new HSSFWorkbook(fis);
Sheet sheet = workbook.getSheetAt(0);
int startRow = sheet.getFirstRowNum();
int endRow = sheet.getLastRowNum();
for(int i=startRow;i<=endRow;i++)
{
Row row = sheet.getRow(i);
int startCell = row.getFirstCellNum();
int endCell = row.getLastCellNum();
for(int j=startCell;j<=endCell;j++)
{
Cell cell = row.getCell(j);
String data = ExcelUtil.getCellData(cell);
System.out.println(data+"\t");
}
System.out.println();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
참고로 xls 파일이면 HSSFWorkbook
xlsx파일이면 XSSFWorkbook
import해주면 되는데 XSSFWorkbook import가 안되면
여기서 poi ooxml 검색 후 코드 복사해서 pom.xml에 붙여넣으세요
결과 출력