Java POI:如何查找具有字符串值的 Excel 单元格并获取其位置(行)以使用该位置查找另一个单元格
我正在电子表格中查找包含字符串“Total”的单元格,然后使用该单元格所在的行来查找另一个单元格中的总值,该单元格始终是相同的单元格/列(0 中的第 10 个单元格)为基础的索引)。
我有以下代码,没有错误(语法),但 findCell 方法不返回 rowNum 值:
public static void main(String[] args) throws IOException{
String fileName = "C:\\file-path\\report.xls";
String cellContent = "Total";
int rownr=0, colnr = 10;
InputStream input = new FileInputStream(fileName);
HSSFWorkbook wb = new HSSFWorkbook(input);
HSSFSheet sheet = wb.getSheetAt(0);
rownr = findRow(sheet, cellContent);
output(sheet, rownr, colnr);
finish();
}
private static void output(HSSFSheet sheet, int rownr, int colnr) {
/*
* This method displays the total value of the month
*/
HSSFRow row = sheet.getRow(rownr);
HSSFCell cell = row.getCell(colnr);
System.out.println("Your total is: " + cell);
}
private static int findRow(HSSFSheet sheet, String cellContent){
/*
* This is the method to find the row number
*/
int rowNum = 0;
for(Row row : sheet) {
for(Cell cell : row) {
while(cell.getCellType() == Cell.CELL_TYPE_STRING){
if(cell.getRichStringCellValue().getString () == cellContent);{
rowNum = row.getRowNum();
return rowNum;
}
}
}
}
return rowNum;
}
private static void finish() {
System.exit(0);
}
}
I'm looking for a cell in a spreadsheet that has the string 'Total' and then use the row in which that cell is to find the total value in another cell which is always the same cell/column (the 10th cell in a 0 based index).
I have the following code, which has no errors (syntax), but the findCell method is not returning rowNum value:
public static void main(String[] args) throws IOException{
String fileName = "C:\\file-path\\report.xls";
String cellContent = "Total";
int rownr=0, colnr = 10;
InputStream input = new FileInputStream(fileName);
HSSFWorkbook wb = new HSSFWorkbook(input);
HSSFSheet sheet = wb.getSheetAt(0);
rownr = findRow(sheet, cellContent);
output(sheet, rownr, colnr);
finish();
}
private static void output(HSSFSheet sheet, int rownr, int colnr) {
/*
* This method displays the total value of the month
*/
HSSFRow row = sheet.getRow(rownr);
HSSFCell cell = row.getCell(colnr);
System.out.println("Your total is: " + cell);
}
private static int findRow(HSSFSheet sheet, String cellContent){
/*
* This is the method to find the row number
*/
int rowNum = 0;
for(Row row : sheet) {
for(Cell cell : row) {
while(cell.getCellType() == Cell.CELL_TYPE_STRING){
if(cell.getRichStringCellValue().getString () == cellContent);{
rowNum = row.getRowNum();
return rowNum;
}
}
}
}
return rowNum;
}
private static void finish() {
System.exit(0);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
此方法修复可以解决您的问题:
请记住,您的
colnr
仍然是固定值。This method fix is the solution to your problem:
Keep in mind that your
colnr
is still a fixed value.您的
if
语句后面有一个分号,这意味着您的if
将不起作用:即使这不能解决您的问题,我认为您的
while 语句可能不适合这里;
据我记得,POI 中还有其他 Cell 类型。尝试在这些行上放置断点并检查它们是否具有正确的 CellType。
You have a semicolon after your
if
statement which means yourif
won't work:Even if this won't resolve your problem, I think your
while
statement may not be proper for here;As far as I remember, there are other Cell types in POI. Try to put a breakpoint on these lines and check them if they have correct CellType.