首先先引入excel application的几个类
通过菜单 View->ClassWizard->Add Class->From a type library 引入excel的olb文件,一般在office目录下,可能为excel8.olb,excel9.olb,xlen32.olb。导入类 applicaton worksheets _worksheet workbooks _workbook
在你的程序中先include该类文件的头文件。
在序入口点添加:
if(!AfxOleInit()){
AfxMessageBox("Could not initialize COM dll");
return FALSE;
}//装载ole
在所要用到的dialog装载代码出添加:
if(!app.CreateDispatch("Excel.Application")){
AfxMessageBox("Couldn't start Excel and get Application object.");
return FALSE;
}//装载excel对象
主代码:
_Application app;
COleVariant
covTrue((short)TRUE),
covFalse((short)FALSE),
covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
Workbooks books;
_Workbook book;
Worksheets sheets;
_Worksheet sheet;
Range range;
Range cols;
books = app.GetWorkbooks();
book = books.Add (covOptional);
sheets =book.GetSheets();
sheet = sheets.GetItem(COleVariant((short)1));
//Fill cells A1, B1, C1, and D1 one cell at a time with "headers".
range = sheet.GetRange(COleVariant("A1"),COleVariant("A1"));
range.SetValue(COleVariant("First Name"));
range = sheet.GetRange(COleVariant("B1"),COleVariant("B1"));
range.SetValue(COleVariant("Last Name"));
range = sheet.GetRange(COleVariant("C1"),COleVariant("C1"));
range.SetValue(COleVariant("Full Name"));
range = sheet.GetRange(COleVariant("D1"),COleVariant("D1"));
range.SetValue(COleVariant("Salary"));
//Format A1:D1 as bold, vertical alignment = center.
range = sheet.GetRange(COleVariant("A1"), COleVariant("D1"));
font = range.GetFont();
font.SetBold(covTrue);
range.SetVerticalAlignment(
COleVariant((short)xlVAlignCenter));
app.SetVisible(TRUE);
app.SetUserControl(TRUE);
在office2003的excel对象中 没有_application _workbook _worksheet 代替的是application workbook worksheet