Excel image in a cell

后端 未结 2 1160
忘掉有多难
忘掉有多难 2020-12-06 03:18

How do I insert an image (of type Image) into a specific cell in a Excel sheet

taperSheet = (Microsoft.Office.Interop.Excel.Worksheet)excelSheets.get_Item(\"         


        
相关标签:
2条回答
  • 2020-12-06 03:43

    Or Try this:

    private void PlacePicture(Image picture, Range destination)
    {
        Worksheet ws = destination.Worksheet;
        Clipboard.SetImage(picture);
        ws.Paste(destination, false);
        Pictures p = ws.Pictures(System.Reflection.Missing.Value) as Pictures;
        Picture pic = p.Item(p.Count) as Picture;
        ScalePicture(pic, (double)destination.Width, (double)destination.Height);
    }
    
    private void ScalePicture(Picture pic, double width, double height)
    {
        double fX = width / pic.Width;
        double fY = height / pic.Height;
        double oldH = pic.Height;
        if (fX < fY)
        {
            pic.Width *= fX;
            if (pic.Height == oldH) // no change if aspect ratio is locked
                pic.Height *= fX;
            pic.Top += (height - pic.Height) / 2;
        }
        else
        {
            pic.Width *= fY;
            if (pic.Height == oldH) // no change if aspect ratio is locked
                pic.Height *= fY;
            pic.Left += (width - pic.Width) / 2;
        }
    }
    
    0 讨论(0)
  • 2020-12-06 03:59

    Try this:

    object missing = System.Reflection.Missing.Value;
    Excel.Range picPosition = GetPicturePosition(); // retrieve the range for picture insert
    Excel.Pictures p = yourWorksheet.Pictures(missing) as Excel.Pictures;
    Excel.Picture pic = null;
    pic = p.Insert(yourImageFilePath, missing);
    pic.Left = Convert.ToDouble(picPosition .Left);
    pic.Top = Convert.ToDouble(picPosition .Top);
    pic.Placement = // Can be any of Excel.XlPlacement.XYZ value
    

    And don't forget to release all that stuff!

    0 讨论(0)
提交回复
热议问题