<!--
  Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->
<%@page pageEncoding="utf-8" contentType="text/html; charset=utf-8"
	import="java.io.*,org.apache.poi.poifs.filesystem.POIFSFileSystem,org.apache.poi.xssf.model.*,org.apache.poi.xssf.usermodel.*,org.apache.poi.xssf.util.*"%>
<%
	request.setCharacterEncoding("utf-8");
	String filename = request.getParameter("xls_filename");
	if (null == filename) {
		filename = "";
	}
%>
<html>
<head>
<title>Read Excel file</title>
</head>
<body>
An example of using Jakarta POI's <span style="font-size:large;color:blue;">XSSF</span> package to read an excel spreadsheet (.xlsx)<br>
<br>
<form name="form1" method="post" action="">
Input an Excel file path to read.<br>
<input type="text" name="xls_filename" size="80" value="<%=filename %>"><br>
 (e.g.)
<span id="test_file"><%=session.getServletContext().getRealPath("") %>\workbook.xlsx</span>
<input type="button" value="set" onclick="this.form.xls_filename.value=document.getElementById('test_file').innerHTML;"><br>
<br>
<input type="submit" value="submit"><br>
<br>
</form>
<br>
<%
	if (filename != null && !filename.equals("")) {
%>
You chose the file
<%=filename%>.
<br>
<br>
It's contents are:
<%
	try {
			// create a poi workbook from the excel spreadsheet file
			XSSFWorkbook wb = new XSSFWorkbook(filename);
			for (int k = 0; k < wb.getNumberOfSheets(); k++) {
%>
<br>
<br>
Sheet
<%=k%>
<br>
<%
	XSSFSheet sheet = (XSSFSheet) wb.getSheetAt(k);
				int rows = sheet.getPhysicalNumberOfRows();
				for (int r = 0; r < rows; r++) {
					XSSFRow row = (XSSFRow) sheet.getRow(r);
					if (row != null) {
						int cells = row.getPhysicalNumberOfCells();
%>
<br>
<b>ROW <%=row.getRowNum()%> </b><br>
<%
	for (short c = 0; c < cells; c++) {
							XSSFCell cell = (XSSFCell) row.getCell(c);
							if (cell != null) {
								String value = null;
								switch (cell.getCellType()) {
								case XSSFCell.CELL_TYPE_FORMULA:
									value = "FORMULA ";
									break;
								case XSSFCell.CELL_TYPE_NUMERIC:
									value = "NUMERIC value="
											+ cell.getNumericCellValue();
									break;
								case XSSFCell.CELL_TYPE_STRING:
									value = "STRING value="
											+ cell.getStringCellValue();
									break;
								default:
								}
%>
<%="CELL col="
												+ cell.getColumnIndex()
												+ " VALUE=" + value%><br>
<%
	}
						}
					}
				}
			}
		} catch (Exception e) {
%>
Error occurred:
<%=e.getMessage()%>
<%
	e.printStackTrace();
		}

	}
%>
</body>
</html>
