Cell cell = sheet.getRow(i).getCell(j);
时间: 2024-05-23 07:16:19 浏览: 14
这段代码的作用是获取 Excel 表格中第 i 行第 j 列的单元格对象。首先通过 `sheet.getRow(i)` 获取第 i 行的行对象,然后通过 `.getCell(j)` 获取该行中第 j 列的单元格对象。最后将该单元格对象赋值给 `cell` 变量。这样就可以通过 `cell` 对象来获取单元格中的值或者对单元格进行修改操作。
相关问题
public static void main(String[] args) throws IOException { Workbook workbook = WorkbookFactory.create(new File("lib/1.xlsx")); Sheet sheet = workbook.getSheetAt(0); int rowCount = sheet.getLastRowNum() + 1; int colCount = sheet.getRow(0).getLastCellNum(); int cellWidth = 100 / colCount; int cellHeight = 100 / rowCount; StringBuilder html = new StringBuilder(); html.append("<table>"); for (int i = 0; i < rowCount; i++) { html.append("<tr>"); for (int j = 0; j < colCount; j++) { Cell cell = sheet.getRow(i).getCell(j); String value = cell.getStringCellValue(); html.append("<td style="width: " + cellWidth + "%; height: " + cellHeight + "%;">"); html.append(value); html.append("</td>"); } html.append("</tr>"); } html.append("</table>"); FileOutputStream fos = new FileOutputStream("lib/output.html"); fos.write(html.toString().getBytes()); fos.close();提示错误Exception in thread "main" java.lang.NullPointerException at a.b.main(b.java:26)第26行是Cell cell = sheet.getRow(i).getCell(j);
这个错误通常是因为在获取行或单元格时,没有检查它们是否为 null 引用。你可以尝试在访问行或单元格之前先检查它们是否为 null,如果是 null,则可能是因为该行或单元格为空。以下是一个示例代码片段,可以用于检查并避免 NullPointerException 错误:
```
for (int i = 0; i < rowCount; i++) {
Row row = sheet.getRow(i);
if (row != null) {
html.append("<tr>");
for (int j = 0; j < colCount; j++) {
Cell cell = row.getCell(j);
if (cell != null) {
String value = cell.getStringCellValue();
html.append("<td style='width: " + cellWidth + "%; height: " + cellHeight + "%;'>");
html.append(value);
html.append("</td>");
}
}
html.append("</tr>");
}
}
```
在这个示例代码中,我们在访问每一行和单元格之前,都先检查了它们是否为 null 引用,如果是 null,则跳过该行或单元格。这样可以避免 NullPointerException 错误。
解释一下下面这段代码@Override public List<List<Object>> getBankListExcel(InputStream in, String fileName) throws Exception { List<List<Object>> list = new ArrayList<>(); Workbook work = this.getWorkbook(in, fileName); if (null == work) { throw new Exception("创建Excel工作簿为空"); } Sheet sheet = null; Row row = null; Cell cell = null; for( int i = 0;i<work.getNumberOfSheets();i++) { sheet = (Sheet) work.getSheetAt(i); //sheet.getSheetName(); if (sheet == null) { continue; } for (int j = sheet.getFirstRowNum(); j <= sheet.getLastRowNum(); j++) { row = sheet.getRow(j); if (row == null) { continue; } List<Object> li = new ArrayList<>(); for (int y = row.getFirstCellNum(); y < row.getLastCellNum(); y++) { cell = row.getCell(y); li.add(cell); } list.add(li); } } return list; }
这段代码是一个方法,用于从Excel文件中读取数据并返回一个包含数据的二维列表。下面是对代码的逐行解释:
1. `@Override`:这是一个注解,表示该方法是对父类或接口中同名方法的重写。
2. `public List<List<Object>> getBankListExcel(InputStream in, String fileName) throws Exception`:这是方法的定义,参数包括一个输入流和文件名,返回一个二维列表。
3. `List<List<Object>> list = new ArrayList<>();`:创建一个空的二维列表,用于存储读取到的数据。
4. `Workbook work = this.getWorkbook(in, fileName);`:调用一个名为`getWorkbook`的方法,传入输入流和文件名,返回一个`Workbook`对象,表示Excel工作簿。
5. `if (null == work) { throw new Exception("创建Excel工作簿为空"); }`:如果`work`为空,则抛出异常,表示无法创建Excel工作簿。
6. `Sheet sheet = null; Row row = null; Cell cell = null;`:创建变量`sheet`、`row`和`cell`,用于引用工作表、行和单元格对象。
7. `for( int i = 0;i<work.getNumberOfSheets();i++) {`:遍历工作簿中的所有工作表。
8. `sheet = (Sheet) work.getSheetAt(i);`:获取第`i`个工作表。
9. `if (sheet == null) { continue; }`:如果工作表为空,跳过当前循环,继续下一次循环。
10. `for (int j = sheet.getFirstRowNum(); j <= sheet.getLastRowNum(); j++) {`:遍历工作表中的所有行。
11. `row = sheet.getRow(j);`:获取第`j`行。
12. `if (row == null) { continue; }`:如果行为空,跳过当前循环,继续下一次循环。
13. `List<Object> li = new ArrayList<>();`:创建一个空的列表,用于存储当前行的数据。
14. `for (int y = row.getFirstCellNum(); y < row.getLastCellNum(); y++) {`:遍历行中的所有单元格。
15. `cell = row.getCell(y);`:获取第`y`个单元格。
16. `li.add(cell);`:将单元格的值添加到当前行的列表中。
17. `list.add(li);`:将当前行的列表添加到最终的二维列表中。
18. `return list;`:返回包含从Excel中读取到的数据的二维列表。
该方法通过遍历每个工作表、每行、每个单元格,将读取到的数据存储在一个二维列表中,并最终返回该列表。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)