Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/29.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Excel工作表中的迭代错误_Java_Excel_Iteration - Fatal编程技术网

Java Excel工作表中的迭代错误

Java Excel工作表中的迭代错误,java,excel,iteration,Java,Excel,Iteration,当我试图通过Java环境使用Excel工作表从项目列表中选择项目时,只选择并执行第一个项目。下一组迭代没有发生 int i=1; while (i<=sheet.getLastRowNum() ) { row = sheet.getRow(i); String w = row.getCell(0).getStringCellValue(); // Processing : logic on "items" from excel i++; }

当我试图通过Java环境使用Excel工作表从项目列表中选择项目时,只选择并执行第一个项目。下一组迭代没有发生

int i=1;

while (i<=sheet.getLastRowNum()  )  
{

    row = sheet.getRow(i);

    String w = row.getCell(0).getStringCellValue();
    //  Processing : logic on "items" from excel 

    i++;
}
inti=1;

虽然(iOK),但在编写代码之前,请先尝试此示例。检查访问的每一列和每一行。也许更好的方法是使用迭代器

try {

    FileInputStream file = new FileInputStream(new File("C:\\test.xls"));

    //Get the workbook instance for XLS file 
    HSSFWorkbook workbook = new HSSFWorkbook(file);

    //Get first sheet from the workbook
    HSSFSheet sheet = workbook.getSheetAt(0);

    //Iterate through each rows from first sheet
    Iterator<Row> rowIterator = sheet.iterator();
    while(rowIterator.hasNext()) {
        Row row = rowIterator.next();

        //For each row, iterate through each columns
        Iterator<Cell> cellIterator = row.cellIterator();
        while(cellIterator.hasNext()) {

            Cell cell = cellIterator.next();

            switch(cell.getCellType()) {
                case Cell.CELL_TYPE_BOOLEAN:
                    System.out.print(cell.getBooleanCellValue() + "\t\t");
                    break;
                case Cell.CELL_TYPE_NUMERIC:
                    System.out.print(cell.getNumericCellValue() + "\t\t");
                    break;
                case Cell.CELL_TYPE_STRING:
                    System.out.print(cell.getStringCellValue() + "\t\t");
                    break;
            }
        }
        System.out.println("");
    }
    file.close();
    FileOutputStream out = 
        new FileOutputStream(new File("C:\\test.xls"));
    workbook.write(out);
    out.close();

} catch (FileNotFoundException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}
试试看{
FileInputStream文件=新的FileInputStream(新文件(“C:\\test.xls”);
//获取XLS文件的工作簿实例
HSSF工作簿=新的HSSF工作簿(文件);
//从工作簿中获取第一张工作表
HSSFSheet sheet=工作簿。getSheetAt(0);
//从第一张图纸开始遍历每行
迭代器rowIterator=sheet.Iterator();
while(roweiterator.hasNext()){
行=行迭代器。下一步();
//对于每一行,遍历每一列
迭代器cellIterator=row.cellIterator();
while(cellIterator.hasNext()){
Cell=cellIterator.next();
开关(cell.getCellType()){
case Cell.Cell\u类型\u布尔值:
System.out.print(cell.getBooleanCellValue()+“\t\t”);
打破
case Cell.Cell\u类型\u数值:
System.out.print(cell.getNumericCellValue()+“\t\t”);
打破
case Cell.Cell\u类型\u字符串:
System.out.print(cell.getStringCellValue()+“\t\t”);
打破
}
}
System.out.println(“”);
}
file.close();
FileOutputStream out=
新文件输出流(新文件(“C:\\test.xls”);
练习册。写(出);
out.close();
}catch(filenotfounde异常){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}

您是在谈论行的下一列吗?因为您迭代了一行列。由于这是一个矩阵,您还应该为列使用另一个内部循环。这可能是sheet.getRow(i)。getLastColumnNum()单元格始终为(零),而行正在更改。b) 应用逻辑任务从excel中的列中检索一个元素。c) 从excel中的列中检索第二个元素,应用逻辑任务等等…您使用哪个库从excel中读取?您是否检查了lastRowNum的值?a)ApachePOI b)它会自动在空单元格处停止,并获取表列或打印数据中存储数据的计数。c) 但是,即使我将最后一行编号作为条件,它也不会从excel检索数据我认为您正在从excel工作表中获取值,并形成另一张excel工作表,将数据传输到其他工作表…并打印它们。没关系,代码的重要部分是从行和单元格中读取。a)读取正在进行,这不是问题所在。b) 读取每行(i)单元格(0)逻辑任务将应用于excel工作表中的每个字符串值。c) 逻辑任务是从excel中逐个拆分所有字符串值,使用for循环将前缀和后缀存储在D基中。a)单词列表在excel中…所以行(i)单元格(o)是excel中的一列。b) 现在excel中有一个列表,只有一列。。。c) 再次读取每个单元格应用逻辑任务读取另一个单元格应用逻辑任务。。。d) 意思是说行始终在更改单元格(0)。您的问题是什么?我又读了一遍你的问题,你说你不能通过第二行的第一列。这不是你的问题吗?