Excel 도구 클래스 POIExcelUtil
3768 단어 Excel
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
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.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class POIExcelUtil {
public static void readXls() throws IOException {
InputStream is = new FileInputStream("D:\\excel\\xls_test2.xls");
HSSFWorkbook hssfWorkbook = new HSSFWorkbook(is);
// Sheet
for (int numSheet = 0; numSheet < hssfWorkbook.getNumberOfSheets(); numSheet++) {
HSSFSheet hssfSheet = hssfWorkbook.getSheetAt(numSheet);
if (hssfSheet == null) {
continue;
}
// Row
for (int rowNum = 0; rowNum <= hssfSheet.getLastRowNum(); rowNum++) {
HSSFRow hssfRow = hssfSheet.getRow(rowNum);
if (hssfRow == null) {
continue;
}
// Cell
for (int cellNum = 0; cellNum <= hssfRow.getLastCellNum(); cellNum++) {
HSSFCell hssfCell = hssfRow.getCell(cellNum);
if (hssfCell == null) {
continue;
}
System.out.print(" " + getValue(hssfCell));
}
System.out.println();
}
}
}
public static String getValue(HSSFCell hssfCell) {
if (hssfCell.getCellType() == hssfCell.CELL_TYPE_BOOLEAN) {
return String.valueOf(hssfCell.getBooleanCellValue());
} else if (hssfCell.getCellType() == hssfCell.CELL_TYPE_NUMERIC) {
return String.valueOf(hssfCell.getNumericCellValue());
} else {
return String.valueOf(hssfCell.getStringCellValue());
}
}
//xlsx
public static void readXlsx() throws IOException{
String fileName = "D:\\excel\\xlsx_test.xlsx";
XSSFWorkbook xssfWorkbook = new XSSFWorkbook( fileName);
// Sheet
for(int numSheet = 0; numSheet < xssfWorkbook.getNumberOfSheets(); numSheet++){
XSSFSheet xssfSheet = xssfWorkbook.getSheetAt( numSheet);
if(xssfSheet == null){
continue;
}
// Row
for(int rowNum = 0; rowNum <= xssfSheet.getLastRowNum(); rowNum++ ){
XSSFRow xssfRow = xssfSheet.getRow( rowNum);
if(xssfRow == null){
continue;
}
// Cell
for(int cellNum = 0; cellNum <= xssfRow.getLastCellNum(); cellNum++){
XSSFCell xssfCell = xssfRow.getCell( cellNum);
if(xssfCell == null){
continue;
}
System.out.print(" "+getsValue(xssfCell));
}
System.out.println();
}
}
}
public static String getsValue(XSSFCell xssfCell){
if(xssfCell.getCellType() == xssfCell.CELL_TYPE_BOOLEAN){
return String.valueOf( xssfCell.getBooleanCellValue());
}else if(xssfCell.getCellType() == xssfCell.CELL_TYPE_NUMERIC){
return String.valueOf( xssfCell.getNumericCellValue());
}else{
return String.valueOf( xssfCell.getStringCellValue());
}
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Excel Grep toolExcel Grep tool ■히나가타 ■ 시트 구성 ExcelGrep.cls...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.