[Import] C # Two Methods for inserting images into an Excel report

Source: Internet
Author: User

If you use excel2003, you need to add Microsoft. Office. InterOP. Excel Version 11.0 of the. NET library. The reference bar displays Microsoft. Office. InterOP. Excel.

Then add the com library, Microsoft Office 11.0 object library. Microsoft. Office. Core appears in the reference column.

If c: \ Program Files \ Microsoft Office \ office11 \ excel. EXE is added, Excel and Microsoft. Office. Core are displayed in the reference column.

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 packaging class

Using system; using system. windows. forms; using Excel = Microsoft. office. interOP. excel; namespace mengxianhui. utility. excelreport {class insertpicturetoexcel {// & lt; summary & gt; // open the operation without a template. /// & Lt;/Summary & gt; Public void open () {This. open (string. empty) ;}/// & lt; summary & gt; // function: Enable the Excel application /// & lt;/Summary & gt; /// & lt; Param name = & quot; templatefilepath & quot; & gt; physical path of the template file & lt;/Param & gt; Public void open (string templatefilepath) {// open the object m_objexcel = new excel. application (); m_objexcel.visible = false; m_objexcel.displayalerts = false; If (m_objexcel.version! = & Quot; 11.0 & quot;) {MessageBox. Show (& quot; your Excel version is not 11.0 (Office 2003). problems may occur. & Quot;); m_objexcel.quit (); return;} m_objbooks = (Excel. workbooks) m_objexcel.workbooks; If (templatefilepath. equals (string. empty) {m_objbook = (Excel. _ workbook) (values (m_objopt);} else {m_objbook = values (values, m_objopt, m_objopt, values, 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 (& quot; saved! & Quot;) ;}/// & lt; summary & gt; // Insert the image to the specified cell position. /// Note: The image must be an absolute physical path // & lt;/Summary & gt; // & lt; Param name = & quot; rangename & quot; & gt; cell name, for example, B4 & lt;/Param & gt; // & lt; Param name = & quot; picturepath & quot; & gt; the absolute path of the image to be inserted. & Lt;/Param & gt; 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) ;}/// & lt; summary & gt; // Insert the image to the specified cell position and set the width and height of the image. /// Note: The image must be an absolute physical path // & lt;/Summary & gt; // & lt; Param name = & quot; rangename & quot; & gt; cell name, for example, B4 & lt;/Param & gt; // & lt; Param name = & quot; picturepath & quot; & gt; the absolute path of the image to be inserted. & Lt;/Param & gt; // & lt; Param name = & quot; pictutewidth & quot; & gt; the width of the inserted image in Excel. & Lt;/Param & gt; // & lt; Param name = & quot; pictureheight & quot; & gt; height of the inserted image in Excel. & Lt;/Param & gt; Public void insertpicture (string rangename, string picturepath, float pictutewidth, float pictureheight) {m_objrange = values (rangename, m_objopt); m_objrange.select (); float picleft, pictop; picleft = convert. tosingle (m_objrange.left); pictop = convert. tosingle (m_objrange.top); // parameter description: // image path // whether to link to a file // whether the image is saved along with the document during insertion // The Coordinate Position of the image in the document (unit: points) // the width and height of the image (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) ;}/// & lt; summary & gt; // Save the Excel file to the specified directory. The directory must exist in advance, the file name does not have to exist. /// & Lt;/Summary & gt; /// & lt; Param name = & quot; outputfilepath & quot; & gt; full path of the file to be saved. & Lt;/Param & gt; Public void SaveFile (string outputfilepath) {m_objbook.saveas (outputfilepath, m_objopt, Excel. xlsaveasaccessmode. xlnochange, m_objopt, m_objopt); this. close () ;}/// & lt; summary & gt; // close the application /// & lt;/Summary & gt; private void close () {m_objbook.close (false, m_objopt, m_objopt); m_objexcel.quit () ;}/// & lt; Su Mmary & gt; // release the referenced COM object. Note: This process must be executed. /// & Lt;/Summary & gt; Public void dispose () {releaseobj (m_objsheets); releaseobj (m_objbook); releaseobj (m_objbooks); releaseobj (m_objexcel); system. GC. collect (); system. GC. waitforpendingfinalizers () ;}/// & lt; summary & gt; // release the object, call it internally /// & lt;/Summary & gt; // & lt; param name = & quot; O & quot; & gt; & lt;/Param & gt; 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 ;}}

Running 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.