search
HomeJavajavaTutorialJava tool class-poi guides WeChat Niuniu platform development into tool class

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;
   }

}


The above is the detailed content of Java tool class-poi guides WeChat Niuniu platform development into tool class. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log?How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log?Apr 19, 2025 pm 11:45 PM

Start Spring using IntelliJIDEAUltimate version...

How to elegantly obtain entity class variable names to build database query conditions?How to elegantly obtain entity class variable names to build database query conditions?Apr 19, 2025 pm 11:42 PM

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

How to use the Redis cache solution to efficiently realize the requirements of product ranking list?How to use the Redis cache solution to efficiently realize the requirements of product ranking list?Apr 19, 2025 pm 11:36 PM

How does the Redis caching solution realize the requirements of product ranking list? During the development process, we often need to deal with the requirements of rankings, such as displaying a...

How to safely convert Java objects to arrays?How to safely convert Java objects to arrays?Apr 19, 2025 pm 11:33 PM

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

How do I convert names to numbers to implement sorting and maintain consistency in groups?How do I convert names to numbers to implement sorting and maintain consistency in groups?Apr 19, 2025 pm 11:30 PM

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products?E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products?Apr 19, 2025 pm 11:27 PM

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...

How to set the default run configuration list of SpringBoot projects in Idea for team members to share?How to set the default run configuration list of SpringBoot projects in Idea for team members to share?Apr 19, 2025 pm 11:24 PM

How to set the SpringBoot project default run configuration list in Idea using IntelliJ...

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)