poi讀取excel2010


package com.icss.test;

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

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;

public class ImportExcelToDatabase {

    
    public static void main(String[] args) {
        
        //String excel2003_2007 = Common.STUDENT_INFO_XLS_PATH;
        String excel2010 = "C:\\Users\\gls\\Desktop\\移動手機號碼測試格式.xlsx";
        
        List<NodeAddress> list = readExcel(excel2010);
        if(list!=null){
            for (NodeAddress n : list) {
                
                System.out.println("time="+n.getTime()+"地區="+n.getArea()+"縣市="+n.getCounty()+"區域="+n.getDistrict()
                        +"鄉鎮="+n.getVillage()+"電話號碼="+n.getTelephone());
                
            }
            
        }
        
    }
    
    
    private static List<NodeAddress> readExcel(String path){
        List<NodeAddress> list = new ArrayList<NodeAddress>();
        try {
            InputStream is =  new FileInputStream(path);
           XSSFWorkbook xssfWorkbook = new XSSFWorkbook(is);
           NodeAddress nodeAddress = null;
           //讀sheet
           for (int numSheet = 0; numSheet < xssfWorkbook.getNumberOfSheets(); numSheet++) {
            XSSFSheet xssfSheet = xssfWorkbook.getSheetAt(numSheet);
            if(xssfSheet==null){
                continue;
            }
            //讀行
            for(int rowNum=1;rowNum<=xssfSheet.getLastRowNum();rowNum++){
                XSSFRow xssfRow = xssfSheet.getRow(rowNum);
                if(xssfRow!=null){
                    nodeAddress = new NodeAddress();
                    XSSFCell time = xssfRow.getCell(0);
                    
                    XSSFCell area = xssfRow.getCell(1);
                    XSSFCell county = xssfRow.getCell(2);
                    XSSFCell district = xssfRow.getCell(3);
                    XSSFCell village = xssfRow.getCell(4);
                    XSSFCell telehone = xssfRow.getCell(5);
                    nodeAddress.setTime(getValue(time));
                    nodeAddress.setArea(getValue(area));
                    nodeAddress.setCounty(getValue(county));
                    nodeAddress.setDistrict(getValue(district));
                    nodeAddress.setVillage(getValue(village));
                    nodeAddress.setTelephone(getValue(telehone));
                    list.add(nodeAddress);
                    
                }
                
            }
               return list;
        }
           
            
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }
    
    private static String getValue(XSSFCell xssfRow){
        if(xssfRow.getCellType()==xssfRow.CELL_TYPE_BOOLEAN){
            return String.valueOf(xssfRow.getBooleanCellValue());
        }else if(xssfRow.getCellType()==xssfRow.CELL_TYPE_NUMERIC){
            return String.valueOf(xssfRow.getNumericCellValue());
        }else{
            return String.valueOf(xssfRow.getStringCellValue());
        }
        
    }
    
    
    
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM