void CAutoProjectDlg::OnBnClickedRun()
{
// Commonly used OLE variants.
COleVariant
covTrue((short)TRUE),
covFalse((short)FALSE),
covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
CApplication app;
CWorkbooks books;
CWorkbook book;
CWorksheets sheets;
CWorksheet sheet;
CRange range;
CFont0 font;
CRange cols;
CRange resizedrange;
Cnterior interior;
CBorders borders;
CBorder bottomborder;
// Start Excel and get an Application object.
if(!app.CreateDispatch("Excel.Application"))
{
AfxMessageBox("Couldn't start Excel and get Application object.");
return;
}
//Get a new workbook.
books = app.get_Workbooks();
book = books.Add (covOptional);
//Get the first sheet.
sheets =book.get_Sheets();
sheet = sheets.get_Item(COleVariant((short)1));
//Fill cells A1, B1, C1, and D1 one cell at a time with "headers".
range = sheet.get_Range(COleVariant("A1"),COleVariant("A1"));
range.put_Value2(COleVariant("First Name"));
range = sheet.get_Range(COleVariant("B1"),COleVariant("B1"));
range.put_Value2(COleVariant("Last Name"));
range = sheet.get_Range(COleVariant("C1"),COleVariant("C1"));
range.put_Value2(COleVariant("Full Name"));
range = sheet.get_Range(COleVariant("D1"),COleVariant("D1"));
range.put_Value2(COleVariant("Salary"));
//Format A1:D1 as bold, vertical alignment = center.
range = sheet.get_Range(COleVariant("A1"), COleVariant("D1"));
font = range.get_Font();
font.put_Bold(covTrue);
range.put_VerticalAlignment(
COleVariant((short)-4108)); //xlVAlignCenter = -4108
//Fill A2:B6 with an array of values (First & Last Names).
{
COleSafeArray saRet;
DWORD numElements[]={5,2}; //5x2 element array
saRet.Create(VT_BSTR, 2, numElements);
//Fill the 5x2 SafeArray with the following data:
// John Smith
// Tom Brown
// Sue Thomas
// Jane Jones
// Adam Johnson
FillSafeArray(L"John", 0, 0, &saRet);
FillSafeArray(L"Smith", 0, 1, &saRet);
FillSafeArray(L"Tom", 1, 0, &saRet);
FillSafeArray(L"Brown", 1, 1, &saRet);
FillSafeArray(L"Sue", 2, 0, &saRet);
FillSafeArray(L"Thomas", 2, 1, &saRet);
FillSafeArray(L"Jane", 3, 0, &saRet);
FillSafeArray(L"Jones", 3, 1, &saRet);
FillSafeArray(L"Adam", 4, 0, &saRet);
FillSafeArray(L"Johnson", 4, 1, &saRet);
range = sheet.get_Range(COleVariant("A2"), COleVariant("B6"));
range.put_Value2(COleVariant(saRet));
saRet.Detach();
}
//Fill C2:C6 with a relative formula (=A2 & " " & B2).
range = sheet.get_Range(COleVariant("C2"), COleVariant("C6"));
range.put_Formula(COleVariant("=A2 & \" \" & B2"));
//Fill D2:D6 with a formula(=RAND()*100000) and apply a number
//format.
range = sheet.get_Range(COleVariant("D2"), COleVariant("D6"));
range.put_Formula(COleVariant("=RAND()*100000"));
range.put_NumberFormat(COleVariant("$0.00"));
//AutoFit columns A:D.
range = sheet.get_Range(COleVariant("A1"), COleVariant("D1"));
cols = range.get_EntireColumn();
cols.AutoFit();
//Manipulate a variable number of columns for Quarterly Sales Data.
{
short NumQtrs;
CString msg;
//Determine how many quarters to display data for.
for(NumQtrs=1;NumQtrs<=3;NumQtrs++)
{
msg.Format("Enter sales data for %d quarter(s)?", NumQtrs);
if(AfxMessageBox(msg,MB_YESNO)==IDYES)
{
break;
}
}
msg.Format("Displaying data for %d quarters.", NumQtrs);
AfxMessageBox(msg);
//Starting at E1, fill headers for the number of columns selected.
range = sheet.get_Range(COleVariant("E1"), COleVariant("E1"));
resizedrange = range.get_Resize(covOptional, COleVariant(NumQtrs));
resizedrange.put_Formula(
COleVariant("=\"Q\" & COLUMN()-4 & CHAR(10) & \"Sales\""));
//Change the Orientation and WrapText properties for the headers.
resizedrange.put_Orientation(COleVariant((short)38));
resizedrange.put_WrapText(covTrue);
//Fill the interior color of the headers.
interior = resizedrange.get_Interior();
interior.put_ColorIndex(COleVariant((short)36));
//Fill the columns with a formula and apply a number format.
range = sheet.get_Range(COleVariant("E2"), COleVariant("E6"));
resizedrange = range.get_Resize(covOptional, COleVariant(NumQtrs));
resizedrange.put_Formula(COleVariant("=RAND()*100"));
resizedrange.put_NumberFormat(COleVariant("$0.00"));
//Apply borders to the Sales data and headers.
range = sheet.get_Range(COleVariant("E1"), COleVariant("E6"));
resizedrange= range.get_Resize(covOptional, COleVariant(NumQtrs));
borders = resizedrange.get_Borders();
borders.put_Weight(COleVariant((short)2)); //xlThin = 2
//Add a Totals formula for the Quarterly sales data and apply a
//border.
range = sheet.get_Range(COleVariant("E8"), COleVariant("E8"));
resizedrange = range.get_Resize(covOptional, COleVariant(NumQtrs));
resizedrange.put_Formula(COleVariant("=SUM(E2:E6)"));
borders = resizedrange.get_Borders();
{
bottomborder = borders.get_Item((long)9);
bottomborder.put_LineStyle(
COleVariant((short)-4119)); //xlDouble = -4119
bottomborder.put_Weight(
COleVariant((short)4)); //xlThick = 4
}
}
//Make the application visible and give the user control of
//Excel.
app.put_Visible(TRUE);
app.put_UserControl(TRUE);
}