1. 程式人生 > >NPOI匯出excel,插入圖片

NPOI匯出excel,插入圖片

//add picture data to this workbook.

byte[] bytes = System.IO.File.ReadAllBytes(@"D:\MyProject\NPOIDemo\ShapeImage\image1.jpg");

int pictureIdx = hssfworkbook.AddPicture(bytes, HSSFWorkbook.PICTURE_TYPE_JPEG);



//create sheet

HSSFSheet sheet = hssfworkbook.CreateSheet("Sheet1");



// Create the drawing patriarch.  This is the top level container for all shapes. 

HSSFPatriarch patriarch = sheet.CreateDrawingPatriarch();



//add a picture

HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 1023, 0, 0, 0, 1, 3);

HSSFPicture pict = patriarch.CreatePicture(anchor, pictureIdx);

有時並不需要拉伸的效果

在結尾加上pict.Resize();即可使圖片自動伸縮到原始大小了