`

读取excel数据到二维数组

 
阅读更多
public static void main(String[] args) {
    File file = new File("Test.xls");
    try {
    FileInputStream fis = new FileInputStream(file);
POIFSFileSystem POIStream = new POIFSFileSystem(fis);
HSSFWorkbook workBook = new HSSFWorkbook(POIStream);
HSSFSheet sheet = workBook.getSheetAt(0);

int rowSize = sheet.getLastRowNum();
HSSFRow headRow = sheet.getRow(0);
int colSize = headRow.getLastCellNum();

System.out.println("rowSize: " + rowSize);
System.out.println("colSize: " + colSize);

    List<String[]> list = new ArrayList<String[]>();
for (int rowIndex = 1; rowIndex <= rowSize; rowIndex++) {
HSSFRow row = sheet.getRow(rowIndex);

String[] values = new String[colSize - 1];
String cellValue = null;
for (int colIndex = 0; colIndex < colSize - 1; colIndex++) {
HSSFCell cell = row.getCell(colIndex);
if(cell != null){
cellValue = cell.getStringCellValue();
}else{
cellValue = "";
}
values[colIndex] = cellValue;
}
list.add(values);
}
fis.close();

Object[][] returnArray = new Object[rowSize][colSize]; 
        for (int i = 0; i < returnArray.length; i++) {   
            returnArray[i] = (Object[]) list.get(i);    
        }
       
        for (int i = 0; i < returnArray.length; i++) {
        for (int j = 0; j < returnArray[i].length; j++) {
System.out.print(returnArray[i][j] + "###    ");
}
        System.out.println();
}
       
    } catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e){
e.printStackTrace();
}
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics