C # 2 ways to insert a picture into an Excel report

Source: Internet
Author: User
Tags empty

These days do to insert data into Excel, which has the need to insert pictures, after testing, the following 2 methods can be inserted into the picture, but each has different uses. Now the 2 methods are shared out, want to need friends for reference, the code has been detailed comments.

Note: You need to refer to the Com:microsoft Office 11.0 Object Library before using

If you do not have a list of references, you need to add C:\Program Files\Microsoft Office\office11\excel yourself. Exe

Call Method:

MengXianhui.Utility.ExcelReport.InsertPictureToExcel IPT = new MengXianhui.Utility.ExcelReport.InsertPictureToExcel ();
Ipt. Open ();
Ipt. InsertPicture ("B2", @ "C:\Excellogo.gif");
Ipt. InsertPicture ("B8", @ "C:\Excellogo.gif", 120,80);
Ipt. SaveFile (@ "C:\ExcelTest.xls");
Ipt. Dispose ();

Simple Packaged classes:

using System;


using System.Windows.Forms;


using Excel = Microsoft.Office.Interop.Excel;


namespace MengXianhui.Utility.ExcelReport


{


class Insertpicturetoexcel


{


///<summary>


///to open an operation without a template.


///</summary>


public void Open ()


{


this. Open (String.Empty);


}


///<summary>


///function: To enable Excel applications to open


///</summary>


///<param name= "Templatefilepath" > Template file physical path </param>


public void Open (string templatefilepath)


{


//Open Object


m_objexcel = new Excel.Application ();


m_objexcel.visible = false;


m_objexcel.displayalerts = false;


if (m_objexcel.version!= "11.0")


{


MessageBox.Show ("Your version of Excel is not 11.0 (Office 2003), there may be a problem with the operation." ");


M_objexcel.quit ();


return;


}


m_objbooks = (excel.workbooks) m_objexcel.workbooks;


if (templatefilepath.equals (String.Empty))


{


m_objbook = (Excel._workbook) (M_objbooks.add (m_objopt));


}


Else


{


m_objbook = M_objbooks.open (Templatefilepath, m_objopt, m_objopt, m_objopt, m_objopt,


m_objopt, m_objopt, m_objopt, m_objopt, m_objopt, m_objopt, m_objopt, m_objopt, m_objopt, m_objOpt);


}


m_objsheets = (excel.sheets) m_objbook.worksheets;


m_objsheet = (excel._worksheet) (M_objsheets.get_item (1));


M_objexcel.workbookbeforeclose + = new Excel.appevents_workbookbeforecloseeventhandler (m_objExcel_ WorkbookBeforeClose);


}


private void M_objexcel_workbookbeforeclose (Excel.Workbook m_objbooks, ref bool _cancel)


{


MessageBox.Show ("Save complete! ");


}


///<summary>


///inserts a picture into the specified cell location.


///Note: The picture must be an absolute physical path


///</summary>


///<param name= "Rangename" > cell name, such as:b4</param>


<param name= "PicturePath" > absolute path to insert a picture. </param>


public void InsertPicture (string rangename, string picturepath)


{


m_objrange = M_objsheet.get_range (Rangename, m_objopt);


M_objrange.select ();


excel.pictures pics = (excel.pictures) m_objsheet.pictures (m_objopt);


pics. Insert (PicturePath, m_objopt);


}


///<summary>


///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, such as:b4</param>


///<param name= "PicturePath" > absolute path to insert a picture. </param>


///<param name= "Pictutewidth" > The width of the picture displayed in Excel after insertion. </param>


///<param name= "Pictureheight" > The height of the picture displayed in Excel after insertion. </param>


public void InsertPicture (string rangename, String picturepath, float pictutewidth, float pictureheight)


{


m_objrange = M_objsheet.get_range (Rangename, m_objopt);


M_objrange.select ();


float picleft, pictop;


picleft = Convert.tosingle (m_objrange.left);


pictop = Convert.tosingle (m_objrange.top);


//Parameter meaning:


//Picture path


//is linked to file


//Picture insertion is saved with the document


//Picture coordinates in document (unit: points)


//Picture display width and height (unit: points)


//parameter details see: http://msdn2.microsoft.com/zh-cn/library/aa221765 (office.11). aspx


m_objSheet.Shapes.AddPicture (PicturePath, Microsoft.Office.Core.MsoTriState.msoFalse,


Microsoft.Office.Core.MsoTriState.msoTrue, Picleft, Pictop, Pictutewidth, pictureheight);


}


///<summary>


///saves the Excel file to the specified directory, the directory must exist beforehand, and the file name does not necessarily exist.


///</summary>


///<param name= "Outputfilepath" > The full path of the file to be saved. </param>


public void SaveFile (string outputfilepath)


{


m_objBook.SaveAs (Outputfilepath, m_objopt, m_objopt,


m_objopt, m_objopt, m_objopt, Excel.XlSaveAsAccessMode.xlNoChange,


m_objopt, m_objopt, m_objopt, m_objopt, m_objopt);


this. Close ();


}


///<summary>


///shutdown Application


///</summary>


private void Close ()


{


M_objbook.close (False, m_objopt, m_objopt);


M_objexcel.quit ();


}


///<summary>


///Releases the referenced COM object. Note: This process must be performed.


///</summary>


public void Dispose ()


{


releaseobj (m_objsheets);


releaseobj (m_objbook);


Releaseobj (m_objbooks);


releaseobj (m_objexcel);


System.GC.Collect ();


System.GC.WaitForPendingFinalizers ();


}


///<summary>


///Release object, internal call


///</summary>


///<param name= "O" ></param>


private void Releaseobj (object o)


{


Try


{


System.Runtime.InteropServices.Marshal.ReleaseComObject (o);


}


Catch {}


finally {o = null;}


}


private Excel.Application m_objexcel = null;


private Excel.Workbooks m_objbooks = null;


private Excel._workbook m_objbook = null;


private Excel.Sheets m_objsheets = null;


private Excel._worksheet m_objsheet = null;


private Excel.Range m_objrange = null;


Private object m_objopt = System.Reflection.Missing.Value;


}


}

Run Result:

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.