Java工具类(two)
poi导入工具类
maven依赖
< artifactid & gt; poi-ooxml & lt;/artifactid & gt;
& lt; version & gt; 3.9 & lt;/version & gt; & lt;/lt;/lt;/gt;
package cc.vvxtoys.poi; import java.math.bigdecimal; 수입 Java. text.SimpleDateFormat;import org.apache.poi.hssf.usermodel.HSSFCell;import org.apache.poi.hssf.usermodel.HSSFDateUtil;import org.apache.poi.ss.usermodel.Cell;import org.apache.poi. ss.usermodel.Row;import org.apache.poi.xssf.usermodel.XSSFCell;public class ImportExcelUtils { public static final String EXCEL_2003 = ".xls"; public static final String EXCEL_2007 = ".xlsx";
@SuppressWarnings("static-access") public String getValue(XSSFCell cell) { if (cell.getCellType() == cell.CELL_TYPE_BOOLEAN) { return String.valueOf(cell .getBooleanCellValue()).trim();
} else if (cell.getCellType() == cell.CELL_TYPE_NUMERIC) { if (HSSFDateUtil.isCellDateFormatted(cell)) {
SimpleDateFormat sdf = new Simple DateFormat("yyyy-MM-dd "); return String.valueOf(sdf.format(HSSFDateUtil.getJavaDate(cell.getNumericCellValue()))).trim();
}
BigDecimal bd = new BigDecimal(String.valueOf(cell.getNumericCellValue()).trim()) ; return bd.toPlainString();
} else { return String.valueOf(cell.getStringCellValue()).trim();
}
}
@SuppressWarnings("static-access") public String getValue(HSSFC 엘 셀) { if (cell.getCellType() == cell.CELL_TYPE_BOOLEAN) { return String.valueOf(cell.getBooleanCellValue()).trim();
} else if (cell.getCellType() == cell.CELL_TYPE_NUMERIC) { if (HSSFDateUtil .isCellDateFormatted(cell)) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); return String.valueOf(sdf.format(HSSFDateUtil.getJavaDate(cell.getNumericCellValue()))).trim();
}
BigDecimal bd = new BigDecimal(String.valueOf(cell.getNumericCellValue()).trim()) ; return bd.toPlainString();
} else { return String.valueOf(cell.getStringCellValue()).trim();
}
} //判断공간行
public boolean isBlank(행 행){ for(int i= 0;i
}
} return true;
} //비공공判断
public boolean isEmpty(Object obj){ if (obj 인스턴스of HSSFCell) {
HSSFCell 셀 = (HSSFCell) obj; return getValue(cell) == null || "".equals(getValue(cell));
} if (obj instanceof XSSFCell) {
XSSFCell cell = (XSSFCell) obj; return getValue(cell) == null || "".equals(getValue(cell));
} return obj == null || obj.toString().trim().equals("");
}
}
패키지 cc.vvxtoys.poi;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.InputStream;import java.util.Map;import org.apache.poi.hssf.usermodel.HSSFWorkbook;import org.apache.poi.xssf.usermodel.XSSFWorkbook;public class ExcelService { private static ImportExcelUtils utils = new ImportExcelUtils();
public static Object getExcelParser(String path, String sheet,
String start, String end)에서 예외 발생{
InputStream is = null; if(utils.isEmpty(path)){ return new FileNotFoundException("파일을 찾을 수 없음");
}else{
is = new FileInputStream(path);
} if(path.endsWith(ImportExcelUtils.EXCEL_2003)){
HSSFWorkbook 통합 문서 = new HSSFWorkbook(is);
return importXls(start, end, workbook, sheet);
}else if(path.endsWith(ImportExcelUtils.EXCEL_2007)){
XSSFWorkbook workbook = new XSSFWorkbook(is); return importXlsx(시작, 끝, 통합 문서, 시트);
}else{
is.close(); return new Exception("type error");
}
}
public static Map
return null;
} public static Map
return null;
}
}
위 내용은 Java 도구 클래스-poi는 WeChat Niuniu 플랫폼 개발을 도구 클래스로 안내합니다.의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!