1

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();
    }
}
1
  • what is the issue you're facing? is the data being read successfully if yes what do you wish to achieve? Commented Apr 19, 2017 at 15:56

1 Answer 1

1

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);
Sign up to request clarification or add additional context in comments.

1 Comment

As far as I understand the question title, the questioner wants to store the cell data in a string[][] array. You are proposing to save the workbook in a byte[] array. So this is not an answer to the question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.