zoukankan      html  css  js  c++  java
  • Excelutil 工具类

    1.说明:ExcelUtil主要用于获得单元格的数据和对对指定单元格中写入数据用!

    相关代码如下:

      1 package main.java;
      2 
      3 import java.io.File;
      4 import java.io.FileInputStream;
      5 import java.io.FileOutputStream;
      6 import java.util.ArrayList;
      7 import java.util.List;
      8 
      9 import org.apache.poi.hssf.usermodel.HSSFWorkbook;
     10 import org.apache.poi.ss.usermodel.Row;
     11 import org.apache.poi.ss.usermodel.Sheet;
     12 import org.apache.poi.ss.usermodel.Workbook;
     13 import org.apache.poi.xssf.usermodel.XSSFCell;
     14 import org.apache.poi.xssf.usermodel.XSSFRow;
     15 import org.apache.poi.xssf.usermodel.XSSFSheet;
     16 import org.apache.poi.xssf.usermodel.XSSFWorkbook;
     17 
     18 
     19 public class ExcleUtil {
     20     private static XSSFSheet ExcelWSheet;
     21     private static XSSFWorkbook ExcelWBook;
     22     private static XSSFCell Cell;
     23     private static XSSFRow Row;
     24     private static String ExcelFilePath="C:\TEST.xlsx";
     25 
     26     // 设定要设置的Excel的文件路径和Excel 中Sheet名;
     27     // 在读/写Excel 的时候先要调用此方法
     28     public static void setExcleFile(String FilePath, String sheetName) throws Exception {
     29         FileInputStream ExcleFile;
     30         try {
     31             // 实例化Excle文件的FileInputStream 对象;
     32             ExcleFile = new FileInputStream(FilePath);
     33             // 实例化Excle文件的XSSFWorkbook 对象;
     34             ExcelWBook = new XSSFWorkbook(ExcleFile);
     35             /*
     36              * 实例化XSSFSheet 对象,指定ExcelFile中的sheet名称,用于后续对sheet中行和列的操作;
     37              * 
     38              */
     39             ExcelWSheet = ExcelWBook.getSheet(sheetName);
     40 
     41         } catch (Exception e) {
     42             e.getStackTrace();
     43         }
     44 
     45     }
     46     /*
     47      * 读取excle文件指定单元格的函数 ;
     48      * 
     49      */
     50 
     51     public static String getCell(int row, int col) throws Exception {
     52 
     53         try {
     54             // 通过函数参数指定单元格的行号和列,获取指定单元格的对象;
     55             Cell = ExcelWSheet.getRow(row).getCell(col);
     56             /*
     57              * 1.如果单元格的类型为字符串类型,使用getStringCellValue();来获取单元格的内容;
     58              * 2.如果单元格的类型为数字类型,使用getNumberricCellValue();来获取单元格的内容;
     59              * 注意:getNumberricCellValue();返回的值为double类型,转为为字符串类型,必须在
     60              * getNumberricCellValue();前面加上(" ")双引号,用于强制转换为String类型,不加双引号
     61              * 则会抛错;double类型无法转换为String类型的异常;
     62              * 
     63              */
     64             String CellData = Cell.getCellType() == XSSFCell.CELL_TYPE_STRING ? Cell.getStringCellValue() + ""
     65                     : String.valueOf(Math.round(Cell.getNumericCellValue()));
     66             return CellData;
     67         } catch (Exception e) {
     68             e.getStackTrace();
     69             return "";
     70         }
     71 
     72     }
     73     /*
     74      * 在Excle中执行单元格写入数据;
     75      * 
     76      * 
     77      */
     78 
     79     public static void setCellData(int rownum, int colnum, String Result) throws Exception {
     80 
     81         try {
     82             // 获取excle文件的中行对象;
     83             Row = ExcelWSheet.getRow(rownum);
     84             // 如果单元格为空则返回null;
     85             Cell = Row.getCell(colnum, Row.RETURN_BLANK_AS_NULL);
     86             if (Cell == null) {
     87                 // 当单元格为空是则创建单元格
     88                 // 如果单元格为空无法调用单元格对象的setCellValue方法设定单元格的值 ;
     89                 Cell = Row.createCell(colnum);
     90                 // 创建单元格和后可以通过调用单元格对象的setCellValue方法设置单元格的值了;
     91                 Cell.setCellValue(Result);
     92             } else {
     93                 // 单元格中有内容,则可以直接调用单元格对象的 setCellValue 方法来设置单元格的值;
     94                 Cell.setCellValue(Result);
     95             }
     96             FileOutputStream fileout = new FileOutputStream(ExcelFilePath);
     97             // 将内容写到Excel文件中 ;
     98             ExcelWBook.write(fileout);
     99             // j调用flush方法强制刷新写入文件;
    100             fileout.flush();
    101             fileout.close();
    102             System.out.println("-----写入成功!------");
    103         } catch (Exception e) {
    104             System.out.println(e.getMessage() + e.getStackTrace());
    105             throw (e);
    106         }
    107 
    108     }
    109 
    110     public static void TangsetCellData(int RowNum, int ColNum, String Result) {
    111         try {
    112             // 获取行对象
    113             Row = ExcelWSheet.getRow(RowNum);
    114             // 如果单元格为空,则返回null
    115             Cell = Row.getCell(ColNum, Row.RETURN_BLANK_AS_NULL);
    116             if (Cell == null) {
    117                 // 当单元格对象是Null时,则创建单元格
    118                 // 如果单元格为空,无法直接调用单元格的setCellValue方法设定单元格的值
    119                 Cell = Row.createCell(RowNum);
    120                 // 调用setCellValue方法设定单元格的值
    121                 Cell.setCellValue(Result);
    122             } else {
    123                 // 单元格中有内容,则可以直接调用seCellValue方法设定单元格的值
    124                 Cell.setCellValue(Result);
    125             }
    126             // 实例化写入Excel文件的文件输出流对象
    127             FileOutputStream fileOut = new FileOutputStream(ExcelFilePath);
    128             // 将内容写入Excel中
    129             ExcelWBook.write(fileOut);
    130             fileOut.flush();
    131             fileOut.close();
    132         } catch (Exception e) {
    133             // TODO: handle exception
    134             e.printStackTrace();
    135         }
    136     }
    137 
    138     // 从excel 文件中获取测试数据的静态方法;
    139     public static Object[][] getTestData(String excelFilePath, String sheetName) throws Exception {
    140         // 根据参数传入的数据文件路径和文件名称,组合出Excel 数据文件的绝对路径
    141         // 声明一个文件;
    142         File file = new File(excelFilePath);
    143         // 创建FileInputStream 来读取Excel文件内容;
    144         FileInputStream inputStream = new FileInputStream(file);
    145         // 声明Workbook 对象;
    146         Workbook workbook = null;
    147         // 获取文件名参数的扩展名,判断是“.xlsx” 还是 “.xls” ;
    148         String fileExtensionName = excelFilePath.substring(excelFilePath.indexOf('.'));
    149         if (fileExtensionName.equals(".xlsx")) {
    150             workbook = new XSSFWorkbook(inputStream);
    151 
    152         } else if (fileExtensionName.equals(".xls")) {
    153             workbook = new HSSFWorkbook(inputStream);
    154 
    155         }
    156         Sheet sheet = workbook.getSheet(sheetName);
    157         // 获取Excel 数据文件Sheet1 中数据的行数,getLastRowNum 方法获取数据的最后一行的行号,
    158         // getFistRowNum 获取第一行 最后一行减去第一行就是总行数了
    159         // 注意excle 的行和列都是从0开始的;
    160         int rowCount = sheet.getLastRowNum() - sheet.getFirstRowNum();
    161         // 创建名为records 的List对象来存储从Excel文件中读取的数据;
    162         List<Object[]> records = new ArrayList<Object[]>();
    163         // 使用for循环遍历Excel 数据文件的所有数据(除了第一行,第一行为标题行),所以i从1开始而不是从0开始;
    164 
    165         for (int i = 1; i < rowCount + 1; i++) {
    166             // 使用getRow来获取行对象;
    167             Row row = sheet.getRow(i);
    168             /*
    169              * 声明一个数据,用来存储Excel数据文件每行中的测试用例和数据,数据的大小用getLastCellNum-2
    170              * 来进行动态声明,实现测试数据个数和数组大小一致,
    171              * 因为Excel数据文件中的测试数据行的最后一个单元格是测试执行结果,倒数第二个单元格为此测试数据行是否运行的状态位,
    172              * 所以最后俩列的单元格数据并
    173              * 不需要传入测试方法中,所以是用getLastCellNum-2的方式去掉每行中的最后俩个单元格数据,计算出需要存储的测试数据个数,
    174              * 并作为测试数据数组的初始化大小
    175              * 
    176              */
    177             String fields[] = new String[row.getLastCellNum() - 2];
    178 
    179             /*
    180              * 判断数据行是否要参与测试的执行,Excel 文件的倒数第二列为数据行的状态位, 标记为“y”
    181              * 表示此数据行要被测试脚本执行,标记为非“y”的数据行均被认为不会参数测试脚本执行,会被跳过;
    182              */
    183 
    184             if (row.getCell(row.getLastCellNum() - 2).getStringCellValue().equals("y")) {
    185                 for (int j = 0; j < row.getLastCellNum() - 2; j++) {
    186                     /*
    187                      * 判断Excel 单元格的内容是数字还是字符, 字符格式调用:
    188                      * row.getCell(j).getStringCellValue();
    189                      * 数字格式调用:row.getCell(j).getNumericCellValue();
    190                      */
    191                     fields[j] = (String) (row.getCell(j).getCellType() == XSSFCell.CELL_TYPE_STRING
    192                             ? row.getCell(j).getStringCellValue() : "" + row.getCell(j).getNumericCellValue());
    193 
    194                 }
    195                 // fields 存储到数组当中;
    196                 records.add(fields);
    197 
    198             }
    199         }
    200 
    201         /*
    202          * 定义函数的返回值,即Object[] [] 将存储测试数据的list 转换为一个Object 的二维数组;
    203          */
    204         Object[][] results = new Object[records.size()][];
    205         for (int i = 0; i < records.size(); i++) {
    206             results[i] = records.get(i);
    207         }
    208 
    209         return results;
    210 
    211     }
    212 
    213     public static int getLastColumnNum() {
    214 
    215         return ExcelWSheet.getRow(0).getLastCellNum() - 1;
    216     }
    217 
    218 
    219 
    220 
    221 }
  • 相关阅读:
    StackExchange.Redis 使用 (一)
    委托的一个小问题
    MemberwishClone学习
    类的内容学习
    类的学习
    构造函数学习
    C# 重新筑基-从头开始
    C语言入门:结构体+文件的应用
    C语言入门:结构体的概要
    C语言入门:指针的概要
  • 原文地址:https://www.cnblogs.com/linbo3168/p/7479490.html
Copyright © 2011-2022 走看看