vc6操作Excel2003

void CTestExcelDlg::OnButton1()
{
 // TODO: Add your control notification handler code here
 _Application app;
 Workbooks books;
 _Workbook book;
 Worksheets sheets;
 _Worksheet sheet;
 Range range;
 Font font;
 Range cols;

 
 if( !app.CreateDispatch("Excel.Application") ){
  this->MessageBox("无法创建Excel应用!");
  return;
 }
 books.AttachDispatch(app.GetWorkbooks(),true);
 app.SetVisible(false);
 COleVariant covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);

 book.AttachDispatch(books.Add(covOptional));
 sheets.AttachDispatch(book.GetWorksheets(),true);
 app.SetVisible(true);
 sheet.AttachDispatch(sheets.GetItem(_variant_t((long)1)));
 range.AttachDispatch(sheet.GetCells(),true);
 range.SetItem(_variant_t((long)1),_variant_t((long)1),_variant_t("test"));
 sheet.AttachDispatch(sheets.GetItem(_variant_t((long)2)));
 range.AttachDispatch(sheet.GetCells(),true);
 range.SetItem(_variant_t((long)1),_variant_t((long)1),_variant_t("test"));
 sheet.AttachDispatch(sheets.GetItem(_variant_t((long)3)));
 range.AttachDispatch(sheet.GetCells(),true);
 sheet.Copy(vtMissing,_variant_t(sheet));

 app.SetUserControl(TRUE);

 book.SaveAs(COleVariant("C://a.xls"),covOptional,
  covOptional,covOptional,
  covOptional,covOptional,(long)0,covOptional,covOptional,covOptional,covOptional,
  covOptional);
}

你可能感兴趣的:(c,Excel,application,books)