• 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>

    运行结果:

  • 相关阅读:
    基于requirejs和oclazyloadjs的实际按需加载的angular项目
    vue源码解析data与watcher
    video标签播放数据调整
    ngnix搭建前端服务
    面试总结篇(一)
    mongodb的安装、配置、启动、数据库密码的配置;
    今天随手写个发布订阅的 event.js
    Mac OS系统使用的链接,以及关于前端软件的安装(持续更新)
    bind 以及原型 1px边框的实现(面试后内容整理)
    flexbox的学习,display:flex display:box 淘宝触屏版、饿了么网布局分析分析
  • 原文地址:https://www.cnblogs.com/bb1119/p/5494677.html
Copyright © 2020-2023  润新知