1.Ctrl+W 执行 ClassWizard
2.Add Class.../From a type Library... 在 Office 目录中,找到你想使用的类型库。(excel.exe)根据需要添加,(可以全部)(2003)
3.包含头文件#include "excel.h"
4.详见msdn:Automating Microsoft Office 97 and Microsoft Office 2000(搜索主题)->参见MFC example
5.拷贝msdn代码测试
//添加第5列
oRange = oSheet.GetRange(COleVariant("F1"), vOpt);
oRange.SetValue2(COleVariant("YYY"));
Font oFont = oRange.GetFont();
oFont.SetSize(COleVariant((long)50));
oFont.SetColor(COleVariant((long)0xFF0000));
//设置链接
- void CExcelTest8Dlg::OnBtnExcel()
- {
- // TODO: Add your control notification handler code here
- COleVariant vOpt((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
- _Application oApp;
- oApp.CreateDispatch("Excel.Application");
- if (!oApp)
- {
- AfxMessageBox("Cannot start Excel.");
- return;
- }
- //Get the Workbooks collection so that you can add a new workbook
- Workbooks oBooks = oApp.GetWorkbooks();
- _Workbook oBook = oBooks.Add(vOpt);
- //Get the Worksheets collection of the new Workbook so that
- //you can get the IDispatch for the first WorkSheet
- Worksheets oSheets = oBook.GetWorksheets();
- _Worksheet oSheet = oSheets.GetItem(COleVariant((short)1));
- //*** ADD DATA TO THE WORKSHEET
- //Add Headers to Row 1 of the worksheet
- Range oRange;
- oRange = oSheet.GetRange(COleVariant("A1"), vOpt);
- oRange.SetValue2(COleVariant("Date"));
- oRange = oSheet.GetRange(COleVariant("B1"), vOpt);
- oRange.SetValue2(COleVariant("Order #"));
- oRange = oSheet.GetRange(COleVariant("C1"), vOpt);
- oRange.SetValue2(COleVariant("Amount"));
- oRange = oSheet.GetRange(COleVariant("D1"), vOpt);
- oRange.SetValue2(COleVariant("Tax"));
- //Create a safe array that is NUMROWS x 3 --
- //column 1 will contain dates column 2 will contain strings
- //and column 2 will contain numbers
- COleSafeArray sa;
- DWORD dwElements[2];
- dwElements[0]= NUMROWS; //Number of rows
- dwElements[1]= 3; //Number of columns
- sa.Create(VT_VARIANT, 2, dwElements);
- //Populate the safe array with the data
- long index[2];
- long lRow;
- COleVariant vTemp;
- COleDateTime vDateTime;
- CString s;
- for(lRow=0;lRow<=NUMROWS-1;lRow++)
- {
- index[0] = lRow;
- //Fill the first column with dates
- index[1] = 0;
- vDateTime.SetDate(1999, rand()%12, rand()%28);
- sa.PutElement(index, (COleVariant)vDateTime);
- //Fill the second column with strings
- index[1] = 1;
- s.Format("ORDR%d", lRow+1000);
- vTemp = s;
- sa.PutElement(index, vTemp);
- //Fill the third column with numbers
- index[1] = 2;
- vTemp = (long)rand();
- sa.PutElement(index, vTemp);
- }
- //Fill a range, starting at A2 with the data in
- //the safe array
- oRange = oSheet.GetRange(COleVariant("A2"), vOpt);
- oRange = oRange.GetResize(COleVariant((short)NUMROWS),
- COleVariant((short)3));
- oRange.SetValue2(sa);
- sa.Detach();
- //*** ADD FORMULAS TO THE WORKSHEET
- //Fill the fourth column with a formula to compute the
- //sales tax. Note that the formula uses a "relative"
- //cell reference so that it fills properly.
- oRange = oSheet.GetRange(COleVariant("D2"), vOpt);
- oRange = oRange.GetResize(COleVariant((long)NUMROWS),
- COleVariant((long)1));
- oRange.SetFormula(COleVariant("=C2*0.07"));
- //*** FORMAT THE WORKSHEET
- oRange = oSheet.GetRange(COleVariant("A1"), COleVariant("D1"));
- Font oFont = oRange.GetFont();
- oFont.SetBold(COleVariant((short)TRUE));//Apply Bold to Headers
- oRange = oRange.GetEntireColumn();
- oRange.AutoFit(); //AutoFit the columns 1:4
- //Make Excel visible and give the user control
- oApp.SetVisible(TRUE);
- oApp.SetUserControl(TRUE);
- }
补充:
HYPERLINK的格式和Excel中的文件格式差不多,只是在vc中要在字符串中加入转义字符