【问题标题】:How to append cell (String) value from Excel file to JTextArea?如何将 Excel 文件中的单元格(字符串)值附加到 JTextArea?
【发布时间】:2014-10-19 00:31:44
【问题描述】:

这是我的问题:

我尝试从 Excel 文件的循环中附加单元格的值。我使用这部分代码:

Workbook workbook = new HSSFWorkbook(new FileInputStream(fileChooser.getSelectedFile()));
            Sheet sheet = workbook.getSheetAt(0);

            for(Iterator<Row> rit = sheet.rowIterator(); rit.hasNext();)
            {
                Row row = rit.next();

                for(Iterator<Cell> cit = row.cellIterator(); cit.hasNext();)
                {
                    Cell cell = cit.next();


                    cell.setCellType(Cell.CELL_TYPE_STRING);

                    while(cit.hasNext())
                    {
                        notatnik.append(String.valueOf(cell.getSheet().toString()) + "\n");
                    }

                System.out.print(cell.getStringCellValue() + "\t");         
                }
                System.out.println();
            }

但它只返回值“1”或“org.apache.poi.hssf.usermodel.HSSFSheet@6325a3ee”。在excel中我有例如值: 1 SP25 kp 5 6.5 等

我应该怎么做才能将这个值从 Excel 取回到 JTextArea?

附言。我使用 POI 3.10 库。

【问题讨论】:

    标签: java excel jtextarea


    【解决方案1】:

    你把迭代器弄得有点乱……

    迭代器应该这样使用:

    Iterator<Cell> cit = row.cellIterator(); // you get the iterator ...
    
    while (cit.hasNext()) {
      // let't go cell by cell
      Cell cell = cit.next();
      System.out.println(cell.getStringCellValue());
    }
    

    不知道你为什么要打印出cell.getSheet(),因为这个值总是相同的(你在同一张纸上)......可能你正在寻找cell.getValue()......或类似的东西

    【讨论】:

    • 谢谢,我试试。 Noo 我只使用“System.print.put(...)”来检查这个方法是否有效:)
    【解决方案2】:

    如果有人正在寻找答案(并且会遇到与我相同的问题),这是我程序中代码的一部分:

    JFileChooser fileChooser = new JFileChooser();
    
            if(fileChooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION)
            {
                try
                {
                    Workbook workbook = new HSSFWorkbook(new FileInputStream(fileChooser.getSelectedFile()));
                    Sheet sheet = workbook.getSheetAt(0);
    
                    for(Iterator<Row> rit = sheet.rowIterator(); rit.hasNext();)
                    {
                        Row row = rit.next();
    
                        for(Iterator<Cell> cit = row.cellIterator(); cit.hasNext();)
                        {
                            Cell cell = cit.next();
    
                            cell.setCellType(Cell.CELL_TYPE_STRING);
                            notatnik.append(cell.getStringCellValue() + "\t");
                        }
                        notatnik.append("\n");
                    }
                } 
                catch (FileNotFoundException e1)
                {
                    e1.printStackTrace();
                } 
                catch (IOException e2)
                {
                    e2.printStackTrace();
                }
            }
    

    这部分代码从excel文件中获取所有的值,并将它们粘贴到JTextArea中,名为“notatnik”。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-24
      • 2011-12-25
      • 1970-01-01
      • 1970-01-01
      • 2014-11-18
      • 1970-01-01
      相关资源
      最近更新 更多