使用Java从Excel(多个单元格)读取

时间:2018-07-11 10:14:52

标签: java excel

我有一个Excel工作表,并且使用 Apache POI 库读取该工作表,但是我希望用户选择哪一行和(多个选定的单元格)来显示其数据。 例如:

Name | ID | Password | date

john | 1001 | 1234 | 2-9-1997

James | 1002 |4567 | 24-6-1980

Liza | 1003 | 7890 | 18 -11-1990

我想读取第一行,但只读取名称,ID和数据单元格,而不是全部行。我该怎么做?

这是我的尝试:

package javaapplication;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Scanner;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;


public class JavaApplication {
    public static void main(String[] args)  {
        try {
            // Specify the path of file
            File path=new File("/Users/monasaad1/Desktop/emp.xlsx");

            // load file
            FileInputStream file=new FileInputStream(path);

            // load workbook
            XSSFWorkbook workbook=new XSSFWorkbook(file);

            // Load sheet- Here we are loading first sheetonly
            XSSFSheet sheet1= workbook.getSheetAt(0);
            int  rowCounter = sheet1.getLastRowNum();

            // user input row & cell
            Scanner input = new Scanner(System.in);
            System.out.print("Please choose row");
            int choosenRow = input.nextInt();
            System.out.print("Please choose cell");
            int choosenCell = input.nextInt();

            for (int i=0; i<rowCounter+1; i++){
                String data =sheet1.getRow(choosenRow).getCell(choosenCell).getStringCellValue();
                System.out.println("Data from Row"+i+" is "+data);
            }
            } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }
}

1 个答案:

答案 0 :(得分:1)

首先,您不必遍历单元格和行,知道如果选择一行和一个单元格,则期望一个值。其次,您必须在执行getStringCellValue()之前验证单元格的类型,因为如果该值不是String,则可能会发生异常。

这是我想出的一种尝试,希望对您有所帮助。

 public static void main(String[] args) {
    try {
        // Specify the path of file
        File path=new File("/Users/monasaad1/Desktop/emp.xlsx");

        // load file
        FileInputStream file = new FileInputStream(path);

        // load workbook
        XSSFWorkbook workbook = new XSSFWorkbook(file);
        // Load sheet- Here we are loading first sheetonly
        XSSFSheet sheet1 = workbook.getSheetAt(0);
        // user input row & cell
        Scanner input = new Scanner(System.in);
        System.out.print("Please choose row");
        int choosenRow = input.nextInt();
        System.out.print("Please choose cell");
        int choosenCell = input.nextInt();

        String data = sheet1.getRow(choosenRow).getCell(choosenCell).toString();// may return null if the choosen row and/or cell goes beyond
        // the rows count and/or the cells count
        System.out.println(String.format("Data from Row %d and cell %d is %s", choosenRow, choosenCell, data));


    } catch (Exception e) {
        System.out.println(e.getMessage());
    }
}

编辑:(基于以下评论):

要打印除密码外的整个行,请使用以下命令:

public static void main(String[] args) {
//.... Skipped identical lines as above 
        System.out.print("Please choose row");
        int chosenRow = input.nextInt();

        StringBuilder stringBuilder = new StringBuilder();
        XSSFRow row = sheet1.getRow(chosenRow);
        int i = 0;
        for (; i < row.getPhysicalNumberOfCells() - 1; i++) {
            if (i == 2) {
                continue;// skipping the password cell
            }
            stringBuilder.append(row.getCell(i))
                    .append(" | ");
        }
        stringBuilder.append(row.getCell(i));

        String data = stringBuilder.toString();

        System.out.println(String.format("Data from Row %d are :%s", chosenRow, data));
  //.... Skipped identical lines as above 
}