如何使用Apache POI读取Excel的数据并将数据存储到字符串[] []数组中



我想使用Apache POI读取Excel的数据,然后将数据存储到2Dimential的字符串数组中。使用以下代码我将显示数据,但我想存储数据。

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 do

String s = new String(bytes);

最新更新