官網
我們使用的是usermodel
讀取excel所有數據,並打印出來
方式一:
表單名:testcase
定義實體類(說明:這里單純打印讀取的excel內容,未用到實體類,反射的時候才會用到實體類)
package com.qzcsbj; /** * @公眾號 : 全棧測試筆記 * @博客 : www.cnblogs.com/uncleyong * @微信 : ren168632201 * @描述 : <> */ public class TestCase { private String caseId; private String describe; private String url; private String method; private String parameters; private String expect; private String actual; public String getCaseId() { return caseId; } public void setCaseId(String caseId) { this.caseId = caseId; } public String getDescribe() { return describe; } public void setDescribe(String describe) { this.describe = describe; } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } public String getMethod() { return method; } public void setMethod(String method) { this.method = method; } public String getParameters() { return parameters; } public void setParameters(String parameters) { this.parameters = parameters; } public String getExpect() { return expect; } public void setExpect(String expect) { this.expect = expect; } public String getActual() { return actual; } public void setActual(String actual) { this.actual = actual; } @Override public String toString() { return "TestCase{" + "caseId='" + caseId + '\'' + ", describe='" + describe + '\'' + ", url='" + url + '\'' + ", method='" + method + '\'' + ", parameters='" + parameters + '\'' + ", expect='" + expect + '\'' + ", actual='" + actual + '\'' + '}'; } }
讀取excel
package com.qzcsbj; import org.apache.poi.ss.usermodel.*; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.util.Arrays; /** * @公眾號 : 全棧測試筆記 * @博客 : www.cnblogs.com/uncleyong * @微信 : ren168632201 * @描述 : <> */ public class Test { public static void readExcel(String excelPath, String sheetName){ InputStream in = null; try { File file = new File(excelPath); in = new FileInputStream(file); Workbook workbook = WorkbookFactory.create(in); Sheet sheet = workbook.getSheet(sheetName); Row firstRow = sheet.getRow(0); int lastCellNum = firstRow.getLastCellNum(); String[] titles = new String[lastCellNum]; for (int i = 0; i < lastCellNum; i++) { Cell cell = firstRow.getCell(i); String title = cell.getStringCellValue(); titles[i] = title; } int lastRowNum = sheet.getLastRowNum(); for (int i = 1; i <= lastRowNum ; i++) { Row rowData = sheet.getRow(i); System.out.print("第"+i+"行數據:"); for (int j = 0; j < lastCellNum ; j++) { Cell cell = rowData.getCell(j); String cellValue = cell.getStringCellValue(); // 打印獲取到的值 System.out.print("【"+ titles[j] + "="+ cellValue+"】"); } System.out.println(); } } catch (Exception e) { e.printStackTrace(); } finally { if (in!=null){ try { in.close(); } catch (IOException e) { e.printStackTrace(); } } } } public static void main(String[] args) { readExcel("E:\\case.xlsx","testcase"); } }
結果:
方式二:
表單名:testcase2
package com.qzcsbj; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.DataFormatter; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.testng.annotations.DataProvider; import org.testng.annotations.Test; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.List; /** * @公眾號 : 全棧測試筆記 * @博客 : www.cnblogs.com/uncleyong * @微信 : ren168632201 * @描述 : <> */ public class ReadExcel { public static void readExcel(String excelPath, String sheetName){ InputStream in = null; DataFormatter dataFormatter = new DataFormatter(); File file = new File(excelPath); try { in = new FileInputStream(file); XSSFWorkbook workbook = new XSSFWorkbook(in); XSSFSheet sheet = workbook.getSheet(sheetName); // 首行 Row header = sheet.getRow(0); // 首行所有列 Iterator<Cell> headerCells = header.iterator(); while (headerCells.hasNext()){ Cell cell = headerCells.next(); System.out.println("行索引:" + cell.getRowIndex()+",列索引:" + cell.getColumnIndex() + ",當前單元格的值:" + dataFormatter.formatCellValue(cell)); } // 所有行 Iterator<Row> rowIterator = sheet.rowIterator(); boolean isFirstRow = true; while (rowIterator.hasNext()){ Row row = rowIterator.next(); // 忽略首行 if (isFirstRow){ isFirstRow = false; continue; } HashMap<String, String> rowMap = new HashMap<>(); // 獲取當前行所有列 Iterator<Cell> cells = row.cellIterator(); while (cells.hasNext()){ // 當前列 Cell cell = cells.next(); System.out.println("行索引:" + cell.getRowIndex()+",列索引:" + cell.getColumnIndex() + ",當前單元格的值:" + dataFormatter.formatCellValue(cell)); rowMap.put(dataFormatter.formatCellValue(header.getCell(cell.getColumnIndex())),dataFormatter.formatCellValue(cell)); } } } catch (IOException e) { e.printStackTrace(); } finally { // 關閉流 if (in != null){ try { in.close(); } catch (IOException e) { e.printStackTrace(); } } } } public static void main(String[] args) { String excelPath = "E:\\case.xlsx"; String sheetName = "testcase2"; readExcel(excelPath, sheetName); } }
原文會持續更新,原文地址:https://www.cnblogs.com/uncleyong/p/15867741.html