示例#1
0
        /// <summary>
        /// 向sheet插入图片
        /// </summary>
        /// <param name="sheet"></param>
        /// <param name="workbook"></param>
        /// <param name="fileurl"></param>
        /// <param name="row"></param>
        /// <param name="col"></param>
        private async Task AddCellPicture(ISheet sheet, HSSFWorkbook workbook, string fileurl, int row, int col)
        {
            try
            {
                var imageStream = await _fileUploader.GetFileStreamAsync(fileurl);

                if (imageStream != null && imageStream.Length > 0)
                {
                    byte[] bytes = new byte[imageStream.Length];
                    await imageStream.ReadAsync(bytes);

                    int              pictureIdx = workbook.AddPicture(bytes, NPOI.SS.UserModel.PictureType.JPEG);
                    HSSFPatriarch    patriarch  = (HSSFPatriarch)sheet.CreateDrawingPatriarch();
                    HSSFClientAnchor anchor     = new HSSFClientAnchor(0, 0, 0, 0, col, row, col + 1, row + 1);
                    HSSFPicture      pict       = (HSSFPicture)patriarch.CreatePicture(anchor, pictureIdx);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }