You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@poi.apache.org by bu...@apache.org on 2002/12/31 00:22:43 UTC

DO NOT REPLY [Bug 15727] New: - example use of POI (self-contained JSP page)

DO NOT REPLY TO THIS EMAIL, BUT PLEASE POST YOUR BUG 
RELATED COMMENTS THROUGH THE WEB INTERFACE AVAILABLE AT
<http://nagoya.apache.org/bugzilla/show_bug.cgi?id=15727>.
ANY REPLY MADE TO THIS MESSAGE WILL NOT BE COLLECTED AND 
INSERTED IN THE BUG DATABASE.

http://nagoya.apache.org/bugzilla/show_bug.cgi?id=15727

example use of POI (self-contained JSP page) 

           Summary: example use of POI (self-contained JSP page)
           Product: POI
           Version: 1.5.1
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: Enhancement
          Priority: Other
         Component: HSSF
        AssignedTo: poi-dev@jakarta.apache.org
        ReportedBy: bw@mcwestcorp.com


Tried POI HSSF the other day and it was great!  Then I noticed a few issues 
with the example code I found that printed an entire worksheet, notably that it 
didn't like blank cells. I've attached a modified version that no longer has 
this problem. Also, I've made it a JSP page (though I know this isn't the
best way to use POI in a *real* app) which makes it easy for someone to try out 
POI/HSSF and have a simple example to work from. The JSP is attached below as a 
possible contribution to POI. 


JSP source file begins below this line.  

<%@page contentType="text/html" 
import="java.io.*,org.apache.poi.poifs.filesystem.POIFSFileSystem,org.apache.poi
.hssf.record.*,org.apache.poi.hssf.model.*,org.apache.poi.hssf.usermodel.*,org.a
pache.poi.hssf.util.*" %>
<html>
<head><title>Read Excel file </title>
</head>
<body>
An example of using Jakarta POI's HSSF package to read an excel spreadsheet: 


<form name="form1" method="get" action="">
Select an Excel file to read. 
  <input type="file" name="xls_filename" onChange="form1.submit()">
</form>

<%
	String filename = request.getParameter("xls_filename"); 
	if (filename != null && !filename.equals("")) {
%>	
	<br>You chose the file <%= filename %>. 
	<br><br>It's contents are: 	
<%	
            try
            {

                // create a poi workbook from the excel spreadsheet file
                POIFSFileSystem fs =
                    new POIFSFileSystem(new FileInputStream(filename));
                HSSFWorkbook wb = new HSSFWorkbook(fs);

                for (int k = 0; k < wb.getNumberOfSheets(); k++)
                {
%>				
                    <br><br>Sheet  <%= k %> <br>
<%					
					
                    HSSFSheet sheet = wb.getSheetAt(k);
                    int       rows  = sheet.getPhysicalNumberOfRows();

                    for (int r = 0; r < rows; r++)
                    {
                        HSSFRow row   = sheet.getRow(r);
                        if (row != null) { 
                            int     cells = row.getPhysicalNumberOfCells();
%>
							<br><b>ROW  <%= 
row.getRowNum() %> </b>
<%
                            for (short c = 0; c < cells; c++) 
                            { 
                                HSSFCell cell  = row.getCell(c);
                                if (cell != null) { 
                                    String   value = null;

                                    switch (cell.getCellType())
                                    {

                                        case HSSFCell.CELL_TYPE_FORMULA :
                                            value = "FORMULA ";
                                            break;

                                        case HSSFCell.CELL_TYPE_NUMERIC :
                                            value = "NUMERIC value="
                                                    + cell.getNumericCellValue
();
                                            break;

                                        case HSSFCell.CELL_TYPE_STRING :
                                            value = "STRING value="
                                                    + cell.getStringCellValue();
                                            break;

                                        default :
                                    }
%>									
                                    <%= "CELL col=" 
									
	+ cell.getCellNum()
                                        + " VALUE=" + value %>
<%
                                } 
                            }
                        }
                    }
                }
            }
            catch (Exception e)
            {
%>
	Error occurred:  <%= e.getMessage() %>
<%			
                e.printStackTrace();
            }

	} 
%> 
</body>
</html>



JSP source file ends above this line.