简体   繁体   中英

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

I want to read data from excel using apache poi and store that data into 2Dimentional String Array. Using below code I will display data but I want to store the data.

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();
    }
}

Try to use byteArray

simplified example:

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

also, take a look at How can I convert POI HSSFWorkbook to bytes?

if you want to use string , simpy do

String s = new String(bytes);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM