C # How do I draw a diagonal line in a cell in Excel ??

Source: Internet
Author: User
Code snippet

Practice:

1. First add reference COM and find Excel

2, using Excel = Microsoft. Office. InterOP. Excel;

3,

Code

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;

 

Private void button3_click (Object sender, eventargs E)
{
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;

M_objbook = require (@ "E: \ website2 \ book2.xls", m_objopt, empty, empty, m_objopt, merge, merge, and merge, m_objopt );

M_objsheets = (Excel. Sheets) m_objbook.worksheets;
M_objsheet = (Excel. _ worksheet) (m_objsheets.get_item (1 ));
M_objrange = m_objsheet.get_range ("B3", m_objopt );
M_objrange.borders [microsoft. Office. InterOP. Excel. xlbordersindex. xldiagonaldown]. linestyle = excel. xllinestyle. xllinestylenone;
M_objrange.borders [microsoft. Office. InterOP. Excel. xlbordersindex. xldiagonalup]. linestyle = excel. xllinestyle. xlcontinuous;
M_objrange.borders [microsoft. Office. InterOP. Excel. xlbordersindex. xldiagonalup]. colorindex = excel. xlcolorindex. xlcolorindexautomatic;
M_objrange.borders [microsoft. Office. InterOP. Excel. xlbordersindex. xledgeleft]. linestyle = excel. xllinestyle. xllinestylenone;
M_objrange.borders [microsoft. Office. InterOP. Excel. xlbordersindex. xledgetop]. linestyle = excel. xllinestyle. xllinestylenone;
M_objrange.borders [microsoft. Office. InterOP. Excel. xlbordersindex. xledgebottom]. linestyle = excel. xllinestyle. xllinestylenone;
M_objrange.borders [microsoft. Office. InterOP. Excel. xlbordersindex. xledgeright]. linestyle = excel. xllinestyle. xllinestylenone;
M_objbook.save ();

M_objbook.close (false, m_objopt, m_objopt );
M_objexcel.quit ();

This. disposeexcel ();
}

 

 

 

/// <Summary>
/// Release the referenced COM object. Note: This process must be executed.
/// </Summary>
Public void disposeexcel ()
{

Releaseobj (m_objsheets );
Releaseobj (m_objbook );
Releaseobj (m_objbooks );
Releaseobj (m_objexcel );
System. gc. Collect ();
System. gc. waitforpendingfinalizers ();
}
Private void releaseobj (Object O)
{
Try
{
System. runtime. interopservices. Marshal. releasecomobject (O );
}
Catch {}
Finally {o = NULL ;}
}

 

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.