import java.io.FileInputStream;
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;
public class XlsRead {
public static void main(String[] args) {
HSSFRow row;
HSSFCell cell;
try {
FileInputStream inputStream = new FileInputStream("XlsRead.xls");
POIFSFileSystem fileSystem = new POIFSFileSystem(inputStream);
HSSFWorkbook workbook = new HSSFWorkbook(fileSystem);
//sheet수 취득
int sheetCn = workbook.getNumberOfSheets();
System.out.println("sheet수 : " + sheetCn);
for(int cn = 0; cn < sheetCn; cn++){
System.out.println("취득하는 sheet 이름 : " + workbook.getSheetName(cn));
System.out.println(workbook.getSheetName(cn) + " sheet 데이터 취득 시작");
//cn번째 sheet 정보 취득
HSSFSheet sheet = workbook.getSheetAt(cn);
//취득된 sheet에서 rows수 취득
int rows = sheet.getPhysicalNumberOfRows();
System.out.println(workbook.getSheetName(cn) + " sheet의 row수 : " + rows);
//취득된 row에서 취득대상 cell수 취득
int cells = sheet.getRow(cn).getPhysicalNumberOfCells(); //
System.out.println(workbook.getSheetName(cn) + " sheet의 row에 취득대상 cell수 : " + cells);
for (int r = 0; r < rows; r++) {
row = sheet.getRow(r);
if (row != null) {
for (int c = 0; c < cells; c++) {
cell = row.getCell(c);
if (cell != null) {
String value = null;
switch (cell.getCellType()) {
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:
value = "[type?]";
break;
}
System.out.print(value + "\t");
} else {
System.out.print("[null]\t");
} // if
} // for(c)
System.out.print("\n");
} // if
} // for(r)
System.out.println(workbook.getSheetName(cn) + " sheet 데이터 취득 종료");
}// for(cn)
} catch (Exception e) {
e.printStackTrace();
}
}
}
QRCode 출력 및 바코드 출력 (0) | 2014.12.19 |
---|---|
PDF 파일 이미지 변환 (0) | 2014.12.19 |
Java Excel 읽기(.xlsx) (0) | 2014.12.19 |
Java Email 전송 (0) | 2014.12.19 |
Java Excel 파일 생성 (0) | 2014.12.19 |