感谢Tony移植了POI到了.net平台,我尝试了一下.
关键的几行代码如下
创建HSSFPatriarch对象,如果有多张图片需要添加,只能有一个HSSFPatriarch对象。
HSSFPatriarch patriarch
=
sheet1.CreateDrawingPatriarch();
byte [] buff = File.ReadAllBytes( " /home/meyer/030.jpg " );
int pic = hssfworkbook.AddPicture(buff,HSSFWorkbook.PICTURE_TYPE_JPEG);
HSSFClientAnchor anchor = new HSSFClientAnchor(0 ,0 ,0 ,0 , 1 , 1 , 2 , 2 );
anchor.AnchorType = 2 ;
patriarch.CreatePicture(anchor,pic);
byte [] buff = File.ReadAllBytes( " /home/meyer/030.jpg " );
int pic = hssfworkbook.AddPicture(buff,HSSFWorkbook.PICTURE_TYPE_JPEG);
HSSFClientAnchor anchor = new HSSFClientAnchor(0 ,0 ,0 ,0 , 1 , 1 , 2 , 2 );
anchor.AnchorType = 2 ;
patriarch.CreatePicture(anchor,pic);
JAVA的POI上的文档说只能支持:jpg,png,dib三种格式。
对于常见的gif是没有支持的。HSSFWorkbook中也没有PICTURE_TYPE_GIF的定义。
不过,好玩的是你可以在NPOI中尝试下面的代码
byte
[] buff
=
File.ReadAllBytes(
"
/home/meyer/
xml.gif
"
);
int pic = hssfworkbook.AddPicture(buff,HSSFWorkbook.PICTURE_TYPE_JPEG);
int pic = hssfworkbook.AddPicture(buff,HSSFWorkbook.PICTURE_TYPE_JPEG);