java - 用 XSSFRow 填充组合框
问题描述
我想用从 Excel 表中获取的行号填充组合框,问题是我无法循环行,因为 combobox.addItem() 只接受字符串。还有什么其他解决方案可以解决这个问题?我得到的错误:
XSSFRow cannot be converted to String
我的代码:
int lastRow = sheets.getLastRowNum();
for (int i=0;i<lastRow;i++){
XSSFRow row = sheets.getRow(i);
chooseRowComboBox.addItem(row);
}
解决方案
你不能得到整行。我想你想得到填充行的行号。因此,您应该尝试以下方法:
public int getNonBlankRowCount(String path) throws IOException{
File excel = new File(path);
FileInputStream fis = new FileInputStream(excel);
XSSFWorkbook book = new XSSFWorkbook(fis);
XSSFSheet sheet = book.getSheetAt(1);
int rowCount = 0;
int index = book.getSheetIndex(sheet);
if(index==-1){
rowCount=-1;
return rowCount;
}
else{
sheet=book.getSheetAt(index);
Iterator<Row> rowIterator = sheet.rowIterator();
rowCount=0;
while(rowIterator.hasNext()){
Row row = (Row) rowIterator.next();
XSSFCell cell =(XSSFCell) row.getCell(0, Row.RETURN_BLANK_AS_NULL);
if(cell == null){
break;
}
rowCount++;
}
return rowCount;
}
}
此方法将计算您填充的行。现在您只需要将返回值打印到您的组合框中,例如:
for(int i=0; i<getNonBlankRowCount("path_to_ur_excel"); i++){
chooseRowComboBox.addItem(Integer.toString(i)
}
如果你想获取所有行,你只需迭代 throw all rows 或设置 0 < i < 1048577 (最大行数)
推荐阅读
- html - 使用其他 HTML 编辑 HTML
- reactjs - 有没有办法在 redux 中重用 reducer 逻辑?
- android - 如何以编程方式在 Oreo 中创建 wifihotspot?
- android - 回收站视图中的动态网格布局
- php - 根据 td 文本禁用表格中的按钮
- crc - 通过模2二进制除法获得的余数与正常十进制除法获得的余数之间是否有任何关系?
- html - 无法将动态组件附加到动态锚点
- python - Python,Tkinter如何在按下时给optionmenu自己的窗口
- angular - Angular 5,请告诉我 ngx 在 FormlyFieldConfig 中正式支持的类型列表
- asp.net - 不支持该请求。在 ASP.net 中加密图像