Click here to Skip to main content
16,018,460 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
I have two questions, hope you can help me, thank you very much!

1)for example, I have gotten the range
C++
range = sheet.get_Range(COleVariant(_T("A1")),COleVariant(_T("A1"+intToCString2)));
, how can i set cell A1's background color?

2)
C++
range = sheet.get_Range(COleVariant(_T("A1")),COleVariant(_T("A5")));
, I want to group row A1 to A2, I know class CRange has a method group(parameter1,parameter2,parameter3,parameter4), but i don't know how to invoke it.
Posted
Updated 7-Sep-16 19:48pm
Comments
Jochen Arndt 5-Aug-13 4:14am    
Regarding your first question you may try this:
Interior = range.GetInterior();
Interior.SetColor(COleVariant(lColor));
The SetColor() parameter is of type long. So pass a long variable or a constant with type (e.g. SetColor(COleVariant(0x808080), VT_I4)).
I'm not sure if this works (not tested), so I posted it as comment rather than a solution.

The Group() function prototype is:
VARIANT Range::Group(const VARIANT& Start, const VARIANT& End, const VARIANT& By, const VARIANT& Periods);

1 solution

set bg color:
	CApplication app;
	CWorkbooks books;
    CWorkbook book;
    CWorksheets sheets;
	CWorksheet sheet;
	CRange range;
	CInterior interior;

......

		// Commonly used OLE variants.
	COleVariant  covTrue((short)TRUE),  covFalse((short)FALSE),  covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);	
	// Start Excel and get an Application object.
	if(!app.CreateDispatch(TEXT("Excel.Application"))) {
		AfxMessageBox(TEXT("Couldn't start Excel and get Application object.\nPlease (re)install MS Excel.",MB_OK|MB_ICONSTOP));
		return;
	}

books = app.get_Workbooks();  // Get an IDispatch pointer.

if(m_bFromFile)
    book = books.Open(m_strFile, covOptional, covOptional, covOptional, covOptional, covOptional, covOptional, covOptional, covOptional, covOptional, covOptional, covOptional, covOptional,covOptional,covOptional );   // Return Workbook's IDispatch pointer.
else
    book = books.Add (covOptional);// Open a new workbook and attach that IDispatch pointer to the Workbook object.

sheets =book.get_Sheets();   //CWorksheets get a pointer to CWorksheets pools
sheet = sheets.get_Item(COleVariant((short)1));   //CWorksheet get 1st sheet from the pool
//upto here is all regular excel instance. Now the bg colour
range = sheet.get_Range(COleVariant(TEXT("A1")), COleVariant(TEXT("G1")));
interior=range.get_Interior();
interior.put_ColorIndex(COleVariant((short)36)); //color by index
interior.put_Color(COleVariant(long(RGB(0xFF,0xC4,0xC4))));  //colour by RGB


here is how to merge:
range = sheet.get_Range(COleVariant(TEXT("A1")),COleVariant(TEXT("G1")));
range.Merge(covOptional);  //merge A1 - G1
 
Share this answer
 
v3
Comments
Member 11099444 9-Apr-18 11:56am    
what is CInterior class? and how to invoke it in MFC application

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900