入参:工作簿对象,某个单元格对象,将要写入的图片字节数组
对象解释:
XSSFClientAnchor:可设置图片放置的开始、结束单元格,X、Y 起始点位(这里挖个坑,具体设置多少可以根据行高等进行计算,具体可参考 pic.Resize() 的实现 ) /// <summary>
/// 将图片添加到工作簿 /// </summary> /// <param name="workbook"></param> /// <param name="cell"></param> /// <param name="bytes"></param> void SetCellPhoto(IWorkbook workbook, NPOI.SS.UserModel.ICell cell, byte[] bytes) { int pictureIdx = workbook.AddPicture(bytes, NPOI.SS.UserModel.PictureType.PNG);// 创建绘图对象 var drawing = workbook.GetSheetAt(0).CreateDrawingPatriarch();
XSSFClientAnchor anchor = new XSSFClientAnchor( 0, 50000, 0, 0, // 从单元格左上角开始 cell.ColumnIndex + 2, cell.RowIndex, cell.ColumnIndex + 5, cell.RowIndex + 1 ); anchor.AnchorType = (AnchorType)2; // 绝对定位
// 创建图片对象 XSSFPicture pic = (XSSFPicture)drawing.CreatePicture(anchor, pictureIdx);
// 设置图片的缩放大小 pic.Resize(1, 0.9); }
标签:pic,C#,单元格,Excel,NPOI,cell,XSSFClientAnchor,图片 From: https://www.cnblogs.com/pengxx/p/18256224