In the past few days, data is inserted into the Excel file. The following two methods can be used to insert images, but they are of different use. Now I want to share these two methods. I hope you can refer to them,CodeHas been annotated in detail.
Note: COM: Microsoft Office 11.0 Object Library must be referenced before use. If the reference list does not exist, you must add c: \ Program Files \ Microsoft Office \ office11 \ excel. EXE.
Call method:
Mengxianhui. utility. excelreport. insertpicturetoexcel EPT = new mengxianhui. utility. excelreport. insertpicturetoexcel (); EPT. open (); EPT. insertpicture ("B2", @ "C: \ excellogo.gif"); EPT. insertpicture ("B8", @ "C: \ excellogo.gif",); EPT. saveFile (@ "C: \ exceltest.xls"); EPT. dispose ();
Simple packaging class:
Using system; using system. windows. forms; using Excel = Microsoft. office. interOP. excel; namespace mengxianhui. utility. excelreport {class insertpicturetoexcel {/// <summary> /// open an operation without a template. /// </Summary> Public void open () {this. Open (string. Empty);} // <summary> // function: Implement an Excel application Program /// </Summary> /// <Param name = "templatefilepath"> physical path of the template file </param> 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! = "11.0") {MessageBox. Show ("your Excel version is not 11.0 (Office 2003). problems may occur. "); 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 = (E Xcel. 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 ("saved! ") ;}/// <Summary> /// Insert the image to the specified cell position. /// Note: The image must be an absolute physical path // </Summary> /// <Param name = "rangename"> cell name, for example: b4 </param> /// <Param name = "picturepath"> the absolute path of the image to be inserted. </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> /// 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 // </Summary> /// <Param name = "rangename"> cell name, for example: b4 </param> /// <Param name = "picturepath"> the absolute path of the image to be inserted. </Param> /// <Param name = "pictutewidth"> the width of the inserted image displayed in Excel. </Param> /// <Param name = "pictureheight"> the height of the inserted image displayed in Excel. </Param> Public void insertpicture (string rangename, string picturepath, float pictutewidth, float pictureheight) {m_objrange = values (rangename, m_objopt); values (); 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 Display (unit: Points) // Parameter For more information, 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> // Save the Excel file to the specified directory. The directory must exist in advance, the file name does not have to exist. /// </Summary> /// <Param name = "outputfilepath"> full path of the file to be saved. </Param> Public void SaveFile (string outputfilepath) {m_objbook.saveas (outputfilepath, m_objopt, Excel. xlsaveasaccessmode. xlnochange, m_objopt, m_objopt); this. close () ;}/// <summary> /// close the application // </Summary> private void close () {m_objbook.close (false, m_objopt, m_objopt ); m_objexcel.quit () ;}/// <summary> /// release the referenced com pair Image. Note: This process must be executed. /// </Summary> Public void dispose () {releaseobj (m_objsheets); releaseobj (m_objbook); releaseobj (m_objbooks); releaseobj (m_objexcel); system. GC. collect (); system. GC. waitforpendingfinalizers ();} // <summary> // release the 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 ;}}