1. 读取
//读取excel指定sheet中的各行数据,存入二维数组,包括首行 public static String[][] getSheetData(XSSFSheet sheet) throws IOException { String[][] testArray = new String[sheet.getPhysicalNumberOfRows()][]; for(int rowId =0;rowId<sheet.getPhysicalNumberOfRows();rowId++){ XSSFRow row = sheet.getRow(rowId); List<String> testSetList = new ArrayList<String>(); for(int column=0;column<row.getPhysicalNumberOfCells();column++){ row.getCell(column).setCellType(Cell.CELL_TYPE_STRING); testSetList.add(row.getCell(column).getStringCellValue()); } testArray[rowId] = (String[])testSetList. toArray(new String[testSetList.size()]); } return testArray; }
2. 打印
//打印二维数组 public static void printDoubleArray(String[][] testArray) throws IOException{ for(int i =0; i<testArray.length;i++ ) { for (int j=0; j<testArray[i].length;j++) { print(testArray[i][j]+" ||"); } println(); } }
3. 调用
public static void main(String[] args) throws IOException { // TODO 自动生成的方法存根 File file = new File("testData\\testData.xlsx"); FileInputStream fis = new FileInputStream(file); @SuppressWarnings("resource") XSSFWorkbook wb = new XSSFWorkbook(new BufferedInputStream(fis)); printDoubleArray(getSheetData(wb.getSheetAt(0))); }
4. 效果