[英]How can i view only my sheetname on jsp page
大家好,我是 java 的新手,我剛剛創建了一個用於查看 excel 文件的程序,它運行良好,但現在我想創建一個 jsp 頁面,我有一個表,我想在這個表中查看excel 文件表名,我不知道從哪里開始,這是我查看 excel 內容的代碼:
public class ReadInvoices {
private static final String NAME = "C:\\Users\\........\\excelfile.xlsx";
public static void main(String[] args) {
try {
FileInputStream file = new FileInputStream(new File(NAME));
Workbook workbook = new XSSFWorkbook(file);
DataFormatter dataFormatter = new DataFormatter();
Iterator<Sheet> sheets = workbook.sheetIterator();
while(sheets.hasNext()) {
Sheet sh = sheets.next();
System.out.println("Sheet name is "+sh.getSheetName()); //HERE IS WHEN I PRINT THE SHEET NAME ON CONSOLE, THIS IS WHAT I WANT TO VIEW ON MY JSP PAGE.
System.out.println("---------");
Iterator<Row> iterator = sh.iterator();
while(iterator.hasNext()) {
Row row = iterator.next();
Iterator<Cell> cellIterator = row.iterator();
while (cellIterator.hasNext()) {
Cell cell = cellIterator.next();
String cellValue = dataFormatter.formatCellValue(cell);
System.out.print(cellValue+"\t");
}
System.out.println();
}
}
workbook.close();
}
catch(Exception e) {
e.printStackTrace();
}
}
}
現在這是我的 jsp 頁面,只有表格。
<html lang="it-it" class="no-js"> <!--<![endif]-->
<head>
</head>
<body>
<table class="center">
<tr>
<th>EXCEL SHEET NAME</th>
</tr>
<tr>
<th>...sheetsnames</th>
</tr>
</table>
</body>
</html>
我想應該是這樣的
<%try
{
FileInputStream file = new FileInputStream(new File(NAME));
Workbook workbook = new XSSFWorkbook(file);
DataFormatter dataFormatter = new DataFormatter();
Iterator<Sheet> sheets = workbook.sheetIterator();
%><table border=1 align=center style="text-align:center">
<thead>
<tr>
<th>EXCEL SHEET NAME</th>
</tr>
</thead>
<tbody>
<%while(sheets.next())
{
%>
<tr>
<td><%=sheets.next().getSheetName()%></td>
</tr>
<%}%>
</tbody>
</table><br>
<%}
catch(Exception e){
out.print(e.getMessage());%><br><%
}
finally{
st.close();
con.close();
}
%>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.