簡體   English   中英

如何使用apache poi從excel讀取數據並將數據存儲到String [] []數組中?

[英]How to read data from excel using apache poi and store the data into String[][] array?

我想使用apache poi從excel讀取數據並將該數據存儲到2Dimentional String Array中。 使用下面的代碼,我將顯示數據,但我想存儲數據。

public static void main(String[] args) throws Exception {

    File f = new File("C:/Users/SYKAMREDDY/Desktop/testData.xls");
    FileInputStream fis = new FileInputStream(f);

    HSSFWorkbook wb = new HSSFWorkbook(fis);
    HSSFSheet sh = wb.getSheet("Data");
    int rc=sh.getLastRowNum()-sh.getFirstRowNum();

    for (int i = 1; i < rc; i++) {
        Row r = sh.getRow(i);
        for (int j = 1; j < r.getLastCellNum(); j++) {
            String s = r.getCell(j).getStringCellValue();
            System.out.print(s+" ");
        }
        System.out.println();
    }
}

嘗試使用byteArray

簡化示例:

ByteArrayOutputStream bos = new ByteArrayOutputStream();
try {
    workbook.write(bos);
} finally {
    bos.close();
}
byte[] bytes = bos.toByteArray();

另外,看看如何將POI HSSFWorkbook轉換為字節?

如果你想使用字符串,simpy呢

String s = new String(bytes);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM