当前位置: 首页 > 图灵资讯 > 技术篇> java中excel的读取操作

java中excel的读取操作

来源:图灵教育
时间:2023-06-02 09:23:08

很多时候,excel文件中的内容需要批量上传,此时需要相应的操作。但是excel有两种,一种是xls,另一种是xlsx。

(1):首先介绍jar包。maven的主要介绍如下:

<!-- https://mvnrepository.com/artifact/net.sourceforge.jexcelapi/jxl --> <dependency>     <groupId>net.sourceforge.jexcelapi</groupId>     <artifactId>jxl</artifactId>     <version>2.6.10</version> </dependency> <dependency>       <groupId>org.apache.poi</groupId>       <artifactId>poi</artifactId>        <version>3.9</version> </dependency><!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml --> <dependency>     <groupId>org.apache.poi</groupId>     <artifactId>poi-ooxml</artifactId>     <version>3.9</version> </dependency>

excel操作主要采用poi进行。

(2):读取excel内容,代码如下:

import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.util.ArrayList; import java.util.List; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.DateUtil; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; public class Test {     public static Object[][] getTestData(String filePath) throws FileNotFoundException, IOException {         File excelFile = new File(filePath);         /*          * 判断给定文件的类型; 1.如果是xls的问价类型,创建XSSFWorkBook ;          * 2.如果是xlsx的文件类型,则创建HSSFWorkBook ;          */         String xls = filePath.substring(filePath.indexOf('.'));         System.out.println("传入文件的后缀是:" + xls + " ;");         if (xls.equals(".xls")) {             HSSFWorkbook xlswb = new HSSFWorkbook(new FileInputStream(excelFile));             HSSFSheet sheet = xlswb.getSheetAt(0);             int rowcount = sheet.getLastRowNum() - sheet.getFirstRowNum();             List<Object[]> list = new ArrayList<Object[]>();             // System.out.println("-sheet总共有 :" + rowcount + " ;");             Row row;             Cell cell;             for (int i = 0; i < rowcount + 1; i++) {                 row = sheet.getRow(i);                 if(row.getCell(i)==null){                     continue ;                 }else{                                       }                 Object[] obj = new Object[row.getLastCellNum()];                              /*                  * System.out.println("当前行是:" + (row.getRowNum() + 1) +                  * " ;当前行的第一个单元格是:" + row.getFirstCellNum() + " ; 目前最后一个单元格是:"                  * + row.getLastCellNum() + "; ");                  */                 // System.out.println("obj 数组的长度是 :" + obj.length + " ;");                 for (int j = 0; j < row.getLastCellNum(); j++) {                     cell = row.getCell(j);                     switch (cell.getCellType()) {                     case Cell.CELL_TYPE_STRING:                         obj[j] = cell.getRichStringCellValue().getString();                         /*                          * System.out.print(cell.getRichStringCellValue().                          * getString()); System.out.print("|");                          */ break;                     case Cell.CELL_TYPE_NUMERIC:                         if (DateUtil.isCellDateFormatted(cell)) {                             obj[j] = cell.getDateCellValue();                             // System.out.print(String.valueOf(cell.getDateCellValue()));                         } else {                             obj[j] = cell.getNumericCellValue();                             // System.out.print(cell.getNumericCellValue());                         }                         // System.out.print("|");                         break;                     case Cell.CELL_TYPE_BOOLEAN:                         obj[j] = cell.getBooleanCellValue();                         /*                          * System.out.print(cell.getBooleanCellValue());                          * System.out.print("|");                          */ break;                     default:                     }                 }                 list.add(obj);                 // System.out.println();             }             // System.out.println("list.size()===” + list.size());             Object[][] object = new Object[list.size()][];             for (int i = 0; i < object.length; i++) {                 object[i] = list.get(i);             }             return object;         } else if (xls.equals(".xlsx")) {             XSSFWorkbook wbxlsx = new XSSFWorkbook(new FileInputStream(excelFile));             XSSFSheet sheet = wbxlsx.getSheetAt(0);             int rowcount = sheet.getLastRowNum() - sheet.getFirstRowNum();             List<Object[]> list = new ArrayList<Object[]>();             // System.out.println("-sheet总共有 :" + rowcount + " ;");             Row row;             Cell cell;             for (int i = 0; i < rowcount + 1; i++) {                 row = sheet.getRow(i);                 /*                  * System.out.println("当前行是:" + (row.getRowNum() + 1) +                  * " ;当前行的第一个单元格是:" + row.getFirstCellNum() + " ; 目前最后一个单元格是:"                  * + row.getLastCellNum() + "; ");                  */                 String orderCode=null;                 String shipmentNo=null;                 Object[] obj = new Object[row.getLastCellNum()];                 // System.out.println("obj 数组的长度是 :" + obj.length + " ;");                 for (int j = 0; j < row.getLastCellNum(); j++) {                     cell = row.getCell(j);                     if(j==0){                     orderCode=cell.toString();                     }else if(j==1){                     shipmentNo=cell.toString();                     }                                    }                 if(orderCode!=null&&shipmentNo!=null){                 System.out.println("orderCode=="+orderCode+",shipmentNo="+shipmentNo);                 }                              }             // System.out.println("list.size()===” + list.size());             Object[][] object = new Object[list.size()][];             for (int i = 0; i < object.length; i++) {                 object[i] = list.get(i);             }             return object;         } else {             System.out.println(”指定的文件不是excle文件!");         }         return null;     }     public static void main(String[] args) throws FileNotFoundException, IOException {////读取excel文件的位置         String filePathxlsx = "C:\\Users\\user\\Desktop\\different.xlsx";         Object[][] objxlsx = Test.getTestData(filePathxlsx);     for(int i=0;i<10;i++){     try {               System.out.println("i=="+i);                 Thread.sleep(10000);               } catch (InterruptedException e) {                   e.printStackTrace();                }       }                 /*          * for (int i = 0; i < objxlsx.length; i++) {          * System.out.println(objxlsx[i]); }          */     } }

(3):总结

读取excel的方法有很多种,因人而异。