說明:
1.使用Exce作為數據存放地;
2.使用TestNG的Datarprovide 做數據供應;
3.不足的地方沒有指定明確的result_code , error_code , ERROR_MSG ,如果知道明確規定error_code就可以直接用error_code來作為測試結果;
4.代碼有許多需要改動的地方本次是第一版;
5.可以將整個小項目打成jar包執行,將excle的文件放入C盤根目錄即可(畢竟每個電腦都有C盤,這樣就不受機器的限制了)
6.關於本次用到的jar包有
一,讀取EXCLE的相關代碼

1 package main.java; 2 3 import org.apache.poi.ss.usermodel.*; 4 5 import java.io.File; 6 import java.io.FileInputStream; 7 import java.io.IOException; 8 import java.util.ArrayList; 9 import java.util.HashMap; 10 import java.util.List; 11 import java.util.Map; 12 13 public class ExcelReader { 14 private String filePath; 15 private String sheetName; 16 private Workbook workBook; 17 private Sheet sheet; 18 private List<String> columnHeaderList; 19 private List<List<String>> listData; 20 private List<Map<String, String>> mapData; 21 private boolean flag; 22 public Object[][] results; 23 24 public ExcelReader(String filePath, String sheetName) { 25 this.filePath = filePath; 26 this.sheetName = sheetName; 27 this.flag = false; 28 this.load(); 29 } 30 31 private void load() { 32 FileInputStream inStream = null; 33 try { 34 inStream = new FileInputStream(new File(filePath)); 35 workBook = WorkbookFactory.create(inStream); 36 sheet = workBook.getSheet(sheetName); 37 } catch (Exception e) { 38 e.printStackTrace(); 39 } finally { 40 try { 41 if (inStream != null) { 42 inStream.close(); 43 } 44 } catch (IOException e) { 45 e.printStackTrace(); 46 } 47 } 48 } 49 50 private String getCellValue(Cell cell) { 51 String cellValue = ""; 52 DataFormatter formatter = new DataFormatter(); 53 if (cell != null) { 54 switch (cell.getCellType()) { 55 case Cell.CELL_TYPE_NUMERIC: 56 if (DateUtil.isCellDateFormatted(cell)) { 57 cellValue = formatter.formatCellValue(cell); 58 } else { 59 double value = cell.getNumericCellValue(); 60 int intValue = (int) value; 61 cellValue = value - intValue == 0 ? String.valueOf(intValue) : String.valueOf(value); 62 } 63 break; 64 case Cell.CELL_TYPE_STRING: 65 cellValue = cell.getStringCellValue(); 66 break; 67 case Cell.CELL_TYPE_BOOLEAN: 68 cellValue = String.valueOf(cell.getBooleanCellValue()); 69 break; 70 case Cell.CELL_TYPE_FORMULA: 71 cellValue = String.valueOf(cell.getCellFormula()); 72 break; 73 case Cell.CELL_TYPE_BLANK: 74 cellValue = ""; 75 break; 76 case Cell.CELL_TYPE_ERROR: 77 cellValue = ""; 78 break; 79 default: 80 cellValue = cell.toString().trim(); 81 break; 82 } 83 } 84 return cellValue.trim(); 85 } 86 87 private void getSheetData() { 88 89 listData = new ArrayList<>(); 90 mapData = new ArrayList<>(); 91 columnHeaderList = new ArrayList<>(); 92 int numOfRows = sheet.getLastRowNum() + 1; 93 for (int i = 0; i < numOfRows; i++) { 94 Row row = sheet.getRow(i); 95 Map<String, String> map = new HashMap<>(); 96 List<String> list = new ArrayList<>(); 97 98 if (row != null) { 99 for (int j = 0; j < row.getLastCellNum(); j++) { 100 Cell cell = row.getCell(j); 101 if (i == 0) { 102 columnHeaderList.add(getCellValue(cell)); 103 } else { 104 105 map.put(columnHeaderList.get(j), this.getCellValue(cell)); 106 107 } 108 list.add(this.getCellValue(cell)); 109 } 110 } 111 if (i > 0) { 112 mapData.add(map); 113 } 114 listData.add(list); 115 116 117 } 118 119 flag = true; 120 121 for (int i = 0; i < listData.size(); i++) { 122 for (int j = 0; j < listData.get(i).size(); j++) { 123 System.out.println(listData.get(i).get(j).toString()); 124 } 125 } 126 127 } 128 129 public String getCellData(int row, int col) { 130 if (row <= 0 || col <= 0) { 131 return null; 132 } 133 if (!flag) { 134 this.getSheetData(); 135 } 136 if (listData.size() >= row && listData.get(row - 1).size() >= col) { 137 return listData.get(row - 1).get(col - 1); 138 } else { 139 return null; 140 } 141 } 142 143 public String getCellData(int row, String headerName) { 144 if (row <= 0) { 145 return null; 146 } 147 if (!flag) { 148 this.getSheetData(); 149 } 150 if (mapData.size() >= row && mapData.get(row - 1).containsKey(headerName)) { 151 return mapData.get(row - 1).get(headerName); 152 } else { 153 return null; 154 } 155 } 156 157 158 public Object[][] getSheetData2() { 159 160 List<Object[]> result = new ArrayList<>(); 161 listData = new ArrayList<>(); 162 mapData = new ArrayList<>(); 163 columnHeaderList = new ArrayList<>(); 164 165 int numOfRows = sheet.getLastRowNum() + 1; 166 System.out.println("總共有 " + numOfRows + "行 !"); 167 for (int i = 0; i < numOfRows; i++) { 168 Row row = sheet.getRow(i); 169 Map<String, String> map = new HashMap<>(); 170 List<String> list = new ArrayList<>(); 171 Object[] o1 = new Object[row.getLastCellNum()]; 172 173 if (row != null) { 174 for (int j = 0; j < row.getLastCellNum(); j++) { 175 // System.out.println("第 "+i+" 行--- row.getLastCellNum()===="+row.getLastCellNum()); 176 Cell cell = row.getCell(j); 177 if (i == 0) { 178 o1[j] = this.getCellValue(cell); 179 // System.out.println(j+"------this.getCellValue(cell)="+this.getCellValue(cell)); 180 columnHeaderList.add(getCellValue(cell)); 181 } else { 182 o1[j] = this.getCellValue(cell); 183 // System.out.println(j+"------this.getCellValue(cell)="+this.getCellValue(cell)); 184 map.put(columnHeaderList.get(j), this.getCellValue(cell)); 185 186 } 187 list.add(this.getCellValue(cell)); 188 } 189 } 190 if (i > 0) { 191 mapData.add(map); 192 } 193 result.add(o1); 194 listData.add(list); 195 } 196 // 測試數據excel數據用 ; 197 /* for (int i = 0; i < result.size(); i++) { 198 for (int j = 0; j < result.get(i).length; j++) { 199 System.out.print(result.get(i)[j]+" | "); 200 } 201 System.out.println(); 202 }*/ 203 results = new Object[result.size()][]; 204 205 for (int i = 0; i < result.size(); i++) { 206 results[i] = result.get(i); 207 } 208 flag = true; 209 210 System.out.println("results.length==" + results.length); 211 return results; 212 } 213 214 public static void main(String[] args) { 215 /* Object[][] obj1; 216 ExcelReader eh = new ExcelReader("C:\\TEST.xlsx", "Sheet1"); 217 Object[][] sheetData2 = eh.getSheetData2(); 218 System.out.println(sheetData2.length + "------------"); 219 for (int i = 1; i < sheetData2.length; i++) { 220 for (int j = 0; j < sheetData2[i].length; j++) { 221 System.out.print(sheetData2[i][j] + " | "); 222 } 223 System.out.println(); 224 }*/ 225 226 227 } 228 }
二,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 }
三,接口get和post方法

1 package main.java; 2 3 import net.sf.json.JSON; 4 import net.sf.json.JSONObject; 5 6 import java.io.BufferedReader; 7 import java.io.IOException; 8 import java.io.InputStreamReader; 9 import java.io.PrintWriter; 10 import java.net.URL; 11 import java.net.URLConnection; 12 import java.util.List; 13 import java.util.Map; 14 15 /** 16 * Created by ty on 2017/8/17. 17 */ 18 public class HttpInterfaceTest { 19 20 21 public String sendGet(String url, String param) { 22 String result = ""; 23 BufferedReader in = null; 24 try { 25 String urlName = url + "?" + param; 26 System.out.println("Get請求接口:" + urlName); 27 URL realUrl = new URL(urlName); 28 // 打開和URL之間的連接 29 URLConnection conn = realUrl.openConnection(); 30 // 設置通用的請求屬性 31 conn.setRequestProperty("accept", "*/*"); 32 conn.setRequestProperty("connection", "Keep-Alive"); 33 conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)"); 34 // 建立實際的連接 35 conn.connect(); 36 // 獲取所有響應頭字段 37 Map<String, List<String>> map = conn.getHeaderFields(); 38 // 遍歷所有的響應頭字段 39 for (String key : map.keySet()) { 40 System.out.println(key + "--->" + map.get(key)); 41 } 42 // 定義BufferedReader輸入流來讀取URL的響應 43 in = new BufferedReader(new InputStreamReader(conn.getInputStream())); 44 String line; 45 while ((line = in.readLine()) != null) { 46 result += "\n" + line; 47 } 48 } catch (Exception e) { 49 System.out.println("發送GET請求出現異常!" + e); 50 e.printStackTrace(); 51 } 52 // 使用finally塊來關閉輸入流 53 finally { 54 try { 55 if (in != null) { 56 in.close(); 57 } 58 } catch (IOException ex) { 59 ex.printStackTrace(); 60 } 61 } 62 return result; 63 } 64 65 /** 66 * 向指定URL發送POST方法的請求 67 * 68 * @param url 發送請求的URL 69 * @param param 請求參數,請求參數應該是name1=value1&name2=value2的形式或者是json。 70 * @return URL所代表遠程資源的響應 71 */ 72 public String sendPost(String url, String param) { 73 PrintWriter out = null; 74 BufferedReader in = null; 75 String result = ""; 76 try { 77 URL realUrl = new URL(url); 78 // 打開和URL之間的連接 79 URLConnection conn = realUrl.openConnection(); 80 // 設置通用的請求屬性 81 conn.setRequestProperty("accept", "*/*"); 82 conn.setRequestProperty("connection", "Keep-Alive"); 83 conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)"); 84 conn.setRequestProperty("Content-Type", "application/json; charset=utf-8"); 85 // 發送POST請求必須設置如下兩行 86 conn.setDoOutput(true); 87 conn.setDoInput(true); 88 89 // 獲取URLConnection對象對應的輸出流 90 out = new PrintWriter(conn.getOutputStream()); 91 // 發送請求參數 92 out.print(param); 93 // flush輸出流的緩沖 94 out.flush(); 95 // 定義BufferedReader輸入流來讀取URL的響應 96 in = new BufferedReader(new InputStreamReader(conn.getInputStream())); 97 String line; 98 while ((line = in.readLine()) != null) { 99 result += "\n" + line; 100 } 101 } catch (Exception e) { 102 System.out.println("發送POST請求出現異常!" + e); 103 e.printStackTrace(); 104 } 105 // 使用finally塊來關閉輸出流、輸入流 106 finally { 107 try { 108 if (out != null) { 109 out.close(); 110 } 111 if (in != null) { 112 in.close(); 113 } 114 } catch (IOException ex) { 115 ex.printStackTrace(); 116 } 117 } 118 return result; 119 } 120 121 122 public static void main(String[] args) { 123 124 HttpInterfaceTest httpInterfaceTest = new HttpInterfaceTest(); 125 126 // 調用天氣預報接口請求參數方式一 127 String postUrl = "http://op.juhe.cn/onebox/weather/query"; 128 String postParamsOne = "&cityname=上海市" + "&key=1234567890"; 129 // 調用天氣預報接口請求參數方式二 130 String postParamsTwo = "{'cityname':'上海市'," + "'key':'1234567890'}"; 131 JSONObject jsonPostParamsTwo = JSONObject.fromObject(postParamsTwo); 132 System.out.println("----------------"); 133 // 發送POST請求 134 String postResultOne = httpInterfaceTest.sendPost(postUrl, postParamsOne); 135 System.out.println("POST請求參數一:" + postParamsOne); 136 System.out.println("POST請求響應結果:" + postResultOne); 137 // 發送POST請求 138 String postResultTwo = httpInterfaceTest.sendPost(postUrl, jsonPostParamsTwo.toString()); 139 System.out.println("POST請求參數二:" + jsonPostParamsTwo); 140 System.out.println("POST請求響應結果:" + postResultTwo); 141 142 JSONObject jsonObject = JSONObject.fromObject(postResultTwo); 143 Object resultcode = jsonObject.get("resultcode"); 144 Object reason = jsonObject.get("reason"); 145 Object error_code = jsonObject.get("error_code"); 146 System.out.println("resultcode==" + resultcode+ 147 "| reason="+reason+" | error_code= "+error_code); 148 } 149 }
四,測試類

1 package main.test; 2 3 import main.java.ExcelReader; 4 import main.java.ExcleUtil; 5 import main.java.HttpInterfaceTest; 6 import net.sf.json.JSONException; 7 import net.sf.json.JSONObject; 8 import org.testng.annotations.BeforeTest; 9 import org.testng.annotations.DataProvider; 10 import org.testng.annotations.Test; 11 12 import static org.testng.Assert.*; 13 14 /** 15 * Created by linbo.yang on 2017/9/5. 16 */ 17 public class TestHttpInterfaceTest { 18 public static HttpInterfaceTest ht ; 19 ExcelReader ex ; 20 static ExcleUtil excleUtil; 21 @BeforeTest 22 public void init(){ 23 String ExcelFilePath="C:\\TEST.xlsx"; 24 String sheetName="Sheet1"; 25 ht=new HttpInterfaceTest(); 26 ex = new ExcelReader(ExcelFilePath, sheetName); 27 try { 28 excleUtil.setExcleFile( ExcelFilePath,sheetName); 29 } catch (Exception e) { 30 e.printStackTrace(); 31 } 32 } 33 34 @Test(dataProvider = "dp") 35 public void testSendPost(String rowNum,String Url,String paras) throws Exception { 36 System.out.println("rowNum="+rowNum+"; URL="+Url+" ; paras="+paras); 37 Integer it = new Integer(rowNum); 38 int row=it.intValue(); 39 if (paras.contains("&")){ 40 String s1 = ht.sendPost(Url,paras ); 41 excleUtil.setCellData(row,3,s1); 42 System.out.println(s1); 43 }else { 44 try { 45 JSONObject jsonObject = JSONObject.fromObject(paras); 46 String s = ht.sendPost(Url, jsonObject.toString()); 47 excleUtil.setCellData(row,3,s); 48 System.out.println(s); 49 }catch (JSONException jsonException){ 50 51 System.out.println("標題行不能進行轉換!"); 52 } 53 54 } 55 56 57 } 58 @DataProvider 59 public Object[][] dp(){ 60 Object[][] sheetData2 = ex.getSheetData2(); 61 /* System.out.println(sheetData2.length + "------------"); 62 for (int i = 1; i < sheetData2.length; i++) { 63 for (int j = 0; j < sheetData2[i].length; j++) { 64 System.out.print(sheetData2[i][j] + " | "); 65 } 66 System.out.println(); 67 }*/ 68 69 70 return sheetData2 ; 71 72 } 73 74 }
五,日期工具類可以將測試的時間寫入到excle中去

1 package main.java; 2 3 import java.util.*; 4 public class DateUtil { 5 public static String format="yyyy/MM/dd HH:mm:ss"; 6 public static Date date = new Date(); 7 /* 8 * 格式化輸出日期 ; 9 * 10 * @return 一個字符型日期; 11 */ 12 public static String format(Date date ,String format) { 13 String result = ""; 14 try { 15 if (date != null) { 16 java.text.DateFormat df = new java.text.SimpleDateFormat(format); 17 result = df.format(date); 18 19 } 20 } catch (Exception e) { 21 e.printStackTrace(); 22 } 23 return result; 24 } 25 public static String format() { 26 String result = ""; 27 try { 28 if (date != null) { 29 java.text.DateFormat df = new java.text.SimpleDateFormat(format); 30 result = df.format(date); 31 32 } 33 } catch (Exception e) { 34 e.printStackTrace(); 35 } 36 return result; 37 } 38 39 40 public String getYear() { 41 java.util.Calendar c= java.util.Calendar.getInstance(); 42 c.setTime(date); 43 return String.valueOf(c.get(java.util.Calendar.YEAR)) ; 44 } 45 46 //返回年份 ; 47 public static String getYear(java.util.Date date){ 48 java.util.Calendar c= java.util.Calendar.getInstance(); 49 c.setTime(date); 50 return String.valueOf(c.get(java.util.Calendar.YEAR)) ; 51 } 52 //返回月份 ; 53 public static String getMonth(){ 54 java.util.Calendar calendar =java.util.Calendar.getInstance(); 55 calendar.setTime(date); 56 return String.valueOf(calendar.get(java.util.Calendar.MONTH)+1); 57 } 58 public static String getMonth(java.util.Date date){ 59 java.util.Calendar calendar =java.util.Calendar.getInstance(); 60 calendar.setTime(date); 61 return String.valueOf(calendar.get(java.util.Calendar.MONTH)+1); 62 } 63 //返回月份中的第幾天; 64 65 public static String getDay(){ 66 java.util.Calendar calendar =java.util.Calendar.getInstance(); 67 calendar.setTime(date); 68 return String.valueOf(calendar.get(java.util.Calendar.DAY_OF_MONTH)); 69 } 70 public static String getDay(java.util.Date date){ 71 java.util.Calendar calendar =java.util.Calendar.getInstance(); 72 calendar.setTime(date); 73 return String.valueOf(calendar.get(java.util.Calendar.DAY_OF_MONTH)); 74 } 75 //返回小時; 76 public static String getHour(){ 77 Calendar calendar=Calendar.getInstance(); 78 calendar.setTime(date); 79 return String.valueOf(calendar.get(Calendar.HOUR)); 80 } 81 public static String getHour(Date date){ 82 Calendar calendar=Calendar.getInstance(); 83 calendar.setTime(date); 84 return String.valueOf(calendar.get(Calendar.HOUR)); 85 } 86 //返回分鍾 ; 87 public static String getMinute( ){ 88 Calendar calendar = Calendar.getInstance(); 89 calendar.setTime(date); 90 return String.valueOf(calendar.get(Calendar.MINUTE)); 91 } 92 public static String getMinute(Date date ){ 93 Calendar calendar = Calendar.getInstance(); 94 calendar.setTime(date); 95 return String.valueOf(calendar.get(Calendar.MINUTE)); 96 } 97 //返回秒 98 public static String getSecond( ){ 99 Calendar calendar = Calendar.getInstance(); 100 calendar.setTime(date); 101 return String.valueOf(calendar.get(Calendar.SECOND)); 102 } 103 public static String getSecond(Date date ){ 104 Calendar calendar = Calendar.getInstance(); 105 calendar.setTime(date); 106 return String.valueOf(calendar.get(Calendar.SECOND)); 107 } 108 }
六,執行結果