Java web的读取Excel简单实例代码
程序员文章站
2024-03-12 14:36:50
目录结构:
data.xls数据:
后台页面:
public void doget(httpservletrequest reques...
目录结构:
data.xls数据:
后台页面:
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; }
前台页面:
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>
运行结果:
以上这篇java web的读取excel简单实例代码就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。
上一篇: Java封装、继承、多态三大特征的理解
下一篇: PHP PDO操作MySQL基础教程