zoukankan      html  css  js  c++  java
  • Java web的读取Excel简单Demo

    目录结构:

    Data.xls数据:

     

    后台页面:

    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;

        }

    ----------

    前台页面:

    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>

    运行结果:

  • 相关阅读:
    F2etest v2.0.0 正式发布 ,阿里巴巴开源测试方案
    Spring-Wind 1.1.1 发布,SSM 架构核心库
    通过nginx配置文件抵御攻击 | WooYun知识库
    阿里云X-Forwarded-For 发现tomcat记录的日志全部来自于SLB转发的IP地址,不能获取到请求的真实IP。
    openresty+lua在反向代理服务中的玩法 | WooYun知识库
    java~lambda表达式让查询更优雅
    java~日期与字符串的转化
    java~google样式检查和命名规范
    springboot~Mongodb的集成与使用
    springboot~JPA把ORM统一起来
  • 原文地址:https://www.cnblogs.com/bb1119/p/5494677.html
Copyright © 2011-2022 走看看