使用JavaScript

时间:2016-01-27 08:52:06

标签: java excel apache-poi

我的问题是在使用java脚本在excel中提取图像时,图像会在某处交换。[![在此处输入图像描述] [1]] [1]

使用java代码我可以这样做 这里

a94行 - 保存图片94
 a95行 - 保存图片95
 a96行 - 保存图片96

我想像波纹管格式一样保存

a94行 - 保存图片94
 a95行 - 保存图片96
 a96行 - 保存图片95

我想用各自的单元保存每个图像。 请给我一些想法。我必须更改我的java代码。
我的代码是

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;

import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import org.apache.poi.ss.usermodel.PictureData;

public class ReadWriteExcelFile1 {

    public static void readXLSFile() throws IOException
    {
        InputStream ExcelFileToRead = new FileInputStream("E://Eclips//ExcelExtract1.xls");
        HSSFWorkbook wb = new HSSFWorkbook(ExcelFileToRead);

        HSSFSheet sheet=wb.getSheetAt(0);
        HSSFRow row; 
        HSSFCell cell;

        Iterator rows = sheet.rowIterator();

        ///
        row=(HSSFRow) rows.next();
        List lst = wb.getAllPictures();
        int i=1;
        for (Iterator it = lst.iterator(); it.hasNext(); ) {
            PictureData pict = (PictureData)it.next();
            String ext = pict.suggestFileExtension();
            byte[] data = pict.getData();
            if (ext.equals("jpeg") || ext.equals("jpg") || ext.equals("png")) {
                row=(HSSFRow) rows.next();
                Iterator cells = row.cellIterator();
                cell=(HSSFCell) cells.next();
                //if(i<=100)
                //{
                System.out.print("==>"+cell.getStringCellValue()+" == "+cell.getStringCellValue()+"."+ext+"\n");
                System.out.println(row);
              FileOutputStream out = new FileOutputStream("E://Eclips//Output//"+cell.getStringCellValue()+"."+ext);
              out.write(data);
              out.close();
                //}
              i++;
                //}
              //if(i>=101){
             //   break;
              //}
            }
        }   
    }
    public static void main(String[] args) throws IOException {     
        readXLSFile();
        System.out.println("Export is completed");
    }
}

1 个答案:

答案 0 :(得分:1)

图片未直接分配给单元格。它们通过ClientAnchor与细胞相关。 Workbook.getAllPictures()返回的列表未排序,与实际单元格没有关系。

您应该可以通过Drawing上的DrawingPatriarch班级和SheetWorkbook的{​​{1}}来获取此信息:

Drawing drawing = sheet.createDrawingPatriarch();
for(HSSFShape shape : ((HSSFPatriarch)drawing).getChildren()) {
    if(shape instanceof HSSFPicture) {
        HSSFClientAnchor anchor = ((HSSFPicture)shape).getClientAnchor();
        ... anchor has the cell-row/column area that it is "anchored" at
    }
}