Home  >  Article  >  Java  >  Examples of implementing POI import into EXCEL2003 and EXCEL2007 in Java

Examples of implementing POI import into EXCEL2003 and EXCEL2007 in Java

黄舟
黄舟Original
2017-09-09 11:04:401556browse

这篇文章主要介绍了Java 中POI 导入EXCEL2003 和EXCEL2007的实现方法的相关资料,希望通过本文大家能掌握理解这种方法,需要的朋友可以参考下

Java 中POI 导入EXCEL2003 和EXCEL2007的实现方法

实现代码:


import java.io.FileInputStream; 
import java.io.IOException; 
import java.math.BigDecimal; 
import java.text.SimpleDateFormat; 
import java.util.ArrayList; 
import java.util.List; 
 
import org.apache.poi.POIXMLException; 
import org.apache.poi.hssf.usermodel.HSSFCell; 
import org.apache.poi.hssf.usermodel.HSSFDateUtil; 
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.poifs.filesystem.OfficeXmlFileException; 
import org.apache.poi.ss.usermodel.Cell; 
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; 
 
/** 
 * excel 导入功能 
 * @author lyq 20150312 
 * @version 1.0 
 */ 
public class ExcelUtils { 
   
  public List importExcel(String filePath) throws Exception{ 
    List list=new ArrayList(); 
    String fileType=filePath.substring(filePath.lastIndexOf(".")+1); 
    try { 
      if("xls".equalsIgnoreCase(fileType)){ 
        list= importExcel03(filePath); 
      }else{ 
        list= importExcel07(filePath); 
      } 
    } catch(OfficeXmlFileException e){//通过手动修改文件名 引起的异常 比如 3.xlsx 重命名 3.xls 其实际文件类型为xlsx 
      list=importExcel07(filePath); 
    } catch(POIXMLException e){//通过手动修改文件名 引起的异常 比如 3.xls 重命名 3.xlsx 其实际文件类型为xls 
      list=importExcel03(filePath); 
    } 
    return list; 
  } 
  public List importExcel03(String filePath) throws IOException{ 
    FileInputStream in=new FileInputStream(filePath); 
    List list=new ArrayList(); 
    HSSFWorkbook wb=new HSSFWorkbook(in); 
    HSSFSheet sheet = wb.getSheetAt(0); 
    int rows = sheet.getPhysicalNumberOfRows(); 
    HSSFRow row=sheet.getRow(0); 
    int cells=row.getLastCellNum(); 
    Object[] csr=null; 
    for(int i=1;i importExcel07(String filePath) throws IOException{ 
    List list=new ArrayList(); 
    FileInputStream in=new FileInputStream(filePath); 
    XSSFWorkbook wb=new XSSFWorkbook(in); 
    XSSFSheet sheet = wb.getSheetAt(0); 
    int rows = sheet.getPhysicalNumberOfRows(); 
    XSSFRow row=sheet.getRow(0); 
    int cells=row.getLastCellNum(); 
    Object[] csr=null; 
    for(int i=1;i list=ex.importExcel("D:\\3.xls"); 
      for(Object[] ss:list){ 
        for(Object s:ss){ 
          System.out.print(s+"\t"); 
        } 
        System.out.println(); 
      } 
    } catch (Exception e) { 
      e.printStackTrace(); 
    } 
  } 
 
}

注意:

jdk使用版本(影响excel07)

所使用的核心jar如下

poi-3.8-20120326.jar
poi-ooxml-3.8-20120326.jar
poi-ooxml-schemas-3.8-20120326.jar
xmlbeans-2.3.0.jar

The above is the detailed content of Examples of implementing POI import into EXCEL2003 and EXCEL2007 in Java. 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