java+selenium+new——POI读取excel文件——xls——xlsx结尾的文件——并进行操作

你的名字 2023-10-02 08:03 42阅读 0赞

导入和环境相匹配的包:http://archive.apache.org/dist/poi/release/bin/

6c957ed6ef957798596b0f0d754d9460.png

8d1bae1194494db8fe623fa345652cb5.png

  1. package q;
  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.io.IOException;
  5. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  6. import org.apache.poi.ss.usermodel.Cell;
  7. import org.apache.poi.ss.usermodel.Sheet;
  8. import org.apache.poi.ss.usermodel.Workbook;
  9. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  10. public class ff
  11. {
  12. public static void main(String[] args) throws IOException
  13. {
  14. String excelFilePath = "C:\\Users\\del\\Desktop\\Book1.xlsx";
  15. File file = new File(excelFilePath); //根据上面的路径文件,创建一个文件对象
  16. FileInputStream inputStream = new FileInputStream(file); //用于读取excel文件
  17. Workbook workBook = null;
  18. String fileExtensionName = excelFilePath.substring(excelFilePath.indexOf(".")); //截取文件名,获取 . 后面(包括.)的内容
  19. System.out.println(fileExtensionName); //打印内容.xlsx
  20. if ( fileExtensionName.equals(".xlsx"))
  21. {
  22. workBook = new XSSFWorkbook(inputStream);
  23. } else if ( fileExtensionName.equals(".xls") )
  24. {
  25. workBook = new HSSFWorkbook(inputStream);
  26. }
  27. Sheet Sheet = workBook.getSheet("用例"); //通过sheetName参数,生成Sheet对象
  28. System.out.println(Sheet.getFirstRowNum()); //获取数据的第一行行号 0 //excel的行号和列号都是从0 开始
  29. System.out.println(Sheet.getLastRowNum()); //获取数据的最后一行行号 6
  30. System.out.println(Sheet.getRow(0).getCell(0).getStringCellValue());
  31. System.out.println(Sheet.getRow(0).getCell(1).getStringCellValue());
  32. System.out.println(Sheet.getRow(0).getCell(2).getStringCellValue());
  33. System.out.println("------------------------------");
  34. //System.out.println(Sheet.getRow(1).getCell(0).getStringCellValue()); //此处执行会发送错误,因为是数字类型,不是文字类型
  35. System.out.println(Sheet.getRow(1).getCell(1).getStringCellValue());
  36. System.out.println(Sheet.getRow(1).getCell(2).getStringCellValue());
  37. System.out.println("------------------------------");
  38. int totalRows = Sheet.getPhysicalNumberOfRows(); //获取总行数
  39. System.out.println(totalRows); //返回总行数7
  40. System.out.println("------------------------------");
  41. int RowCells = Sheet.getRow(0).getPhysicalNumberOfCells(); //获取总列数
  42. System.out.println(RowCells); //返回总列数3
  43. System.out.println("------------------------------");
  44. System.out.println(Sheet.getRow(2).getCell(0).toString()); //表格里是2,这里返回2.0
  45. System.out.println("------------------------------");
  46. System.out.println(Sheet.getRow(2).getCell(0).getNumericCellValue()); //表格里是2,这里返回2.0
  47. // getStringCellValue
  48. //java.lang.String getStringCellValue()
  49. //以字符串形式获取单元格的值
  50. //对于数字单元格,我们抛出异常。对于空白单元格,我们返回一个空字符串。对于不是字符串公式的FormulaCells,我们抛出异常。
  51. //返回值:单元格的值作为字符串//
  52. System.out.println("------------------------------");
  53. Sheet.getRow(2).getCell(0).setCellType(Cell.CELL_TYPE_STRING); //设置单元格为string类型
  54. System.out.println(Sheet.getRow(2).getCell(0).getStringCellValue()); //返回值为2
  55. }
  56. }

执行结果:

.xlsx
0
6
测试用例编号
输入对象1
输入对象2
-——————————————-
中国
航母
-——————————————-
7
-——————————————-
3
-——————————————-
2.0
-——————————————-
2.0
-——————————————-
2

发表评论

表情:
评论列表 (有 0 条评论,42人围观)

还没有评论,来说两句吧...

相关阅读