NPOI插入圖片到Excel

//插入圖片
string picPath = @"C:\Users\537\Desktop\Test\圖片1.png";
FileStream picFs = File.OpenRead(picPath); //OpenRead
int filelength = 0;
filelength = (int)picFs.Length; //獲得文件長度 
Byte[] image = new Byte[filelength]; //建立一個字節數組 
picFs.Read(image, 0, filelength); //按字節流讀
int pictureIdx = workbook.AddPicture(image, PictureType.PNG);
HSSFPatriarch patriarch = (HSSFPatriarch)sheet1.CreateDrawingPatriarch();
// 插圖片的位置  HSSFClientAnchor(dx1,dy1,dx2,dy2,col1,row1,col2,row2) 
//參數表示圖片的位置.具體含義參考另一篇博文"NPOI獲得圖片信息"
HSSFClientAnchor anchor = new HSSFClientAnchor(70, 10, 0, 0, 1, 0, 2, 0 + 1);
//把圖片插到相應的位置
HSSFPicture pict = (HSSFPicture)patriarch.CreatePicture(anchor, pictureIdx);
HSSFShapeGroup hSSFShapeGroup = patriarch.CreateGroup(anchor);
hSSFShapeGroup.AddShape(pict);
//hSSFShapeGroup.CreateShape()
using (Stream stream = File.OpenWrite("d:\\aaa.xls"))
{
    workbook.Write(stream);
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章