我需要讀取Excel文件並顯示其內容。 我有一個支持bean代碼,它讀取一個特定的Excel文件並在控制檯中顯示它的內容。我必須在文本編輯器中顯示內容。使用PrimeFaces我得到了<p:fileUpload>
和<p:editor>
。使用JSF讀取Excel值
這裏的豆:
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的文本中值編輯。
非常感謝您的幫助。我將進行必要的更改,並將學習與基本Java相關的東西。但是我仍然非常好奇,並且很想知道如何使用JSF標籤來顯示它。這會增加我對JSF的興趣。因此,爲此獲得解決方案對我來說將非常重要,這樣我就可以知道如何在JSF代碼中使用它。 – ramakrishnan