从单元格值Apache POI获取单元索引

时间:2021-03-13 20:23:40
Environment    Status    Version    PatchNumber
Windows        Live      1.0        2
Unix           Live      2.0        4
Mac            Live      1.3        8

If I have the above shown data in an excel, how do I access the cellNumber of PatchNumber using the text

如果我在excel中有上面显示的数据,我如何使用文本访问PatchNumber的cellNumber

XSSFRow row = (XSSFRow) rows.next();
I would like to access row.getCellNumber("PatchNumber"); //Note this method does not exist in Apache POI.

XSSFRow row =(XSSFRow)rows.next();我想访问row.getCellNumber(“PatchNumber”); //注意Apache POI中不存在此方法。

3 个解决方案

#1


5  

I think I understand what you're after - you want to know which column contains the word "Patch" in it's first row? If so, all you need to do is something like:

我想我明白你在追求的是什么 - 你想知道哪一列包含“Patch”这个词的第一行?如果是这样,您需要做的就是:

Sheet s = wb.getSheetAt(0);
Row r = s.getRow(0);

int patchColumn = -1;
for (int cn=0; cn<r.getLastCellNum(); cn++) {
   Cell c = r.getCell(cn);
   if (c == null || c.getCellType() == Cell.CELL_TYPE_BLANK) {
       // Can't be this cell - it's empty
       continue;
   }
   if (c.getCellType() == Cell.CELL_TYPE_STRING) {
      String text = c.getStringCellValue();
      if ("Patch".equals(text)) {
         patchColumn = cn;
         break;
      }
   }
}
if (patchColumn == -1) {
   throw new Exception("None of the cells in the first row were Patch");
}

Just loop over the cells in the first (header) row, check their value, and make a note of the column you're on when you find your text!

只需遍历第一行(标题)行中的单元格,检查它们的值,并在找到文本时记下您所在的列!

#2


2  

Isn't this what you want?

这不是你想要的吗?

row.getCell(3) 

public XSSFCell getCell(int cellnum) Returns the cell at the given (0 based) index, with the Row.MissingCellPolicy from the parent Workbook.

public XSSFCell getCell(int cellnum)返回给定(基于0)索引处的单元格,其中包含来自父工作簿的Row.MissingCellPolicy。

If you want to access exactly by name - "PatchNumber" you can read header row and save an index of PatchNumber. I think the table model only defines access to cells, but doesn't provide associate access by column names. All rows are equal in model I think :-)

如果要完全按名称访问 - “PatchNumber”,您可以读取标题行并保存PatchNumber的索引。我认为表模型仅定义对单元格的访问,但不提供列名称的​​关联访问。在我认为模型中所有行都相同:-)

To get column index. I would iterate over header row and build a list of column names columns then you can use columns.indexOf("PatchNumber")

获取列索引。我将遍历标题行并构建列名列的列表然后您可以使用columns.indexOf(“PatchNumber”)

http://poi.apache.org/apidocs/org/apache/poi/xssf/usermodel/XSSFRow.html#getCell(int)

http://poi.apache.org/apidocs/org/apache/poi/xssf/usermodel/XSSFRow.html#getCell(int)

#3


0  

public CellAddress searchStringInXslx(String string) throws IOException{
            FileInputStream inputStream = new FileInputStream("Books.xlsx");
            Workbook workbook = new XSSFWorkbook(inputStream);
            Sheet firstSheet = workbook.getSheetAt(0);
            Iterator<Row> iterator = firstSheet.iterator();
            CellAddress columnNumber=null;

            while(iterator.hasNext()){
                 Row nextRow = iterator.next();
                 Iterator<Cell> cellIterator = nextRow.cellIterator();
                 while (cellIterator.hasNext()) {
                     Cell cell = cellIterator.next();
                     if(cell.getCellType()==Cell.CELL_TYPE_STRING){ 
                         String text = cell.getStringCellValue();
                          if (string.equals(text)) {
                             columnNumber=cell.getAddress();
                             break;
                          }
                        }
                     }
            }
            workbook.close();
            return columnNumber;
     }

#1


5  

I think I understand what you're after - you want to know which column contains the word "Patch" in it's first row? If so, all you need to do is something like:

我想我明白你在追求的是什么 - 你想知道哪一列包含“Patch”这个词的第一行?如果是这样,您需要做的就是:

Sheet s = wb.getSheetAt(0);
Row r = s.getRow(0);

int patchColumn = -1;
for (int cn=0; cn<r.getLastCellNum(); cn++) {
   Cell c = r.getCell(cn);
   if (c == null || c.getCellType() == Cell.CELL_TYPE_BLANK) {
       // Can't be this cell - it's empty
       continue;
   }
   if (c.getCellType() == Cell.CELL_TYPE_STRING) {
      String text = c.getStringCellValue();
      if ("Patch".equals(text)) {
         patchColumn = cn;
         break;
      }
   }
}
if (patchColumn == -1) {
   throw new Exception("None of the cells in the first row were Patch");
}

Just loop over the cells in the first (header) row, check their value, and make a note of the column you're on when you find your text!

只需遍历第一行(标题)行中的单元格,检查它们的值,并在找到文本时记下您所在的列!

#2


2  

Isn't this what you want?

这不是你想要的吗?

row.getCell(3) 

public XSSFCell getCell(int cellnum) Returns the cell at the given (0 based) index, with the Row.MissingCellPolicy from the parent Workbook.

public XSSFCell getCell(int cellnum)返回给定(基于0)索引处的单元格,其中包含来自父工作簿的Row.MissingCellPolicy。

If you want to access exactly by name - "PatchNumber" you can read header row and save an index of PatchNumber. I think the table model only defines access to cells, but doesn't provide associate access by column names. All rows are equal in model I think :-)

如果要完全按名称访问 - “PatchNumber”,您可以读取标题行并保存PatchNumber的索引。我认为表模型仅定义对单元格的访问,但不提供列名称的​​关联访问。在我认为模型中所有行都相同:-)

To get column index. I would iterate over header row and build a list of column names columns then you can use columns.indexOf("PatchNumber")

获取列索引。我将遍历标题行并构建列名列的列表然后您可以使用columns.indexOf(“PatchNumber”)

http://poi.apache.org/apidocs/org/apache/poi/xssf/usermodel/XSSFRow.html#getCell(int)

http://poi.apache.org/apidocs/org/apache/poi/xssf/usermodel/XSSFRow.html#getCell(int)

#3


0  

public CellAddress searchStringInXslx(String string) throws IOException{
            FileInputStream inputStream = new FileInputStream("Books.xlsx");
            Workbook workbook = new XSSFWorkbook(inputStream);
            Sheet firstSheet = workbook.getSheetAt(0);
            Iterator<Row> iterator = firstSheet.iterator();
            CellAddress columnNumber=null;

            while(iterator.hasNext()){
                 Row nextRow = iterator.next();
                 Iterator<Cell> cellIterator = nextRow.cellIterator();
                 while (cellIterator.hasNext()) {
                     Cell cell = cellIterator.next();
                     if(cell.getCellType()==Cell.CELL_TYPE_STRING){ 
                         String text = cell.getStringCellValue();
                          if (string.equals(text)) {
                             columnNumber=cell.getAddress();
                             break;
                          }
                        }
                     }
            }
            workbook.close();
            return columnNumber;
     }