我有一個從Excel文檔中提取數據的java代碼。 我想計算列數和總行數(在特定列中)。我怎樣才能做到這一點?下面提供了Java代碼和期望的o/p。如何使用Java計算Excel文檔的列中的行數
(編輯):我應該做些什麼修改以獲得所需的o/p。我應該寫一個循環來獲得行和列的數量或有做同樣的
期望中的O/P
ColumnA ColumnB ColumnC
Vinayak James Dan
India US Denmark
Total number of Columns: 3
number of data in ColumnA:2
number of data in ColumnB:2
number of data in ColumnC:2
(編輯)的方法: - 已回答這裏 - Count number of rows in a column of Excel sheet(Java code provided)
我的Java代碼:
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.formula.functions.Column;
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;
public class ExcelRead {
public static void main(String[] args) {
int count=0;
try {
FileInputStream file = new FileInputStream(new File("C:/Users/vinayakp/Desktop/Book.xlsx"));
XSSFWorkbook workbook = new XSSFWorkbook(file);
XSSFSheet sheet = workbook.getSheetAt(0);
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_BOOLEAN:
System.out.print(cell.getBooleanCellValue() + "\t\t");
break;
case Cell.CELL_TYPE_NUMERIC:
System.out.print(cell.getNumericCellValue() + "\t\t");
break;
case Cell.CELL_TYPE_STRING:
System.out.print(cell.getStringCellValue() + "\t\t");
break;
}
}
System.out.println("");
}
file.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException ae) {
ae.printStackTrace();
}
}
}
輸出我得到的是:
ColumnA ColumnB ColumnC
Vinayak James Dan
India US Denmark
我需要得到所需的o/p,如上所示。 代碼正常工作但是我需要獲得計數列和行的值。請爲我提供相同的解決方案。我之前在代碼中已經解決了這個問題:Issue while reading Excel document (Java code)
這幫助我獲得總列數..感謝:)我通過在我的工作表中添加更多的列進行驗證,它工作。現在我需要知道給定列中行數的_count_ –
我應該使用'getPhysicalNumberOfRows'並迭代每列以查找每行數據的總計數? –
不,我不這麼認爲,因爲「返回物理定義的行數(不是表中的行數)」(http://poi.apache.org/apidocs/org/apache/poi/ hssf/usermodel/HSSFSheet.html#getPhysicalNumberOfRows%28)29) – acostache