首页 > 代码库 > C# 导入图片到Excel单元格

C# 导入图片到Excel单元格

导出Excel功能时需要将数据中含有图片也导出到excel单元格中

 public void InsertPicture(string RangeName, string PicturePath, Microsoft.Office.Interop.Excel.Worksheet worksheet)
        {
            Microsoft.Office.Interop.Excel.Range m_objRange = worksheet.get_Range(RangeName, Type.Missing);
            m_objRange.Select();

            float PicLeft, PicTop, PicWidth, PicHeight;    //距离左边距离,顶部距离,图片宽度、高度
            PicTop = Convert.ToSingle(m_objRange.Top);
            PicWidth = Convert.ToSingle(m_objRange.MergeArea.Width);
            PicHeight = Convert.ToSingle(m_objRange.Height)-10;
            PicWidth = Convert.ToSingle(m_objRange.Width)-10;
            PicLeft = Convert.ToSingle(m_objRange.Left);//+ (Convert.ToSingle(rng.MergeArea.Width) - PicWidth) / 2;          
                 
            try
            {
                //worksheet.Shapes.AddPicture(PicturePath, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoTrue, PicLeft, PicTop, PictuteWidth, PictureHeight);
                worksheet.Shapes.AddPicture(PicturePath, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoTrue, PicLeft, PicTop, 90, 70);
            }
            catch
            {
            }
        }

 

C# 导入图片到Excel单元格