Reputation: 11
i know how to read data from excel xls/xlxs. now my requirement is that i have to read image and data from excel file using POI 3.8. can you please guide me how to do that. reading images and data both from one excel file using poi.... Thanks in advance
Upvotes: 1
Views: 10285
Reputation: 81
XSSFSheet mySheet1 = workbook.getSheetAt(0);
Iterator<Row> rowIterator1 = mySheet1.iterator();
if(rowIterator1.hasNext())
{
rowIterator1.next();
}
int pcount=1;
Iterator<XSSFPictureData> it = lst.iterator();
for (int i=0; i<lst.size(); i++)
{
Row row = rowIterator1.next();
// PictureData pict = (PictureData)it.next();
PictureData pict = (PictureData)lst.get(i);
String ext = pict.suggestFileExtension();
byte[] data = pict.getData();
String studentName = row.getCell(3).getStringCellValue();
int age =(int)row.getCell(14).getNumericCellValue();
int year = (int)row.getCell(16).getNumericCellValue();
System.out.println(studentName+":"+age+":"+year);
FileOutputStream out = new FileOutputStream("D:\\excel\\RPC\\"+pcount+"."+year+"."+age+".png");
out.write(data);
out.close();
pcount++;
rowIterator1.hasNext();
}
}
Upvotes: -1
Reputation: 24330
Straight from the developer's guide:
List lst = workbook.getAllPictures();
for (Iterator it = lst.iterator(); it.hasNext(); ) {
PictureData pict = (PictureData)it.next();
String ext = pict.suggestFileExtension();
byte[] data = pict.getData();
if (ext.equals("jpeg")){
FileOutputStream out = new FileOutputStream("pict.jpg");
out.write(data);
out.close();
}
}
RTM :)
Upvotes: 4