我的问题是,当使用java脚本在excel中提取图像时,图像在某个地方被交换了。
使用java代码,我可以在这里得到如下所示
a94行--保存picture94
a95行--保存picture95
a96行--保存picture96
我想像下面的格式那样存钱。
a94行--保存picture94
a95行--保存picture96
a96行--保存picture95
我想要保存每个图像与各自的细胞。请给我一些想法.Do我必须改变我的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");
}
}发布于 2016-01-28 18:11:59
图片不会直接分配给单元格。它们通过ClientAnchor与细胞相关。Workbook.getAllPictures()返回的列表没有排序,并且与实际单元格没有关系。
您应该能够通过Drawing类和Workbook中的Sheet的DrawingPatriarch获取此信息
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
}
}https://stackoverflow.com/questions/35032646
复制相似问题