心在飞扬 2021-06-28 17:37 采纳率: 18.2%
浏览 360
已采纳

C# Npoi 如何往excel插入图片??

百度了一下,这个方法很简单,也很容易懂。但是为啥我操作的时候,返回的是0?

 

Bitmap tmpJPN = PagePhoto.GetWebSiteThumbnail(url, 250, 250, 250, 250);
//上面的位图没问题

MemoryStream ms111 = new MemoryStream();
tmpJPN.Save(ms111, System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] buff = ms111.ToArray(); //二进制有数据

int pictureIdx = hbook.AddPicture(buff, PictureType.JPEG); //这里直接返回了0??

//这样就导致后面无法插入了


XSSFDrawing patriarch = (XSSFDrawing)a.CreateDrawingPatriarch();

 XSSFClientAnchor anchor = new XSSFClientAnchor(0, 0, 0, 0, 1, maxRows + 4, 1, maxRows + 4);

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

这个是为啥...引起的原因是啥? 

 

  • 写回答

4条回答 默认 最新

  • ダ雨夹雪リ 2021-07-01 14:30
    关注

    你的代码没问题,加上这句pict.Resize()就可以了

      private void button2_Click(object sender, EventArgs e)
            {
                IWorkbook hbook = new XSSFWorkbook();
                ISheet a = hbook.CreateSheet("Sheet1");
                IRow excelRow = a.CreateRow(1);
    
    
    
                Bitmap tmpJPN = (Bitmap)Image.FromFile(@"D:\Desktop\微信图片_20210701133515.jpg");
                
    
                //以下是你的代码
                MemoryStream ms111 = new MemoryStream();
                tmpJPN.Save(ms111, System.Drawing.Imaging.ImageFormat.Jpeg);
                byte[] buff = ms111.ToArray(); //二进制有数据
                int pictureIdx = hbook.AddPicture(buff, PictureType.JPEG); //这里没问题
    
    
                XSSFDrawing patriarch = (XSSFDrawing)a.CreateDrawingPatriarch();
                XSSFClientAnchor anchor = new XSSFClientAnchor(0, 0, 0, 0, 1,  4, 1,  4);
             
                XSSFPicture pict = (XSSFPicture)patriarch.CreatePicture(anchor, pictureIdx);
    
                pict.Resize();   //加这句就可以了
    
                //以上
    
                MemoryStream ms = new MemoryStream();
                hbook.Write(ms);
    
                byte[] data = ms.ToArray();
                FileStream fs = new FileStream(@"D:\test.xlsx", FileMode.Create);
                fs.Write(data, 0, data.Length);
                fs.Close();
    
    
                MessageBox.Show("OK");
    
            }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥21 matlab怎么求时域信号的二阶导数
  • ¥15 判断两个表是否完全相同
  • ¥15 java map类型数据格式,如何快速通过前缀匹配元素
  • ¥15 stc12c5a60s2、QMC5883L、LCD1602组合测量磁场所需程序
  • ¥15 vba参数转c++ SAFEARRAY
  • ¥20 Win11测试yolov4,“找不到nvcuda.dll”怎么办?
  • ¥15 simulink绘制bode图
  • ¥15 php_network_getaddresses: getaddrinfo failed: Name or service not known
  • ¥15 用msg发消息出现的问题
  • ¥15 unity3d机械臂