在参考我之前的问题How to calculate number of rows in a column of Excel document using Java时,我能够计算给定表格中的总列数。现在,有一半的工作尚未完成,因为我想计算特定列中的行数。可能的解决方案可能是使用2d数组并存储列索引和总行数或使用地图等。我怎么能实现这一点?这里提供了Java代码。我正在为我的演示文件计算正确的数量(列数)。请根据需要修改/建议更改。
(编辑):我使用hasp map来计算存储列索引作为键,行计数作为值,但它不起作用,可能是应用逻辑错误。好吧,如果我想通过使用哈希映射来实现这一目标,如何在特定列中存储行数(迭代时)作为值
Java代码:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
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.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class ExcelRead {
static int colrange=1000;
public static void main(String[] args) {
HashMap hm=new HashMap();
int count=0;
try {
FileInputStream file = new FileInputStream(new File("C:/Users/vinayakp/Desktop/Demo2.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("");
}
for(Row r:sheet)
{
short minColIx=r.getFirstCellNum();
short maxColIx=r.getLastCellNum();
for(short colIx=minColIx;colIx<maxColIx;colIx++) {
Cell c= r.getCell(colIx);
if(c!=null) {
if(c.getCellType()== Cell.CELL_TYPE_STRING||c.getCellType() == Cell.CELL_TYPE_NUMERIC||c.getCellType() == Cell.CELL_TYPE_BOOLEAN) {
count++; ---// can i use hashcode in here to get the key and value pair? key=column index value=total number of rows in that column
}
}
else break;
}
}
System.out.println("\nTotal Number of columns are:\t"+count);
System.out.println(hm);
file.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException ae) {
ae.printStackTrace();
}
}
}
答案 0 :(得分:6)
试试这个:
private void excelReader() {
String data;
try {
InputStream is = new FileInputStream("Read.xlsx");
Workbook wb = WorkbookFactory.create(is);
Sheet sheet = wb.getSheetAt(0);
Iterator rowIter = sheet.rowIterator();
Row r = (Row)rowIter.next();
short lastCellNum = r.getLastCellNum();
int[] dataCount = new int[lastCellNum];
int col = 0;
rowIter = sheet.rowIterator();
while(rowIter.hasNext()) {
Iterator cellIter = ((Row)rowIter.next()).cellIterator();
while(cellIter.hasNext()) {
Cell cell = (Cell)cellIter.next();
col = cell.getColumnIndex();
dataCount[col] += 1;
DataFormatter df = new DataFormatter();
data = df.formatCellValue(cell);
System.out.println("Data: " + data);
}
}
is.close();
for(int x = 0; x < dataCount.length; x++) {
System.out.println("col " + x + ": " + dataCount[x]);
}
}
catch(Exception e) {
e.printStackTrace();
return;
}
}
我使用以下单元格数据创建了一个xlsx文件:
Col0 Col1 Col2 Col3 Col4
1 a x a q
2 b y s w
3 c z d e
4 d f r
5 e t
y
dataCount数组的内容是:
col 0:6
col 1:6
col 2:4
col 3:5
col 4:7
右侧的数字计算每列的数据单元格数,包括标题行。
如果要排除标题行,只需删除行:
rowIter = sheet.rowIterator();
就在while循环之前。
这是你在找什么?
答案 1 :(得分:1)
这会解决吗?
HSSFSheet mySheet = myWorkBook.getSheetAt(0);
Iterator<Row> rowIter = mySheet.rowIterator();
while (rowIter.hasNext()) {
HSSFRow myRow = (HSSFRow) rowIter.next();
Iterator<Cell> cellIter = myRow.cellIterator();
List<HSSFCell> cellStore = new ArrayList<HSSFCell>();
while (cellIter.hasNext()) {
HSSFCell myCell = (HSSFCell) cellIter.next();
rowCount++ //For myRow
}
}
答案 2 :(得分:0)
它给出了excel表中的数据总数,我的意思是包括标题列名。
根据我的理解,您希望计算列的行数,标题行的EXCEPT除外。
如果是这种情况,您可以使用Binu提供的解决方案。只需修改它就可以跳过列名称所在的行。