天天看點

VC++寫入excel的簡單介紹(摘錄)

首先先引入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

繼續閱讀