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

转载于:https://www.cnblogs.com/guolsblog/p/6054260.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值