On Tue, 2002-11-12 at 12:17, Avik Sengupta wrote: > http://jakarta.apache.org/poi/hssf/quick-guide.html > http://jakarta.apache.org/poi/hssf/how-to.html
Thanks Avik. I had been trying to understand those documents. I had another look and found a link to an example that did help tremendously, and from which I've managed to derive the code that follows. I'd appreciate any comments on the following, and, if anyone has the time, advice on how to do something like this using eventmodel: ----------------------------------------------------------------------- import java.io.*; import java.util.*; import javax.servlet.*; import javax.servlet.http.*; import org.apache.poi.poifs.filesystem.POIFSFileSystem; import org.apache.poi.hssf.record.*; import org.apache.poi.hssf.model.*; import org.apache.poi.hssf.usermodel.*; import org.apache.poi.hssf.util.*; import com.oreilly.servlet.MultipartRequest; /** * Gets uploaded excel spreadsheet * Outputs html table */ /** Configuration Instructions * create the following directory structure: * <app_server>/webapps/fupload/WEB-INF/lib * /classes * create a file upload form, <app_server>/webapps/fupload/WEB-INF/upload.html ** <form action="/fupload/servlet/TableSS" method="post"> * copy cos.jar (from <http://www.servlets.com/cos>) to the lib directory * $ export CLASSPATH=$CLASSPATH:<app_server>/webapps/fupload/WEB-INF/lib/cos.jar ** for Resin: copy TableSS.java to <app_server>/webapps/fupload/WEB-INF/classes ** for Tomcat: compile TableSS.java: * $ export CLASSPATH=$CLASSPATH:<app_server>/webapps/fupload/WEB-INF/lib/cos.jar * javac -d <app_server>/webapps/fupload/WEB-INF/classes TableSS.java */ /** Credits * file upload code is from Jason Hunter's _Java Servlet Programming_ (O'Reilly). * POI HFFS code is derived from Andrew Oliver's example at <http://cvs.apache.org/viewcvs/~checkout~/jakarta-poi/src/java/org/apache/poi/hssf/dev/HSSF.java?rev=1.1>, which is copyrighted to The Apache Foundation. */ public class TableSS extends HttpServlet { public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { res.setContentType("text/html"); PrintWriter out = res.getWriter(); out.println("<html>"); out.println("<head><title>Spreadsheet as Table</title></head>"); out.println("<body>"); try { MultipartRequest multi = new MultipartRequest(req, ".", 5 * 1024 * 1024); // 5 MB limit Enumeration params = multi.getParameterNames(); while (params.hasMoreElements()) { String name = (String)params.nextElement(); String value = multi.getParameter(name); out.println(name + " = " + value); } Enumeration files = multi.getFileNames(); while (files.hasMoreElements()) { String name = (String)files.nextElement(); String filename = multi.getFilesystemName(name); String type = multi.getContentType(name); File f = multi.getFile(name); out.println("<pre>"); out.println("name: " + name); out.println("filename: " + filename); out.println("type: " + type); if (f != null) { out.println("length: " + f.length()); out.println(); } out.println("</pre>"); try { POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(filename)); HSSFWorkbook wb = new HSSFWorkbook(fs); for (int k = 0; k < wb.getNumberOfSheets(); k++) { HSSFSheet sheet = wb.getSheetAt(k); out.println( "<h3>" + filename + " : Sheet " + (k + 1) + "</h3>" ); out.println( "<table border='1' cellpadding='5'>" ); int rows = sheet.getPhysicalNumberOfRows(); for (int r = 0; r < rows; r++) { HSSFRow row = sheet.getRow(r); int cells = row.getPhysicalNumberOfCells(); out.print("<tr><th>" + row.getRowNum() + "</th>"); for (int c = 0; c < cells; c++) { out.print("<td>"); HSSFCell cell = row.getCell((short)c); try { switch (cell.getCellType()) { case HSSFCell.CELL_TYPE_FORMULA : out.print("FORMULA "); break; case HSSFCell.CELL_TYPE_NUMERIC : out.print( cell.getNumericCellValue() ); break; case HSSFCell.CELL_TYPE_STRING : out.print( cell.getStringCellValue() ); break; default : out.print( "unsupported cell type" ); break; } } catch (NullPointerException e) { // blank cell cells++; out.print(" "); } out.print("</td>"); } out.println("</tr>"); } out.println( "</table>" ); } } catch ( IOException ex ) { out.println("<pre>"); ex.printStackTrace(out); out.println("</pre>"); } } } catch (Exception e) { out.println("<pre>"); e.printStackTrace(out); out.println("</pre>"); } out.println("</body></html>"); out.close(); } } ----------------------------------------------------------------------- -- Regards Sean Carte University of Zululand Network Services Unit Phone: 035 902 6081 Fax: 035 902 6028 "Mistakes? We don't make mistakes." -- Bill, Department of Works -- To unsubscribe, e-mail: <mailto:poi-user-unsubscribe@;jakarta.apache.org> For additional commands, e-mail: <mailto:poi-user-help@;jakarta.apache.org>
