Inserts a picture into the specified cell position and sets the width and height of the picture.
Note: The picture must be an absolute physical path
</summary>
<param name= "Rangename" > cell name, for example:b4</param>
<param name= "PicturePath" > the absolute path to insert the picture. </param>
public void InsertPicture (string rangename, Excel._worksheet sheet, string picturepath)
{
Excel.Range rng = (excel.range) sheet.get_range (Rangename, Type.Missing);
Rng. Select ();
Float picleft, Pictop, Picwidth, picheight; Distance from left, top distance, picture width, height
Pictop = Convert.tosingle (rng. TOP);
Picwidth = Convert.tosingle (rng. Mergearea.width);
Picheight = Convert.tosingle (rng. Height);
Picwidth = Convert.tosingle (rng. Width);
Picleft = Convert.tosingle (rng. left);//+ (Convert.tosingle (rng. Mergearea.width)-picwidth)/2;
Try
{
Excel.pictures pics = (excel.pictures) sheet. Pictures (Type.Missing);
Pics. Insert (PicturePath, Type.Missing);
Pics. left = (double) rng. Left;
Pics. Top = (double) rng. Top;
Pics. Width = (double) rng. Width;
Pics. Height = (double) rng. Height;
}
Catch
{
}
Sheet. Shapes.addpicture (PicturePath, Microsoft.Office.Core.MsoTriState.msoFalse,
Microsoft.Office.Core.MsoTriState.msoTrue, Picleft, Pictop, Picwidth, picheight);
}
If you want to insert in an area, and the area is not named, pass directly to the selected area
CELL1 = sourcesheet.cells[The first few lines, the first few columns];
Cell2 = Sourcesheet.cells[row, Column];
SourceRange = Sourcesheet.get_range (Cell1, Cell2);
Then the above sentence is removed excel.range rng = (excel.range) sheet.get_range (Rangename, Type.Missing);
Change the rng to SourceRange.
Method Two:
Sheet. Shapes.addpicture (...) )
///Inserts the picture into the specified cell position and sets the width and height of the picture.
///NOTE: The picture must be an absolute physical path
///</summary>
//<param name= "rng" >excel cell selected area </param>
// <param name= "PicturePath" > the absolute path to insert the picture. </param>
public void InsertPicture (Excel.Range rng, Excel._worksheet Sheet, string picturepath)
{
rng. Select ();
Float picleft, pictop, Picwidth, picheight;
Try
{
Picleft = Convert.tosingle (rng. left);
Pictop = Convert.tosingle (rng. TOP);
Picwidth = Convert.tosingle (rng. Width);
Picheight = Convert.tosingle (rng. Height);
Parameter meaning:
Picture path
Whether link to file
Whether the picture is saved with the document when it is inserted
Coordinates of the position of the picture in the document
Width and height of picture display
Sheet. Shapes.addpicture (PicturePath, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoTrue, Picleft, Pictop, Picwidth, picheight);
}
catch (Exception ex)
{
MessageBox.Show ("error:" + ex.) Message);
}
}
C # Inserts a picture in a specified cell in Excel