[英]Trying to retrieve data from excel sheet using apache POI
我是 Java 編程的新手,我正在編寫一種方法來使用 apache POI 從 excel 工作表中檢索所有行。 我創建了一個名為“ExcelDataProvider”的單獨類,我在其中調用了從工作表中提取值的方法。 我編寫的方法對第一行工作正常,但沒有處理所有行。 這是我寫的代碼...
public static String getCellDataNew(String sheetName, String colName, int rowNum) {
try {
int col_Num = 0;
ExcelWSheet = ExcelWBook.getSheet(sheetName);
Row = ExcelWSheet.getRow(0);
int rowcount = ExcelWSheet.getLastRowNum();
for (int i = 0; i < Row.getLastCellNum(); i++) {
if (Row.getCell(i).getStringCellValue().trim().equals(colName.trim()))
col_Num = i;
}
for (rowNum = 2; rowNum <= rowcount; rowNum++) {
Row = ExcelWSheet.getRow(rowNum - 1);
Cell = Row.getCell(col_Num);
}
if (Cell.getCellType() == CellType.STRING)
return Cell.getStringCellValue();
else if (Cell.getCellType() == CellType.NUMERIC || Cell.getCellType() == CellType.FORMULA) {
String cellValue = String.valueOf(Cell.getNumericCellValue());
return cellValue;
} else if (Cell.getCellType() == CellType.BLANK)
return "";
else
return String.valueOf(Cell.getBooleanCellValue());
} catch (Exception e) {
e.printStackTrace();
return "row " + rowNum + " or column does not exist in Excel";
}
}
……………………………………………………………………………………………………………………………………………………………… ...
這是來自 dataprovider 類的代碼
package utility;
public class ExcelDataProvider {
private String sDealer;
private String sBranchCode;
private String sBranchName;
private String sAccountType;
private String sProductSelection;
private String sTaxResidence;
private String sJointOwnershipOption;
private String sJointOwnershipType;
private String sJointSignatureOption;
private String sJOSelectionLastName;
private String sJOSelectionFirstName;
public ExcelDataProvider() throws Exception {
this.setsDealer(Excelutil.getCellDataNew("OpenAccount", "Dealer", 2));
this.setsBranchCode(Excelutil.getCellDataNew("OpenAccount", "BranchCode", 2));
this.setsBranchName(Excelutil.getCellDataNew("OpenAccount", "BranchName", 2));
this.setsAccountType(Excelutil.getCellDataNew("OpenAccount", "AccountType", 2));
this.setsProductSelection(Excelutil.getCellDataNew("OpenAccount", "ProductSelection", 2));
this.setsTaxResidence(Excelutil.getCellDataNew("OpenAccount", "TaxResidence", 2));
this.setsJointOwnershipOption(Excelutil.getCellDataNew("OpenAccount", "JointOwnership", 2));
this.setsJointOwnershipType(Excelutil.getCellDataNew("OpenAccount", "JointOwnershipType", 2));
this.setsJointSignatureOption(Excelutil.getCellDataNew("OpenAccount", "JointSignature", 2));
this.setsJOSelectionLastName(Excelutil.getCellDataNew("OpenAccount", "JointOwnerSelectionLastName", 2));
this.setsJOSelectionFirstName(Excelutil.getCellDataNew("OpenAccount", "JointOwnerSelectionFirstName", 2));
}
public String getsDealer() {
return this.sDealer;
}
public String getsBranchCode() {
return this.sBranchCode;
}
public String getsBranchName() {
return this.sBranchName;
}
public String getsAccountType() {
return this.sAccountType;
}
public String getsProductSelection() {
return this.sProductSelection;
}
public String getsTaxResidence() {
return sTaxResidence;
}
public String getsJointOwnershipOption() {
return sJointOwnershipOption;
}
public String getsJointOwnershipType() {
return sJointOwnershipType;
}
public String getsJointSignatureOption() {
return sJointSignatureOption;
}
public String getsJOSelectionLastName() {
return sJOSelectionLastName;
}
public String getsJOSelectionFirstName() {
return sJOSelectionFirstName;
}
public void setsDealer(String sDealer) {
this.sDealer = sDealer;
}
public void setsBranchCode(String sBranchCode) {
this.sBranchCode = sBranchCode;
}
public void setsBranchName(String sBranchName) {
this.sBranchName = sBranchName;
}
public void setsAccountType(String sAccountType) {
this.sAccountType = sAccountType;
}
public void setsProductSelection(String sProductSelection) {
this.sProductSelection = sProductSelection;
}
public void setsTaxResidence(String sTaxResidence) {
this.sTaxResidence = sTaxResidence;
}
public void setsJointOwnershipOption(String sJointOwnershipOption) {
this.sJointOwnershipOption = sJointOwnershipOption;
}
public void setsJointOwnershipType(String sJointOwnershipType) {
this.sJointOwnershipType = sJointOwnershipType;
}
public void setsJointSignatureOption(String sJointSignatureOption) {
this.sJointSignatureOption = sJointSignatureOption;
}
public void setsJOSelectionLastName(String sJOSelectionLastName) {
this.sJOSelectionLastName = sJOSelectionLastName;
}
public void setsJOSelectionFirstName(String sJOSelectionFirstName) {
this.sJOSelectionFirstName = sJOSelectionFirstName;
}
}
這就是我的電子表格的樣子..
Testcase| ClientName| Dealer| BranchCode| BranchName| AccountType|ProductSelection| TaxResidence| JointOwnership | JointOwnershipType | JointSignature| JointOwnerSelectionLastName| JointOwnerSelectionFirstName
1 |Test, GR| NBIN Introducing| HOUS| NBIN Intro| Non Registered| Cash | Ontario| Yes| Joint WROS| Yes| Test| ABC
2 |Test, GR| NBIN Introducing| HOUS| NBIN Intro| Non Registered| Cash | Ontario| Yes| Joint WROS| Yes| Test| ABC
我想我看到了這個問題。 下面的 for 循環不應該在那里結束。 它也應該覆蓋下面的 if-else 階梯。
for (rowNum = 2; rowNum <= rowcount; rowNum++) {
Row = ExcelWSheet.getRow(rowNum - 1);
Cell = Row.getCell(col_Num);
}
它應該是:
for (rowNum = 2; rowNum <= rowcount; rowNum++) {
Row = ExcelWSheet.getRow(rowNum - 1);
Cell = Row.getCell(col_Num);
if (Cell.getCellType() == CellType.STRING)
return Cell.getStringCellValue();
else if (Cell.getCellType() == CellType.NUMERIC || Cell.getCellType() == CellType.FORMULA) {
String cellValue = String.valueOf(Cell.getNumericCellValue());
return cellValue;
} else if (Cell.getCellType() == CellType.BLANK)
return "";
else
return String.valueOf(Cell.getBooleanCellValue());
}
您可以使用以下代碼使用 Apache POI 庫從 Excel 工作表中讀取所有行。 我就是直接在main方法里寫的。 請相應地更改參數/路徑。
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Iterator;
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;
/**
* Sample Java program to read Excel file in Java using Apache POI
*
*/
public class ReadAllExcelRow {
public static void main(String[] args) {
try {
File excel = new File("C://temp/test.xlsx");
FileInputStream fis = new FileInputStream(excel);
XSSFWorkbook book = new XSSFWorkbook(fis);
XSSFSheet sheet = book.getSheet("Sheet1");
Iterator<Row> rowIterator = sheet.iterator();
while (rowIterator.hasNext()) {
Row row = rowIterator.next();
Iterator<Cell> cellIterator = row.cellIterator();
while (cellIterator.hasNext()) {
Cell cell = cellIterator.next();
switch (cell.getCellType()) {
case Cell.CELL_TYPE_NUMERIC:
System.out.print(cell.getNumericCellValue() + "\t");
break;
case Cell.CELL_TYPE_STRING:
System.out.print(cell.getStringCellValue() + "\t");
break;
}
}
System.out.println("");
}
book.close();
} catch (FileNotFoundException fe) {
fe.printStackTrace();
} catch (IOException ie) {
ie.printStackTrace();
}
}
}
第一個可能的解決方案:
for (int rownum = 1 ; rownum < sheet.rowcount; ++rownum) {
/* some code */
}
第二:你可以嘗試使用 sheet.rowIterator
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.