Rumah >Java >javaTutorial >Java工具类-poi导微信牛牛平台开发入工具类

Java工具类-poi导微信牛牛平台开发入工具类

phpcn_u87897
phpcn_u87897asal
2018-01-09 15:09:381634semak imbas

Java工具类(二)

poi导入工具类

maven依赖


   org.apache.poi
   poi-ooxml
   3.9

导入工具类

package cc.vvxtoys.poi;import java.math.BigDecimal;import 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 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();
       }
   }

   @SuppressWarnings("static-access")    public  String getValue(HSSFCell 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 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(Row row){        for(int i=0;i Cell cell = row.getCell(i); if(cell!=null&&cell.getCellType()!=cell.CELL_TYPE_BLANK){ return false;
}
} return true;
} //非空判断
public boolean isEmpty(Object obj){ if (obj instanceof HSSFCell) {
HSSFCell cell = (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("");
}


}

service(部分)

package 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) throws Exception{
InputStream is = null; if(utils.isEmpty(path)){ return new FileNotFoundException("file not found");
}else{
is = new FileInputStream(path);
} if(path.endsWith(ImportExcelUtils.EXCEL_2003)){
HSSFWorkbook workbook = new HSSFWorkbook(is);
return importXls(start, end, workbook, sheet);
}else if(path.endsWith(ImportExcelUtils.EXCEL_2007)){
XSSFWorkbook workbook = new XSSFWorkbook(is); return importXlsx(start, end, workbook, sheet);
}else{
is.close(); return new Exception("type error");
}


}
public static Map importXls(String start,String end,HSSFWorkbook workbook,            String sheet){        
       return null;
   }    public static Map importXlsx(String start,String end,XSSFWorkbook workbook,            String sheet){        
       return null;
   }

}


Atas ialah kandungan terperinci Java工具类-poi导微信牛牛平台开发入工具类. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn