如何使用Java来计算Excel文档的列行数 [英] How to calculate number of rows in a column of Excel document using Java
问题描述
我有一个java code从Excel文档中获取数据。 我要计算行和列的总数(在特定列)的数量即可。我怎样才能做到这一点? Java的code和所需的O / P如下
I have a java code which fetches data from excel document. I want to calculate the number of columns and total number of rows(in a particular column). How can I achieve this? Java code and desired o/p is provided below
(编辑):修改什么我应该得到的例如所需的O / P我应该写一个循环来获得行和列的数量,或有做同样的方法
(edit): what modification I should make to get the desired o/p for e.g. I should write a loop to get the count of columns and rows or there is a method to do the same
期望中的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
(编辑): - 回答这里 - <一个href=\"http://stackoverflow.com/questions/13874074/count-number-of-rows-in-a-column-of-excel-sheetjava-$c$c-provided\">Count排在Excel工作表中的列数(Java的code提供)
我的Java code:
My Java Code:
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();
}
}
}
输出我得到的是:
Output I'm getting is:
ColumnA ColumnB ColumnC
Vinayak James Dan
India US Denmark
我需要得到所需的O / P如上图所示。 code是工作的罚款但是我需要得到列和行的计数的值。请给我提供了相同的解决方案。我曾与code早些时候在这个问题解决的问题:的 ISSU在读取Excel文档(JAVA的code)
I need to get the desired o/p as shown above. Code is working fine however I need to get the count values of column and rows. Kindly provide me the solution for the same. I had problems with the code earlier which was resolved in this question: Issu while reading excel document (java code)
推荐答案
我觉得你可以用code建议<一href=\"http://stackoverflow.com/questions/2922692/to-get-columns-from-excel-files-using-apache-poi\">here拿到列,然后每一行中的列(虽然它的多为行中关于POI的做法每一列),仅计算你需要的值。
I think you can use the code suggested here to get the columns and then for each row in a column (though it's more for each column in the row concerning POI's approach), just count the values you need.
所以你的code大概会遵循一些为:
So your code would probably follows something as:
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文档
这篇关于如何使用Java来计算Excel文档的列行数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!