[英]Read from Excel (multiple cells) using Java
我有一個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
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.