java - 如何使用Java计算Excel文档的一列中的行数

标签 java apache-poi

我有一个java代码,它从Excel文档中获取数据。 我想计算列数和总行数(在特定列中)。我怎样才能实现这个目标?下面提供了 Java 代码和所需的 o/p

(编辑):我应该进行哪些修改才能获得所需的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,如上所示。 代码工作正常,但是我需要获取列和行的count值。请为我提供相同的解决方案。我之前的代码遇到了问题,在这个问题中得到了解决:Issue while reading Excel document (Java code)

最佳答案

我认为您可以使用建议的代码 here获取列,然后对于列中的每一行(尽管与 POI 的方法有关的行中的每一列更多),只需计算您需要的值即可。

所以你的代码可能会遵循以下内容:

for(Row row : sheet) {
   short minColIx = row.getFirstCellNum();
   short maxColIx = row.getLastCellNum();
   for(short colIx = minColIx; colIx<maxColIx; colIx++) {
     Cell c = row.getCell(colIx);
     if(c != null) {
        if(c.getCellType() == Cell.CELL_TYPE_NUMERIC) {
           // add c.getNumericCellValue()
        }
     }
   }
}

来自 poi api docs 的好主意用于处理列号。

关于java - 如何使用Java计算Excel文档的一列中的行数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13858846/

相关文章:

java - 如何避免 javax.persistence.EntityExistsException : different object with the same identifier?

java - 如何在 PathMatchingResourcePatternResolver 上应用文件名模式?

javax.swing.text.AbstractDocument$LeafElement 包含无效的 p0/p1 值

java - 我可以使用从 Excel 行读取的不同变量集循环遍历我的程序吗?

java - 如何使用 POI 在 Excel 工作表中设置打印标题行?

java - Apache POI 需要很长时间才能生成 excel 表

java - 如何在 spring-boot 应用程序中为 Apache Tomcat 中的 AccessLogValve 设置属性 requestAttributesEnabled?

java - 系列 setTitle 的 apache-poi 4.0 NullPointer

java - 如何使用java编写基于Excel的csv文件?

java - Android无法将搜索按钮添加到选项菜单