Asp.net c: import an image to an excel file

Source: Internet
Author: User

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;

  }
}

Related Article

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.