我需要读取一个 Excel 文件并显示其内容。我有一个支持 bean 代码,它读取特定的 Excel 文件并在控制台中显示其内容。我必须在文本编辑器中显示内容。使用 PrimeFaces 我得到了<p:fileUpload>
and <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>
在这里,我对如何convertJava()
通过 JSF 标记调用该方法并在文本编辑器中显示读取的 excel 值一无所知。