java excel 列数_计算Excel工作表列中的行数(提供的Java代码)

在参考我之前的问题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 rowIterator = sheet.iterator();

while(rowIterator.hasNext()) {

Row row = rowIterator.next();

Iterator 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

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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值