import com.extentech.formats.XLS.CellNotFoundException; import com.extentech.formats.XLS.CellTypeMismatchException; import javax.servlet.http.*; import javax.servlet.*; import java.io.*; /** A Servlet which allows one to upload an Excel File, then retrieve an XML version of its contents. -> To run this program now, Download your eval copy of ExtenXLS, @author John McMahon -- Copyright ©2003 Extentech Inc. @version 2.01 @since 1.3 */ public class Excel2XMLServlet extends javax.servlet.http.HttpServlet{ // in production we'd probably read these from a config file int DEBUGLEVEL = 0, MAXINPUTSIZE = 50000,MININPUTSIZE = 12000; String ERRPAGE = "/products/ExtenXLS/samples/Excel2XML/Excel2XMLError.jsp?error="; WorkBookHandle book; ExtenXLS xls; public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { log("Excel2XMLServlet Called..."); try{ // get the template file bytes from the client ServletInputStream ins = req.getInputStream(); int bbuf = 0; byte[] barray = new byte[1024]; for(int x=0;x<4;x++)bbuf = ins.readLine(barray, 0, barray.length); ByteArrayOutputStream bout = new ByteArrayOutputStream(); while(ins.available()>0)bout.write(ins.read()); barray = bout.toByteArray(); // sanity checks if(barray.length > MAXINPUTSIZE)throw new Exception("File size: " + barray.length + " exceeds Maximum upload file size of:" + MAXINPUTSIZE); if(barray.length < MININPUTSIZE)throw new Exception("File size: " + barray.length + " smaller than Minimum upload file size of:" + MININPUTSIZE); log("loading workbook..."); book = new WorkBookHandle(barray); xls = new ExtenXLS(); xls.setWorkBook(book); log("converting to xml..."); byte[] buf = xls.getXML().getBytes(); book = null; // reset the book log("streaming bytes..."); res.setStatus(HttpServletResponse.SC_OK); res.setContentType("text/xml"); ServletOutputStream out = res.getOutputStream(); out.write(buf); }catch(Exception e){ log(e.toString()); res.sendRedirect(ERRPAGE+e.toString()); } } }