Directory structure:
Data.xls Data:
Background page:
public void doget (HttpServletRequest request, httpservletresponse response) thro
WS Servletexception, IOException {//system.out.println (This.getservletcontext (). Getrealpath ("/")); try{Workbook wb = Workbook.getworkbook (New File (This.getservletcontext (). Getrealpath ("/") + Data.xls
"));
System.out.println ("2222222");
list<data> Data = new arraylist<data> ();
for (int i=1;i<wb.getsheet (0). GetRows (); i++) {cell[] Cell = Wb.getsheet (0). GetRow (i);
Data.add (New Data (Cell[0].getcontents (), cell[1].getcontents (), cell[2].getcontents ());
System.out.println (Cell[1].getcontents ());
} wb.close ();
Request.setattribute ("Data", data); Request.getrequestdispatcher ("..
/index.jsp "). Forward (request, response);
}catch (Exception e) {e.printstacktrace (); }
}
Data.java
public class Data {
private String ID;
private String name;
private String password;
Public Data () {
super ();
TODO auto-generated constructor stub
} public
Data (string ID, string name, string password) {
super ();
this.id = ID;
this.name = name;
This.password = password;
}
Public String GetId () {return
ID;
}
Front page:
INDEX.JSP:
<body>
<form action= "Servlet/getexceldata" method= "POST" >
<input type= "Submit" id= "Tijiao" Value= "Submit" >
</form>
<%
list<data> Data = new arraylist<data> ();
Data= (list<data>) request.getattribute ("Data");
if (Data!=null&&data.size () >0) for
(data d:data) {
out.println (D.getid () + "---" +d.getname () + "--- "+d.getpassword ());
}
%>
</body>
Run Result:
The above Java Web reading Excel Simple example code is a small series to share all the content, hope to give you a reference, but also hope that we support the cloud habitat community.