C # Inserts a picture in a specified cell in Excel

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.