Asp.net c imports an image to the excel Code. below is the example code that uses c # to import the image to the excel file, which is convenient and simple. If you want to import some images to the excel file,.
Asp tutorial. net c Import images to excel Code
The following is an example code that uses c # To import images to an excel file, which is convenient and simple. If you want to import some images to an excel file, it works well.
*/
Using system;
Using system. collections. generic;
Using system. text;
Using microsoft. office. tools. excel;
Using system. windows. forms;
Using excel;
Namespace serial_port
{
Class insertpicturetoexcel
{
Public void open ()
{
This. open (string. empty );
}
/// <Summary>
/// Function: Enable an excel application
/// </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), and operations may fail. ");
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 = values (values, m_objopt, m_obj );
}
M_ob webpage effect heets = (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 ("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"> specifies 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"> specifies 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 = 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 description:
// Image path
// Whether to link to the file
// Whether the image is saved along with the document during insertion
// Coordinate Position of the image in the document (unit: points)
// The width and height of the image (unit: points)
// For 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. msoctrue, picleft, pictop, pictutewidth, pictureheight );
}
/// <Summary>
/// Save the excel file to the specified directory. The directory must exist beforehand and the file name may not exist.
/// </Summary>
/// <Param name = "outputfilepath"> full path of the file to be saved. </Param>
Public void savefile (string outputfilepath)
{
Values (outputfilepath, m_objopt, 1_aveasaccessmode. 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 object. Note: This process must be executed.
/// </Summary>
Public void dispose ()
{
Releaseo tutorial bj (m_objsheets );
Releaseobj (m_objbook );
Releaseobj (m_objbooks );
Releaseobj (m_objexcel );
System. gc. collect ();
System. gc. waitforpendingfinalizers ();
}
/// <Summary>
/// Release the object, called internally
/// </Summary>
/// <Param name = "o"> </param>
Private void releaseobj (object o)
{
Try
{
System. runtime. interops tutorial ervices. 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;
}
}