1 package FileDemo1; 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 public class ExcleUtil { 19 private static XSSFSheet ExcelWSheet; 20 private static XSSFWorkbook ExcelWBook; 21 private static XSSFCell Cell; 22 private static XSSFRow Row; 23 24 // 設定要設置的Excel的文件路徑和Excel 中Sheet名; 25 // 在讀/寫Excel 的時候先要調用此方法 26 public static void setExcleFile(String FilePath, String sheetName) throws Exception { 27 FileInputStream ExcleFile; 28 try { 29 // 實例化Excle文件的FileInputStream 對象; 30 ExcleFile = new FileInputStream(FilePath); 31 // 實例化Excle文件的XSSFWorkbook 對象; 32 ExcelWBook = new XSSFWorkbook(ExcleFile); 33 /* 34 * 實例化XSSFSheet 對象,指定ExcelFile中的sheet名稱,用於后續對sheet中行和列的操作; 35 * 36 */ 37 ExcelWSheet = ExcelWBook.getSheet(sheetName); 38 39 } catch (Exception e) { 40 e.getStackTrace(); 41 } 42 } 43 /* 44 * 讀取excle文件指定單元格的函數 ; 45 * 46 */ 47 48 public static String getCell(int row, int col) throws Exception { 49 50 try { 51 // 通過函數參數指定單元格的行號和列,獲取指定單元格的對象; 52 Cell = ExcelWSheet.getRow(row).getCell(col); 53 /* 54 * 1.如果單元格的類型為字符串類型,使用getStringCellValue();來獲取單元格的內容; 55 * 2.如果單元格的類型為數字類型,使用getNumberricCellValue();來獲取單元格的內容; 56 * 注意:getNumberricCellValue();返回的值為double類型,轉為為字符串類型,必須在 57 * getNumberricCellValue();前面加上(" ")雙引號,用於強制轉換為String類型,不加雙引號 58 * 則會拋錯;double類型無法轉換為String類型的異常; 59 * 60 */ 61 String CellData = Cell.getCellType() == XSSFCell.CELL_TYPE_STRING ? Cell.getStringCellValue() + "" 62 : String.valueOf(Math.round(Cell.getNumericCellValue())); 63 return CellData; 64 } catch (Exception e) { 65 e.getStackTrace(); 66 return ""; 67 } 68 69 } 70 /* 71 * 在Excle中執行單元格寫入數據; 72 * 73 * 74 */ 75 76 public static void setCellData(int rownum, int colnum, String Result) throws Exception { 77 78 try { 79 // 獲取excle文件的中行對象; 80 Row = ExcelWSheet.getRow(rownum); 81 // 如果單元格為空則返回null; 82 Cell = Row.getCell(colnum, org.apache.poi.ss.usermodel.Row.RETURN_BLANK_AS_NULL); 83 if (Cell == null) { 84 // 當單元格為空是則創建單元格 85 // 如果單元格為空無法調用單元格對象的setCellValue方法設定單元格的值 ; 86 Cell = Row.createCell(colnum); 87 // 創建單元格和后可以通過調用單元格對象的setCellValue方法設置單元格的值了; 88 Cell.setCellValue(Result); 89 } else { 90 // 單元格中有內容,則可以直接調用單元格對象的 setCellValue 方法來設置單元格的值; 91 Cell.setCellValue(Result); 92 } 93 FileOutputStream fileout = new FileOutputStream(Constant.testDataExcelFilePath); 94 // 將內容寫到Excel文件中 ; 95 ExcelWBook.write(fileout); 96 // j調用flush方法強制刷新寫入文件; 97 fileout.flush(); 98 fileout.close(); 99 100 } catch (Exception e) { 101 System.out.println(e.getMessage() + e.getStackTrace()); 102 throw (e); 103 } 104 105 } 106 107 public static void TangsetCellData(int RowNum, int ColNum, String Result) { 108 try { 109 // 獲取行對象 110 Row = ExcelWSheet.getRow(RowNum); 111 // 如果單元格為空,則返回null 112 Cell = Row.getCell(ColNum, Row.RETURN_BLANK_AS_NULL); 113 if (Cell == null) { 114 // 當單元格對象是Null時,則創建單元格 115 // 如果單元格為空,無法直接調用單元格的setCellValue方法設定單元格的值 116 Cell = Row.createCell(RowNum); 117 // 調用setCellValue方法設定單元格的值 118 Cell.setCellValue(Result); 119 } else { 120 // 單元格中有內容,則可以直接調用seCellValue方法設定單元格的值 121 Cell.setCellValue(Result); 122 } 123 // 實例化寫入Excel文件的文件輸出流對象 124 FileOutputStream fileOut = new FileOutputStream(Constant.testDataExcelFilePath); 125 // 將內容寫入Excel中 126 ExcelWBook.write(fileOut); 127 fileOut.flush(); 128 fileOut.close(); 129 } catch (Exception e) { 130 // TODO: handle exception 131 e.printStackTrace(); 132 } 133 } 134 135 // 從excel 文件中獲取測試數據的靜態方法; 136 public static Object[][] getTestData(String excelFilePath, String sheetName) throws Exception { 137 // 根據參數傳入的數據文件路徑和文件名稱,組合出Excel 數據文件的絕對路徑 138 // 聲明一個文件; 139 File file = new File(excelFilePath); 140 // 創建FileInputStream 來讀取Excel文件內容; 141 FileInputStream inputStream = new FileInputStream(file); 142 // 聲明Workbook 對象; 143 Workbook workbook = null; 144 // 獲取文件名參數的擴展名,判斷是“.xlsx” 還是 “.xls” ; 145 String fileExtensionName = excelFilePath.substring(excelFilePath.indexOf('.')); 146 if (fileExtensionName.equals(".xlsx")) { 147 workbook = new XSSFWorkbook(inputStream); 148 149 } else if (fileExtensionName.equals(".xls")) { 150 workbook = new HSSFWorkbook(inputStream); 151 152 } 153 Sheet sheet = workbook.getSheet(sheetName); 154 // 獲取Excel 數據文件Sheet1 中數據的行數,getLastRowNum 方法獲取數據的最后一行的行號, 155 // getFistRowNum 獲取第一行 最后一行減去第一行就是總行數了 156 // 注意excle 的行和列都是從0開始的; 157 int rowCount = sheet.getLastRowNum() - sheet.getFirstRowNum(); 158 // 創建名為records 的List對象來存儲從Excel文件中讀取的數據; 159 List<Object[]> records = new ArrayList<Object[]>(); 160 // 使用for循環遍歷Excel 數據文件的所有數據(除了第一行,第一行為標題行),所以i從1開始而不是從0開始; 161 162 for (int i = 1; i < rowCount + 1; i++) { 163 // 使用getRow來獲取行對象; 164 Row row = sheet.getRow(i); 165 /* 166 * 聲明一個數據,用來存儲Excel數據文件每行中的測試用例和數據,數據的大小用getLastCellNum-2 167 * 來進行動態聲明,實現測試數據個數和數組大小一致, 168 * 因為Excel數據文件中的測試數據行的最后一個單元格是測試執行結果,倒數第二個單元格為此測試數據行是否運行的狀態位, 169 * 所以最后倆列的單元格數據並 170 * 不需要傳入測試方法中,所以是用getLastCellNum-2的方式去掉每行中的最后倆個單元格數據,計算出需要存儲的測試數據個數, 171 * 並作為測試數據數組的初始化大小 172 * 173 */ 174 String fields[] = new String[row.getLastCellNum() - 2]; 175 176 /* 177 * 判斷數據行是否要參與測試的執行,Excel 文件的倒數第二列為數據行的狀態位, 標記為“y” 178 * 表示此數據行要被測試腳本執行,標記為非“y”的數據行均被認為不會參數測試腳本執行,會被跳過; 179 */ 180 181 if (row.getCell(row.getLastCellNum() - 2).getStringCellValue().equals("y")) { 182 for (int j = 0; j < row.getLastCellNum() - 2; j++) { 183 /* 184 * 判斷Excel 單元格的內容是數字還是字符, 字符格式調用: 185 * row.getCell(j).getStringCellValue(); 186 * 數字格式調用:row.getCell(j).getNumericCellValue(); 187 */ 188 fields[j] = (String) (row.getCell(j).getCellType() == XSSFCell.CELL_TYPE_STRING 189 ? row.getCell(j).getStringCellValue() : "" + row.getCell(j).getNumericCellValue()); 190 191 } 192 // fields 存儲到數組當中; 193 records.add(fields); 194 195 } 196 } 197 198 /* 199 * 定義函數的返回值,即Object[] [] 將存儲測試數據的list 轉換為一個Object 的二維數組; 200 */ 201 Object[][] results = new Object[records.size()][]; 202 for (int i = 0; i < records.size(); i++) { 203 results[i] = records.get(i); 204 } 205 206 return results; 207 208 } 209 210 public static int getLastColumnNum() { 211 212 return ExcelWSheet.getRow(0).getLastCellNum() - 1; 213 } 214 215 }