在Excel中查找列中的单元格数

时间:2014-04-10 02:46:59

标签: java jxl

我想从Excel文件中找到列中的内容数。

实施例

Location Device Second
1         A      10
2         B       20
3                 30

现在,我希望它应该显示, 地点:3 装置:2 第二:3

我正在使用jxl。

 Workbook existingWorkbook = Workbook.getWorkbook(new File(filename1));
        WritableWorkbook workbookCopy = Workbook.createWorkbook(new File(filename1), existingWorkbook);
        WritableSheet sheetToEdit = workbookCopy.getSheet(0);

     int n=   sheetToEdit.getcolumns();
     System.out.println(n);

        workbookCopy.write();
        workbookCopy.close();

在这里,我得到的只是列数,

你能告诉我,我怎么能这样做?

1 个答案:

答案 0 :(得分:1)

除了手动计算和确定空单元格之外,我认为没有直接的方法:

Workbook existingWorkbook = Workbook.getWorkbook(new File(filename1));
WritableWorkbook workbookCopy = Workbook.createWorkbook(new File(filename1), existingWorkbook);
WritableSheet sheetToEdit = workbookCopy.getSheet(0);

for (int i=0; i<sheetToEdit.getcolumns(); i++) {
    int columnCount = 0;
    for (Cell cell : sheetToEdit.getColumn(i))
        if (!cell.getContents().equals(""))
            columnCount++;
    System.out.println(columnCount);
}

workbookCopy.write();
workbookCopy.close();