gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 11:01:42 32 4
gpt4 key购买 nike

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

最佳答案

首先,您不必遍历单元格和行,因为您知道如果您选择行和单元格,则您期望的是单个值。其次,您必须在执行 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
}

关于java - 使用 Java 从 Excel(多个单元格)读取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51282864/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com