In the VC, how does excel operate the font, color, and line frame of cells (and throws exceptions)

Source: Internet
Author: User
Tags set background

//////////////////////////////////////// //////////////////////////////////////// ///////
// Change cell font of Excel
//////////////////////////////////////// //////////////////////////////////////// ///////
Void cc2dlg: onok ()
{// HOWTO: Create automation project using MFC and a Type Library q178749
// Change font of a cell in Excel
Try
{
_ Application app; // app is an _ application object.
_ Workbook book; // more object declarations.
_ Worksheet sheet;
Workbooks books;
Worksheets sheets;
Range; // used for Microsoft Excel 97 components.
Lpdispatch lpdisp; // often reused variable.
Colevariant
Covtrue (short) True ),
Covfalse (short) False ),
Covoptional (long) disp_e_paramnotfound, vt_error );
If (! App. createdispatch ("Excel. application "))
{
Afxmessagebox ("couldn't createdispatch () for excel ");
Return;
}
App. setvisible (true );
Lpdisp = app. getworkbooks (); // get an idispatch pointer.
Assert (lpdisp );
Books. attachdispatch (lpdisp); // attach the idispatch pointer
// To the books object.
Lpdisp = books. Open ("C: // temp // book1.xls", // test.xls is a workbook.
Covoptional,
Covoptional,
Covoptional, covoptional); // return workbook's idispatch
// Pointer.
Book. attachdispatch (lpdisp );
Lpdisp = book. getsheets ();
Assert (lpdisp );
Sheets. attachdispatch (lpdisp );
// Get Sheet #1 and attach the idispatch pointer to your sheet
// Object.
Lpdisp = sheets. getitem (colevariant (short) (1 )));
Assert (lpdisp );
Sheet. attachdispatch (lpdisp );
Lpdisp = sheet. getrange (colevariant ("B3"), colevariant ("B3 "));
Range. attachdispatch (lpdisp );
Range. setnumberformat (colevariant ("@"));
Range. setitem (colevariant (long) (1), colevariant (long) (1), colevariant (lpctstr ("000666 ")));
Font newfont;
Lpdisp = range. getfont ();
Newfont. attachdispatch (lpdisp );
Newfont. setname (colevariant (" "));
Newfont. setsize (colevariant (long) 24 ));
Newfont. releasedispatch ();
// Release dispatch pointers.
Range. releasedispatch ();
Sheet. releasedispatch ();
// This is not really necessary because
// The default second parameter of attachdispatch releases
// When the current scope is lost.

} // End of processing.

Catch (coleexception * E)
{
Char Buf [1024]; // For the try... catch error message.
Sprintf (BUF, "coleexception. scode: % 08lx.", (long) E-> m_ SC );
: MessageBox (null, Buf, "coleexception", mb_setforeground | mb_ OK );
}
Catch (coledispatchexception * E)
{
Char Buf [1024]; // For the try... catch error message.
Sprintf (BUF,
"Coledispatchexception. scode: % 08lx, Description:/" % S /".",
(Long) E-> m_wcode, (lpstr) E-> m_strdescription.getbuffer (512 ));
: MessageBox (null, Buf, "coledispatchexception ",
Mb_setforeground | mb_ OK );
}
Catch (...)
{
: MessageBox (null, "General Exception caught.", "catch-all ",
Mb_setforeground | mb_ OK );
}
Cdialog: onok ();
}
//////////////////////////////////////// /////////////////////////////
// Change font color of a cell in Excel, change cell background color, set border
//////////////////////////////////////// /////////////////////////////
Void ccellcolordlg: onok ()
{// HOWTO: Create automation project using MFC and a Type Library q178749
// Change font color of a cell in Excel, change cell background color, set border
// Do not forget to call afxoleinit ();
Try
{
_ Application app; // app is an _ application object.
_ Workbook book; // more object declarations.
_ Worksheet sheet;
Workbooks books;
Worksheets sheets;
Range; // used for Microsoft Excel 97 components.
Lpdispatch lpdisp; // often reused variable.
Colevariant
Covtrue (short) True ),
Covfalse (short) False ),
Covoptional (long) disp_e_paramnotfound, vt_error );
If (! App. createdispatch ("Excel. application "))
{
Afxmessagebox ("couldn't createdispatch () for excel ");
Return;
}
App. setvisible (true );
Lpdisp = app. getworkbooks (); // get an idispatch pointer.
Assert (lpdisp );
Books. attachdispatch (lpdisp); // attach the idispatch pointer
// To the books object.
Lpdisp = books. Open ("C: // temp // book1.xls", // test.xls is a workbook.
Covoptional,
Covoptional,
Covoptional, covoptional); // return workbook's idispatch
// Pointer.
Book. attachdispatch (lpdisp );
Lpdisp = book. getsheets ();
Assert (lpdisp );
Sheets. attachdispatch (lpdisp );
// Get Sheet #1 and attach the idispatch pointer to your sheet
// Object.
Lpdisp = sheets. getitem (colevariant (short) (1 )));
Assert (lpdisp );
Sheet. attachdispatch (lpdisp );
Lpdisp = sheet. getrange (colevariant ("B3"), colevariant ("B3 "));
Range. attachdispatch (lpdisp );
// Set number format
// Range. setnumberformat (colevariant ("@"));
// Range. setitem (colevariant (long) (1), colevariant (long) (1), colevariant (lpctstr ("000666 ")));
// Set cell font here
Font newfont;
Lpdisp = range. getfont ();
Newfont. attachdispatch (lpdisp );
// Newfont. setname (colevariant (" "));
// Newfont. setsize (colevariant (long) 24 ));
Newfont. setcolorindex (colevariant (short) 3); // use VBA to see index value of a certain color
Newfont. releasedispatch ();

// Set background color
Lpdisp = range. getinterior ();
Interior cellinterior;
Cellinterior. attachdispatch (lpdisp );
Cellinterior. setcolorindex (colevariant (short) 6); // use VBA to see index value of a certain color
Cellinterior. releasedispatch ();
// Set border
// Xldiagonaldown = 5 xldiagonalup = 6 xledgeleft = 7
// Xlnone =-4142 xlcontinuous = 1 xlthin = 2 xlautomatic =-4105
// Xledgetop = 8 xledgebottom = 9 xledgeright = 10
Lpdisp = range. getborders ();
Borders BDS;
BDS. attachdispatch (lpdisp );
Border BD;
Lpdisp = BDS. getitem (8 );
BD. attachdispatch (lpdisp );
BD. setlinestyle (colevariant (short) 1 ));

/// Release dispatch pointers.
Range. releasedispatch ();
Sheet. releasedispatch ();
// This is not really necessary because
// The default second parameter of attachdispatch releases
// When the current scope is lost.

} // End of processing.

Catch (coleexception * E)
{
Char Buf [1024]; // For the try... catch error message.
Sprintf (BUF, "coleexception. scode: % 08lx.", (long) E-> m_ SC );
: MessageBox (null, Buf, "coleexception", mb_setforeground | mb_ OK );
}
Catch (coledispatchexception * E)
{
Char Buf [1024]; // For the try... catch error message.
Sprintf (BUF,
"Coledispatchexception. scode: % 08lx, Description:/" % S /".",
(Long) E-> m_wcode, (lpstr) E-> m_strdescription.getbuffer (512 ));
: MessageBox (null, Buf, "coledispatchexception ",
Mb_setforeground | mb_ OK );
}
Catch (...)
{
: MessageBox (null, "General Exception caught.", "catch-all ",
Mb_setforeground | mb_ OK );
}
Cdialog: onok ();
}

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.