1

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

GaryF
  • 23,950
  • 10
  • 60
  • 73

2 Answers2

4

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 :)

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
GaryF
  • 23,950
  • 10
  • 60
  • 73
  • thanks but its only for picture i want to read other data from file as well like i have file in which i have a picture of a person and its name,email,address and phone num and i have 100 record like dis so i want to read picture and the remaining data as well workbook.getAllPictures() will give me all the pictures – Khawaja Shoaib Dec 01 '12 at 10:46
  • I'd suggest reading the developers guide and then, if you've done that, posting what you've already tried. – GaryF Dec 01 '12 at 10:51
  • i already did . and i have tried this code as well its work but my requirement is to read data and picture as well. i think you are not getting my point – Khawaja Shoaib Dec 01 '12 at 10:54
  • I absolutely understand what you're trying to do; but I'm not going to hand you an answer. If you show what you've tried so far to solve the problem on your own, I'll help tell you what's wrong with what you've tried. This site is not here to do your work for you, it's to help you when you get stuck and can show that you've made an effort. Like I say, show what you've tried so far and I'll help you fix it. – GaryF Dec 01 '12 at 11:04
  • i exactly know what this platform is i only need a clue to start graping the data and picture as well – Khawaja Shoaib Dec 01 '12 at 11:35
  • 2
    I understand that's what you want. Show the code that you have so far, and I'll help you fix it. Don't show code, and I can't help. – GaryF Dec 01 '12 at 12:28
-1
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();

    }
}
CDspace
  • 2,639
  • 18
  • 30
  • 36
praveen
  • 71
  • 1
  • 1