zoukankan      html  css  js  c++  java
  • 基于Apache POI 从xlsx读出数据

    【0】写在前面


    package com.cwind.poi;  
    import java.io.File;
    import java.io.FileInputStream;
    import java.util.Iterator;  
    import org.apache.poi.ss.usermodel.Cell;
    import org.apache.poi.ss.usermodel.DateUtil;
    import org.apache.poi.ss.usermodel.Row;
    import org.apache.poi.xssf.usermodel.XSSFSheet;
    import org.apache.poi.xssf.usermodel.XSSFWorkbook;
    
    public class SimpleDatasheetReader {
    
        public static void main(String[] args){
    
            try {
                File excel = new File("E:/bench-cluster/temp-resource/RunningMan.xlsx");
                FileInputStream fis = new FileInputStream(excel);
    

    //创建工作簿

                XSSFWorkbook book = new XSSFWorkbook(fis);
    

    //创建工作簿下的第一页纸张

                XSSFSheet sheet = book.getSheetAt(0);               
    

    // 基于行的迭代器

                Iterator<Row> itr = sheet.iterator();   
                System.out.println(itr.hasNext());              
    

    // Iterating over Excel file in Java

                while (itr.hasNext()) {
    

    //得到行

                    Row row = itr.next();                   System.out.println(row.getLastCellNum());                   
    

    // Iterating over each column of Excel file
    // 基于行创建单元格 迭代器

                    Iterator<Cell> cellIterator = row.cellIterator();
                    while (cellIterator.hasNext()) {
    

    //依次 获取某行的单元格

                        Cell cell = cellIterator.next();
    
                        switch (cell.getCellType()) {
    

    //下面是依据不同数据类型 打印出单元格的 数据

                            case Cell.CELL_TYPE_STRING:
                            System.out.print(cell.getStringCellValue() + "	");
                            break;
                        case Cell.CELL_TYPE_NUMERIC:
                            if(DateUtil.isCellDateFormatted(cell)){
                                System.out.print(cell.getDateCellValue() + "	");
                            }else{
                                System.out.print(cell.getNumericCellValue() + "	");
                            }
    
                            break;
                        case Cell.CELL_TYPE_BOOLEAN:
                            System.out.print(cell.getBooleanCellValue() + "	");
                            break;
                        default:
    
                        }
                    }
                    System.out.println("");
                }
            }catch(Exception ex){
                ex.printStackTrace();
            }
        }
    }
    
  • 相关阅读:
    在Linux下修改图形界面的分辨率
    Linux 常用命令
    读ActiveAndroid源码(三)
    读ActiveAndroid源码(二)
    读ActiveAndroid源码(一)
    Android中图片的文件储存
    构造器内部的多态方法的行为
    摸触事件分发的小实践——关闭View滑动
    【Java】 关于类的初始化
    git配置http代理
  • 原文地址:https://www.cnblogs.com/pacoson/p/4893163.html
Copyright © 2011-2022 走看看