Directory structure:
Data.xls Data:
?
Background page:
Getexceldata.java
?
???? public void doget (HttpServletRequest request, httpservletresponse response)
???????????? Throws 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>
Operation Result:
Java Web read Excel simple Demo