使用JSF读取Excel值

时间:2013-06-26 16:15:43

标签: jsf jsf-2

我需要阅读Excel文件并显示其内容。 我有一个支持bean代码,它读取特定的Excel文件并在控制台中显示其内容。我必须在文本编辑器中显示内容。使用PrimeFaces我得到了<p:fileUpload><p:editor>

这是豆子:

public void handleFileUpload(FileUploadEvent event) {
        FacesMessage msg = new FacesMessage("Succesful", event.getFile().getFileName() + " is uploaded.");
        FacesContext.getCurrentInstance().addMessage(null, msg);
    }
    public String convertjava(String b) {
    try 
    {
    FileInputStream file = new FileInputStream(new File(""));
            // 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.rowIterator();
            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:
                        cell.getBooleanCellValue();
                        break;
                    case Cell.CELL_TYPE_NUMERIC:
                        cell.getNumericCellValue();
                        break;
                    case Cell.CELL_TYPE_STRING:
                        cell.getStringCellValue();
                        break;
                    }
                }
                System.out.println("");
            }
            file.close();
            FileOutputStream out = new FileOutputStream(new File(""));
            workbook.write(out);
            out.close();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            System.out.println("error");
        }
        }

这是Facelet:

<h:body>

    <h:form enctype="multipart/form-data">

        <p:fileUpload fileUploadListener="#{uploadBean.handleFileUpload}"
            mode="advanced" update="display" auto="true" sizeLimit="10000000"
            allowTypes="/(\.|\/)(xls|xlsx)$/" />

<p:growl id="display" showDetail="true" />
                        <h:commandButton action="#{uploadBean.convertjava}"
                value="Excel Report" />
        </h:form>
        <br />

    </h:body>

这里我完全空白如何通过JSF标记调用convertJava()方法并在文本编辑器中显示读取的excel值。

1 个答案:

答案 0 :(得分:5)

您应该在文件上传监听器方法中获取上传的文件。你现在无处可去。你基本上完全忽略了上传的文件。这确实没有意义。

将其指定为(视图作用域)bean的属性,如下所示:

private UploadedFile uploadedFile;

public void handleFileUpload(FileUploadEvent event) {
    uploadedFile = event.getFile();
    // ...
}

然后将其InputStream提供给HSSFWorkbook构造函数。您当前正在尝试读取不存在的文件。这确实没有意义。您应该阅读上传的文件。替换无意义的

FileInputStream file = new FileInputStream(new File(""));
HSSFWorkbook workbook = new HSSFWorkbook(file);
// ...

通过

InputStream input = uploadedFile.getInputStream();
HSSFWorkbook workbook = new HSSFWorkbook(input);
// ...

顺便说一句,在Excel单元格的循环内部,您也无法将找到的数据分配给属性/变量。总而言之,您的根本错误似乎是您不断忽略提供给您的值,而不是将它们分配给变量以供以后重用/重新显示。反过来,这个问题与JSF并不完全相关,而只与基本Java有关。因此,我建议暂停JSF,并努力学习基本的Java。