Reputation: 11
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();
}
}
Upvotes: 1
Views: 771
Reputation: 141
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);
Upvotes: 1