zoukankan      html  css  js  c++  java
  • javaweb jsp页面上传excel文件

    servlet:

    private static final long FILE_MAX_SIZE = 4 * 1024 * 1024; 

    if (!ServletFileUpload.isMultipartContent(request)) {  
              System.out.println("失败1");
             }  
             String fileTempPath = this.getServletContext().getRealPath("/") + "filetemp";  
             File tempDir = new File(fileTempPath);  
             if (!tempDir.exists()) {  
                 tempDir.mkdirs();  
             }  
             FileItemFactory factory = new DiskFileItemFactory(4096, tempDir);  
       
             ServletFileUpload sfu = new ServletFileUpload(factory);  
             sfu.setFileSizeMax(FILE_MAX_SIZE);  
       
             List<FileItem> fileItems = null;  
       
             try {  
                 fileItems = sfu.parseRequest(request);  
             } catch (FileUploadException e) {  
                 e.printStackTrace();  
                 if (e instanceof SizeLimitExceededException) {  
                  
                 }  
                 return;  
             }  
             if (fileItems == null || fileItems.size() == 0) {  
                 System.out.println("失败2" );
                 return;  
             } 

      Workbook rwb;
      try {
       rwb = Workbook.getWorkbook(fileItems.get(0).getInputStream());
       int sheetCount=rwb.getNumberOfSheets();
        Sheet rs=rwb.getSheet(0);
        int rows=rs.getRows(); //行
        int cols=rs.getColumns(); //列     -- getCell(列,行)
        System.out.println(rows+"-*-*-*-"+cols);
        for (int i = 2; i < rows ; i++) {
         Cell cell = rs.getCell(i, 0);
         System.out.print(rs.getCell(0, i).getContents()+"-*-");
         System.out.print(rs.getCell(1, i).getContents()+"-*-");
         System.out.print(rs.getCell(2, i).getContents()+"-*-");
         System.out.print(rs.getCell(3, i).getContents()+"-*-");
         System.out.print(rs.getCell(4, i).getContents()+"-*-");
         System.out.print(rs.getCell(5, i).getContents()+"-*-");
         System.out.print(rs.getCell(6, i).getContents()+"-*-");
         System.out.print(rs.getCell(7, i).getContents()+"-*-");
         System.out.print(rs.getCell(8, i).getContents()+"-*-");
         System.out.print(rs.getCell(9, i).getContents()+"-*-");
         System.out.println();
        }
      } catch (BiffException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
      }finally{
       fileItems.get(0).getInputStream().close();
       
      }

    jsp:

    <script type="text/javascript">
     function CheckExcel() {
      var mime = document.getElementById('Excelfile').value;
      mime = mime.toLowerCase().substr(mime.lastIndexOf("."));
      if (!(mime == ".xls")) {
       alert("请导入正确的EXCEL文件,仅支持xls格式!");
       return false;
      }
     }
    </script>
        <body>
         <form action="servlet/ExcelTest" enctype="multipart/form-data" method="post" onsubmit="return CheckExcel()">
          <input type="file" name="Excelfile" onchange="CheckExcel(this)" id="Excelfile"/>

          <input type="submit" value="读EXCEL"/>
         </form>
      </body>

  • 相关阅读:
    C++学习 之 程序的组成部分(部分知识笔记)
    debian下重装mysql
    CGI 环境变量
    boost的libboost_system问题
    debian下使用shell脚本时出现了 declare:not found 解决方法
    编译的时候出现"/usr/bin/ld: cannot find -lz
    glibc升级,解决glib版本过低的问题
    ubuntu彩色图形界面
    (转)http://blog.chinaunix.net/uid-8363656-id-2031644.html CGI 编写
    linux安装JSONCPP
  • 原文地址:https://www.cnblogs.com/mingjian/p/5226093.html
Copyright © 2011-2022 走看看