java 如何将excel表中的单个单元格读取到java程序中,

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27521209/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 11:56:21  来源:igfitidea点击:

how to read a single cell from excel sheet into java program,

javaexcelapache-poi

提问by sandy

I need to read a single cell, (example A1) into my java program. I am able to read out whole content into my program but how can i read only a specific cell?

我需要将单个单元格(例如 A1)读入我的 java 程序。我可以将整个内容读出到我的程序中,但如何只读取特定单元格?

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 JavaApplication2
{
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("C://users/admin/Desktop/Content.xls");

    readXls(inputFile);

}
}

this is what I have done, I am reading the whole content but I need a specific cell

这就是我所做的,我正在阅读整个内容,但我需要一个特定的单元格

回答by Ye Win

You can use row.getCell() method.
Try this:

您可以使用 row.getCell() 方法。
试试这个:

Workbook workbook = WorkbookFactory.create(yourFile.getInputstream());

 Sheet sheet = workbook.getSheet(0);//1,2,3

 Iterator<Row> rowIterator = sheet.iterator();

        while (rowIterator.hasNext()) {

            Row row = rowIterator.next();

            row.getCell(0);
            row.getCell(1);
}