Java工具類別(二)
poi導入工具類別
maven依賴
匯入工具類別
package cc.vvictoys.poi;import java.math.BigDecimal;import java.text.SimpleDateFormat;import org.apache.poi.hssf.usermodel.HSSFCell;import org. apache.poi.hssf .usermodel.HSSFDateUtil;導入org.apache.poi.ss.usermodel.Cell;導入org.apache.poi.ss.usermodel.Row;導入org.apache.poi.xssf.usermodel.XSSFCell;公共類別ImportExcelUtils { publicstat ic Final String EXCEL_2003 = ".xls"; public static Final String EXCEL_2007 = ".xlsx";
@SuppressWarnings("static-access") public String get (XSSFCell cell) { return String .valueOf(cell.getBooleanCellValue()).trim();
} else if (cell.getCellType() == cell.CELL_TYPE_NUMERIC) SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); return String.valueOf(sdf.format(HSSFDateUtil.getJavaDate(cell.getNumericCellValue()))).trim();
}
1 修剪()); return bd.toPlainString();
} else { return String.valueOf(cell.getStringCellValue()).trim();
. static-access ") public String getValue(HSSFCell cell) { if (cell.getCellType() == cell.CELL_TYPE_BOOLEAN) { } else if (cell.getCellType () == cell.CELL_TYPE_NUMERIC) { if (HSSFDateUtil.isCellDateFormatted(cell)) {
return String.valueOf(sdf.format(HSSFDateUtil.getJavaDate(cell.getNumericCellValue()))).trim();
}
1 修剪()); return bd.toPlainString();
} else { return String.valueOf(cell.getStringCellValue()).trim();
. boolean isBlank(Row row){ for(int i=0;i
} //非空白判斷
public boolean isEmpty(Object obj) { if (obj instanceof HSSFCell) {
HSSFCell cell = (HSSFCell) obj; 返回 getValue(cell) == null || "".equals(getValue(cell));
} if (obj instanceof XSSFCell) {
返回 getValue(cell) == null || "".equals(getValue(cell));
} return obj == null || obj.toString().trim().equals("");
}
}
#service(部分)
package cc.vvictoys.poi;導入java.io.FileInputStream;導入java.io.FileNotFoundException;導入java.io.InputStream;導入java.util.Map;導入org.apache.poi.hssf. usermodel.HSSFWorkbook ;導入 org.apache.poi.xssf.usermodel.XSSFWorkbook;公共類別 ExcelService { 私有靜態 ImportExcelUtils utils = new ImportExcelUtils();
public static Object getExcelParser(String path, String Sheet,
String start, String end) throws Exception{
String start, String end) throws Exception{
if(utils.isEmpty(path)){ .
} if(path.endsWith(ImportExcelUtils.EXCEL_2003)){
HSSFWorkbook 工作簿 = new HS HSSFWorkbook 工作簿 = new HSSFbook;
return importXls(start, end, workbook,sheet);
}else if(path.endsWith(ImportExcelUtils.EXCEL_2007) FW XSSFWorkbook(is); return importXlsx(開始、結束、工作簿、工作表);
}else{
is.close(); return new Exception("型別錯誤");
}
}
public static Map
return null;##o importXlsx(String start,String end,XSSFWorkbook 工作簿, 字串表){
return n
#
#
以上是Java工具類-poi導微信牛牛平台開發入工具類的詳細內容。更多資訊請關注PHP中文網其他相關文章!