欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java 中POI 導入EXCEL2003 和EXCEL2007的實現(xiàn)方法

 更新時間:2017年09月08日 15:46:11   作者:夏夜晴空  
這篇文章主要介紹了Java 中POI 導入EXCEL2003 和EXCEL2007的實現(xiàn)方法的相關(guān)資料,希望通過本文大家能掌握理解這種方法,需要的朋友可以參考下

Java 中POI 導入EXCEL2003 和EXCEL2007的實現(xiàn)方法

實現(xiàn)代碼:

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<Object[]> importExcel(String filePath) throws Exception{ 
    List<Object[]> list=new ArrayList<Object[]>(); 
    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<Object[]> importExcel03(String filePath) throws IOException{ 
    FileInputStream in=new FileInputStream(filePath); 
    List<Object[]> list=new ArrayList<Object[]>(); 
    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<rows;i++){ 
      row=sheet.getRow(i); 
      csr=new String[cells]; 
      for(int j=0;j<cells;j++){ 
        HSSFCell cell=row.getCell(j); 
        Object obj=null; 
        if(cell!=null){ 
          obj=getValue(cell); 
        } 
        csr[j]=obj; 
      } 
      list.add(csr); 
    } 
    if(in!=null)in.close(); 
    return list; 
  } 
  public List<Object[]> importExcel07(String filePath) throws IOException{ 
    List<Object[]> list=new ArrayList<Object[]>(); 
    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<rows;i++){ 
      row=sheet.getRow(i); 
      csr=new String[cells]; 
      for(int j=0;j<cells;j++){ 
        XSSFCell cell=row.getCell(j); 
        Object obj=null; 
        if(cell!=null){ 
          obj=getValue(cell); 
        } 
        csr[j]=obj; 
      } 
      list.add(csr); 
    } 
    if(in!=null)in.close(); 
    return list; 
  } 
  @SuppressWarnings("static-access") 
  public String getValue(Cell cell){ 
    int type=cell.getCellType(); 
    String s=""; 
    if(type==cell.CELL_TYPE_NUMERIC){ 
      if(HSSFDateUtil.isCellDateFormatted(cell)){ 
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
        s=sdf.format(cell.getDateCellValue()); 
      }else { 
        BigDecimal db = new BigDecimal(cell.getNumericCellValue()); 
        s=String.valueOf(db); 
      } 
    }else if(type==cell.CELL_TYPE_STRING){ 
      s=cell.getStringCellValue(); 
    }else if(type==cell.CELL_TYPE_BOOLEAN){ 
      s=cell.getBooleanCellValue()+""; 
    }else if(type==cell.CELL_TYPE_FORMULA){ 
      s=cell.getCellFormula(); 
    }else if(type==cell.CELL_TYPE_BLANK){ 
      s=" "; 
    }else if(type==cell.CELL_TYPE_ERROR){ 
      s=" "; 
    }else{ 
       
    } 
    return s.trim(); 
  } 
  /** 
   * @param args 
   */ 
  public static void main(String[] args) { 
    ExcelUtils ex=new ExcelUtils(); 
    try { 
      List<Object[]> 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

如有疑問請留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

相關(guān)文章

  • controller接口跳轉(zhuǎn)到另一個controller接口的實現(xiàn)

    controller接口跳轉(zhuǎn)到另一個controller接口的實現(xiàn)

    這篇文章主要介紹了controller接口跳轉(zhuǎn)到另一個controller接口的實現(xiàn)方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • Springboot文件上傳功能簡單測試

    Springboot文件上傳功能簡單測試

    這篇文章主要介紹了Springboot文件上傳功能簡單測試,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-05-05
  • 使用Cloud Toolkit在IDEA中極速創(chuàng)建dubbo工程

    使用Cloud Toolkit在IDEA中極速創(chuàng)建dubbo工程

    這篇文章主要介紹了使用Cloud Toolkit在IDEA中極速創(chuàng)建dubbo工程,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-11-11
  • SpringBoot的父級依賴:spring-boot-starter-parent詳解

    SpringBoot的父級依賴:spring-boot-starter-parent詳解

    SpringBoot通過父級依賴spring-boot-starter-parent實現(xiàn)項目快速構(gòu)建,它依賴于spring-boot-dependencies來統(tǒng)一管理項目中的依賴版本,省去了手動指定版本信息的麻煩,這種機制不僅規(guī)定了默認的Java版本和編碼格式
    2024-09-09
  • 關(guān)于@DS注解切換數(shù)據(jù)源失敗的原因?qū)崙?zhàn)記錄

    關(guān)于@DS注解切換數(shù)據(jù)源失敗的原因?qū)崙?zhàn)記錄

    項目配置了多個數(shù)據(jù)源,需要使用@DS注解來切換數(shù)據(jù)源,但是卻遇到了問題,下面這篇文章主要給大家介紹了關(guān)于@DS注解切換數(shù)據(jù)源失敗原因的相關(guān)資料,需要的朋友可以參考下
    2023-05-05
  • Java解析DICOM圖之如何獲得16進制數(shù)據(jù)詳解

    Java解析DICOM圖之如何獲得16進制數(shù)據(jù)詳解

    DICOM就是醫(yī)學數(shù)字成像和通信,是醫(yī)學圖像和相關(guān)信息的國際標準(ISO 12052),下面這篇文章主要給大家介紹了關(guān)于Java解析DICOM圖之如何獲得16進制數(shù)據(jù)的相關(guān)資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考下。
    2017-10-10
  • Java異常退出條件的判斷示例代碼

    Java異常退出條件的判斷示例代碼

    這篇文章主要介紹了Java異常退出條件的判斷示例代碼,涉及常見異常退出條件等相關(guān)內(nèi)容,具有一定參考價值,需要的朋友可以了解下。
    2017-10-10
  • Java日期毫秒值和常見日期時間格式相互轉(zhuǎn)換方法

    Java日期毫秒值和常見日期時間格式相互轉(zhuǎn)換方法

    這篇文章主要給大家介紹了關(guān)于Java日期毫秒值和常見日期時間格式相互轉(zhuǎn)換的相關(guān)資料,在Java的日常開發(fā)中,會隨時遇到需要對時間處理的情況,文中給出了詳細的示例代碼,需要的朋友可以參考下
    2023-07-07
  • 關(guān)于@EnableGlobalMethodSecurity注解的用法解讀

    關(guān)于@EnableGlobalMethodSecurity注解的用法解讀

    這篇文章主要介紹了關(guān)于@EnableGlobalMethodSecurity注解的用法解讀,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-03-03
  • Java?Spring框架創(chuàng)建項目與Bean的存儲與讀取詳解

    Java?Spring框架創(chuàng)建項目與Bean的存儲與讀取詳解

    本篇文章將介紹Spring項目的創(chuàng)建,IDEA國內(nèi)源的配置以及Bean的存儲與讀取,所謂的Bean其實就是對象的意思,更詳細地說Spring Bean是被實例的,組裝的及被Spring 容器管理的Java對象
    2022-07-07

最新評論