【问题标题】:Read from Excel (multiple cells) using Java使用 Java 从 Excel(多个单元格)读取
【发布时间】:2018-07-11 22:43:55
【问题描述】:

我有一个 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());
        }
    }
}

【问题讨论】:

    标签: java excel


    【解决方案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());
        }
    }
    

    编辑:(基于下面的 cmets):

    当你想打印整行时,除了密码,使用这个:

    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 
    }
    

    【讨论】:

    • 感谢您的信息!但我希望我的答案是这样的:第二行,选定的单元格(0,1,3)。输出应为 James | 1002 | 1980 年 24 月 6 日。
    • ok @LaLa 我现在明白了,因为在你的问题中你说你想提供单元格号和行号,那么你会得到指定的单元格值,如果你想做你在您的评论中提到,只需遍历给定的行,然后跳过单元格编号 2,我将根据您的评论编辑我的答案。
    • 谢谢@мυѕτавєւмo,能给我你的邮箱吗?我想向您发送我的尝试,我使用 GUI 实现它,但它有一些错误。
    猜你喜欢
    • 2016-12-02
    • 2013-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多