zoukankan      html  css  js  c++  java
  • javaWeb使用POI操作Excel

    1.为项目添加POI

    POI官网链接

    点进去之后下载(上边的是编译好的类,下边的是源代码)

     解压文件夹,把下面三个文件复制到WebComtent>WEB-INF>lib文件夹下

    再把这三个文件复制到Tomcat的lib文件夹下,否则Tomcat会因为找不到类而报错(这个地方郁闷了一上午)

    读取“.xls”格式使用  import org.apache.poi.hssf.usermodel.*;包的内容,例如:HSSFWorkbook
    读取“.xlsx”格式使用 import org.apache.poi.xssf.usermodel.*; 包的内容,例如:XSSFWorkbook
    读取两种格式使用    import org.apache.poi.ss.usermodel.*    包的内容,例如:Workbook

    由于我是读取xslx文件所以使用以上几个jar文件。

    注意:

    上图中的两个文件夹中也有我们需要的jar文件,具体是哪几个忘记了(当然为了保险也可以把所有的都放进WebContent>WEN-INF>lib下再BuildPath进项目),没关系,一会运行的过程中会报错,根据错误信息再去找到相关的jar文件BuildPath进去就好,注意还要再Tomcat>lib下放置一份副本。

    2.读取Excel文件

     1 //遍历一个Excel文件<br>private void getExcelData(File file)  {
     2          
     3         System.out.println("now in getExcelData" );
     4         System.out.println("get file name:"+file.getName().toString());
     5          
     6         XSSFWorkbook workBook= null;
     7         try {
     8             workBook  = new XSSFWorkbook(file);
     9              
    10              
    11             int sheetCount = workBook.getNumberOfSheets();  //Sheet的数量
    12             System.out.println("num of sheet is : "+sheetCount);
    13              
    14             //遍历每个sheet
    15             for(int i=0;i<sheetCount;i++)
    16             {
    17                 XSSFSheet sheet = workBook.getSheetAt(i);
    18                 //获取总行数
    19                 int rowCount = sheet.getPhysicalNumberOfRows();
    20                 System.out.println("num of row : "+ rowCount);
    21                 System.out.println("i  now in sheet : "+ i);
    22                 //遍历每一行 
    23                 for (int r = 0; r < rowCount; r++) {
    24                     XSSFRow row = sheet.getRow(r); 
    25                     //获取总列数 
    26                     int cellCount = row.getPhysicalNumberOfCells();
    27                      
    28                     //遍历每一列 
    29                      for (int c = 0; c < cellCount; c++) {
    30                          XSSFCell cell = row.getCell(c);
    31                           
    32                          String cellValue = null;
    33                           
    34                          switch (cell.getCellTypeEnum()) {
    35                         case STRING:
    36                             //System.out.println("celltype is string");
    37                             cellValue = cell.getStringCellValue(); 
    38                             break;
    39                         case NUMERIC:
    40                             //System.out.println("celltype is Number");//整数,小数,日期
    41                             cellValue = String.valueOf(cell.getNumericCellValue());
    42                             break;
    43                         case BOOLEAN:
    44                             //System.out.println("celltype is Boolean");
    45                             cellValue = String.valueOf(cell.getBooleanCellValue()); 
    46                             break;
    47                         case FORMULA:
    48                             //System.out.println("celltype is Formula");//公式
    49                             cellValue = "错误,不能为公式"; 
    50                             break;
    51                         case BLANK:
    52                             //System.out.println("celltype is Blank");//空白
    53                             cellValue = cell.getStringCellValue();
    54                             break;
    55                         case ERROR:
    56                             //System.out.println("celltype is Error");
    57                             cellValue = "错误"; 
    58                             break;
    59                          
    60                         default:
    61                             //System.out.println("celltype : default");
    62                             cellValue = "错误";
    63                             break;                         
    64                         }
    65                           
    66                          System.out.println(cellValue.toString());
    67                      }
    68                 }
    69             }
    70  
    71              
    72         } catch (IOException e) {
    73             System.out.println("File Error IOException : "+e.getMessage());
    74         }  
    75         catch (Exception e) {
    76             // TODO: handle exception
    77         }
    78         finally {
    79             try {
    80                 workBook.close();
    81                } catch (Exception e) {
    82                 // TODO Auto-generated catch block
    83                 e.printStackTrace();
    84                 System.out.println("workBook.close()&fileInputStream.close()  Error : "+e.getMessage());       
    85             }
    86              
    87             System.out.println("Try Catch : finally");         
    88         }
    89          
    90         System.out.println("hi feipeng8848 getExcelData is done");
    91     }
  • 相关阅读:
    作妖系列——更改spyder黑色主题
    latex beamer 插入代码
    LaTeX 如何在文档的侧面插入图片实现"绕排"?
    svm
    约束优化方法之拉格朗日乘子法与KKT条件
    Latex algorithm
    对于连续目标函数的学习问题,当误差为正态分布,而且在没有任何先验知识的条件下,最大似然估计与最小均方误差等价
    R语言table()函数
    高性能Linux服务器配置
    深度学习
  • 原文地址:https://www.cnblogs.com/tjzhangjianjun/p/6782326.html
Copyright © 2011-2022 走看看