使用poi从excel文件中读取图像和数据

时间:2012-12-01 09:59:26

标签: java apache-poi

我知道如何从excel xls / xlxs读取数据。现在我的要求是我必须使用POI 3.8从excel文件中读取图像和数据。你能指导我怎么做吗? 使用poi从一个excel文件中读取图像和数据....提前感谢

2 个答案:

答案 0 :(得分:2)

直接来自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:)

答案 1 :(得分:-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();

    }
}