zoukankan      html  css  js  c++  java
  • POI-读取Excel文件

     1 import java.io.File;
     2 import java.io.FileInputStream;
     3 import java.io.FileNotFoundException;
     4 import java.io.IOException;
     5 import java.util.ArrayList;
     6 import java.util.List;
     7 
     8 import org.apache.poi.hssf.usermodel.HSSFCell;
     9 import org.apache.poi.hssf.usermodel.HSSFRow;
    10 import org.apache.poi.hssf.usermodel.HSSFSheet;
    11 import org.apache.poi.hssf.usermodel.HSSFWorkbook;
    12 import org.poi.pojo.User;
    13 
    14 public class ExcelToPoi {
    15     public static void main(String[] args) throws FileNotFoundException, IOException {
    16         long currentTimeMillis = System.currentTimeMillis();
    17         List<User> arrayList = new ArrayList<User>();
    18         int id = 0;
    19         String name = null;
    20         int age = 0;
    21         String sex = null;
    22         long phoneNum = 0L;
    23         String homeAddress = null;
    24         String companyAddress = null;
    25         HSSFWorkbook hssfWorkbook = new HSSFWorkbook(new FileInputStream(new File("F:\students.xls")));
    26         for (int q = 0; q < 8; q++) {
    27             HSSFSheet sheetAt = hssfWorkbook.getSheetAt(q);
    28             int firstRowNum = sheetAt.getFirstRowNum() + 1;// 开始行号
    29             int lastRowNum = sheetAt.getLastRowNum();// 结束行号
    30             for (int i = firstRowNum; i <= lastRowNum; i++) {
    31                 HSSFRow row = sheetAt.getRow(i);
    32                 int firstCellNum = row.getFirstCellNum();
    33                 int lastCellNum = row.getLastCellNum();
    34                 for (int k = firstCellNum; k < lastCellNum; k++) {
    35                     HSSFCell cell = row.getCell(k);
    36                     switch (k) {
    37                     case 0:
    38                         id = (int) cell.getNumericCellValue();
    39                         break;
    40                     case 1:
    41                         name = cell.getStringCellValue();
    42                         break;
    43                     case 2:
    44                         age = (int) cell.getNumericCellValue();
    45                         break;
    46                     case 3:
    47                         sex = cell.getStringCellValue();
    48                         break;
    49                     case 4:
    50                         phoneNum = (long) cell.getNumericCellValue();
    51                         break;
    52                     case 5:
    53                         homeAddress = cell.getStringCellValue();
    54                         break;
    55                     case 6:
    56                         companyAddress = cell.getStringCellValue();
    57                         break;
    58                     }
    59                     arrayList.add(new User(id, name, age, sex, phoneNum, homeAddress, companyAddress));
    60 
    61                 }
    62 
    63             }
    64         }
    65 
    66 
    67         for (User arrayLisst : arrayList) {
    68             System.out.println(arrayLisst.getId() + "|" + arrayLisst.getName() + "|" + arrayLisst.getAge() + "|"
    69                     + arrayLisst.getSex() + "|" + arrayLisst.getPhoneNum() + "|" + arrayLisst.getHomeAddress() + "|"
    70                     + arrayLisst.getCompanyAddress());
    71         }
    72         System.out.println("有这么多数据:" + (arrayList.size()/7));
    73         long currentTimeMillis1 = System.currentTimeMillis();
    74         long oo = currentTimeMillis1-currentTimeMillis;
    75         System.out.println("运行时间:"+oo+"毫秒");
    76     }
    77 }

    实体类如下:

     1 public class User {
     2 
     3     private int id;
     4     private String name;
     5     private int age;
     6     private String sex;
     7     private long phoneNum;
     8     private String homeAddress;
     9     private String companyAddress;
    10     public int getId() {
    11         return id;
    12     }
    13     public void setId(int id) {
    14         this.id = id;
    15     }
    16     public String getName() {
    17         return name;
    18     }
    19     public void setName(String name) {
    20         this.name = name;
    21     }
    22     public int getAge() {
    23         return age;
    24     }
    25     public void setAge(int age) {
    26         this.age = age;
    27     }
    28     public String getSex() {
    29         return sex;
    30     }
    31     public void setSex(String sex) {
    32         this.sex = sex;
    33     }
    34     public long getPhoneNum() {
    35         return phoneNum;
    36     }
    37     public void setPhoneNum(long phoneNum) {
    38         this.phoneNum = phoneNum;
    39     }
    40     public String getHomeAddress() {
    41         return homeAddress;
    42     }
    43     public void setHomeAddress(String homeAddress) {
    44         this.homeAddress = homeAddress;
    45     }
    46     public String getCompanyAddress() {
    47         return companyAddress;
    48     }
    49     public void setCompanyAddress(String companyAddress) {
    50         this.companyAddress = companyAddress;
    51     }
    52     public User(int id, String name, int age, String sex, long phoneNum, String homeAddress, String companyAddress) {
    53         super();
    54         this.id = id;
    55         this.name = name;
    56         this.age = age;
    57         this.sex = sex;
    58         this.phoneNum = phoneNum;
    59         this.homeAddress = homeAddress;
    60         this.companyAddress = companyAddress;
    61     }
    62     public User() {
    63         super();
    64         // TODO Auto-generated constructor stub
    65     }
    66     
    67 
    68 
    69 }
  • 相关阅读:
    4 Apr 18 软件开发目录 logging模块的使用 序列化(Json, Pickle) os模块
    3 Apr 18 内置函数 列表生成式与生成器表达式 模块的使用之import 模块的使用之from…import…
    2 Apr 18 三元表达式 函数递归 匿名函数 内置函数
    30 Mar 18 迭代器 生成器 面向过程的编程
    29 Mar 18 函数 有参、无参装饰器
    28 Mar 18 函数
    27 Mar 18 函数的参数
    26 Mar 18 函数介绍
    23 Mar 18 文件处理
    22 Mar 18 补充数据类型+字符编码+文件处理
  • 原文地址:https://www.cnblogs.com/miss3316/p/8474139.html
Copyright © 2011-2022 走看看