欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java web的读取Excel简单实例代码

程序员文章站 2024-03-12 14:36:50
目录结构: data.xls数据:   后台页面: public void doget(httpservletrequest reques...

目录结构:

Java web的读取Excel简单实例代码

data.xls数据:

Java web的读取Excel简单实例代码

 

后台页面:

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 web的读取excel简单实例代码就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。