zoukankan      html  css  js  c++  java
  • [Training Video

    读取以下两种格式的Excel : *.xls  and *.xlsx

    用Apache POI API来实现,需要用到 HSSF 和 XSSF 的类库

    HSSF is the POI Project's pure Java implementation of the Excel '97(-2007) (.xls) file format.

    XSSF is the POI Project's pure Java implementation of the Excel 2007 OOXML (.xlsx) file format.

    These 4 JARs are needed to read excel:

    将这四个JAR包加入到Java Build Path里面去

    Java code to read Excel :

    package com.file.properties;
    
    import java.io.*;
    import java.util.Iterator;
    
    import org.apache.poi.hssf.usermodel.HSSFSheet;
    import org.apache.poi.hssf.usermodel.HSSFWorkbook;
    import org.apache.poi.ss.usermodel.Cell;
    import org.apache.poi.ss.usermodel.Row;
    import org.apache.poi.xssf.usermodel.XSSFSheet;
    import org.apache.poi.xssf.usermodel.XSSFWorkbook;
    
    class ReadExcel 
    {
    static void readXlsx(File inputFile) 
    {
    try 
    {
            // Get the workbook instance for XLSX file
            XSSFWorkbook wb = new XSSFWorkbook(new FileInputStream(inputFile));
    
            // Get first sheet from the workbook
            XSSFSheet sheet = wb.getSheetAt(0);
    
            Row row;
            Cell cell;
    
            // Iterate through each rows from first sheet
            Iterator<Row> rowIterator = sheet.iterator();
    
            while (rowIterator.hasNext()) 
            {
                    row = rowIterator.next();
    
                    // For each row, iterate through each columns
                    Iterator<Cell> cellIterator = row.cellIterator();
                    
                    while (cellIterator.hasNext()) 
                    {
                    cell = cellIterator.next();
    
                    switch (cell.getCellType()) 
                    {
    
                    case Cell.CELL_TYPE_BOOLEAN:
                            System.out.println(cell.getBooleanCellValue());
                            break;
    
                    case Cell.CELL_TYPE_NUMERIC:
                            System.out.println(cell.getNumericCellValue());
                            break;
    
                    case Cell.CELL_TYPE_STRING:
                            System.out.println(cell.getStringCellValue());
                            break;
    
                    case Cell.CELL_TYPE_BLANK:
                            System.out.println(" ");
                            break;
    
                    default:
                            System.out.println(cell);
    
                    }
                    }
            }
    }
    catch (Exception e) 
    {
            System.err.println("Exception :" + e.getMessage());
    }
    }
    
    static void readXls(File inputFile) 
    {
    try 
    {
            // Get the workbook instance for XLS file
            HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(inputFile));
            // Get first sheet from the workbook
            HSSFSheet sheet = workbook.getSheetAt(0);
            Cell cell;
            Row row;
    
            // Iterate through each rows from first sheet
            Iterator<Row> rowIterator = sheet.iterator();
            
            while (rowIterator.hasNext()) 
            {
                    row = rowIterator.next();
    
                    // For each row, iterate through each columns
                    Iterator<Cell> cellIterator = row.cellIterator();
                    
                    while (cellIterator.hasNext()) 
                    {
                    cell = cellIterator.next();
    
                    switch (cell.getCellType()) 
                    {
    
                    case Cell.CELL_TYPE_BOOLEAN:
                            System.out.println(cell.getBooleanCellValue());
                            break;
    
                    case Cell.CELL_TYPE_NUMERIC:
                            System.out.println(cell.getNumericCellValue());
                            break;
    
                    case Cell.CELL_TYPE_STRING:
                            System.out.println(cell.getStringCellValue());
                            break;
    
                    case Cell.CELL_TYPE_BLANK:
                            System.out.println(" ");
                            break;
    
                    default:
                            System.out.println(cell);
                    }
                    }
            }
    
    } 
    
    catch (FileNotFoundException e) 
    {
            System.err.println("Exception" + e.getMessage());
    }
    catch (IOException e) 
    {
            System.err.println("Exception" + e.getMessage());
    }
    }
    
    public static void main(String[] args) 
    {
            File inputFile = new File("D:\SoapUIStudy\input.xls");
            File inputFile2 = new File("D:\SoapUIStudy\input.xlsx");
            readXls(inputFile);
            readXlsx(inputFile2);
    }
    }
    

     Result :

    User in xls
    Password in xls
    U1
    P1
    U2
    P2
    User in xlsx
    Password in xlsx
    User1
    Password1
    User2
    Password2
    User3
    Password3

  • 相关阅读:
    MPF源码分析之资源文件加载
    oracle存储过程代码日志记录
    fix8源码分析之日志模块
    oracle日期转整数
    记录OCI操作一个诡异的问题
    记录一个虚拟机重启网络启动失败问题
    buff占用内存高
    MFC程序编译链接问题汇总一
    回调函数模型设计
    利用call与apply向函数传递参数
  • 原文地址:https://www.cnblogs.com/MasterMonkInTemple/p/4581357.html
Copyright © 2011-2022 走看看