VC使用Excel创建保存新workbook文档 删除worksheet

 _Application excelApp;
 _Application excelProtectApp;
 Workbooks workbooks;
 _Workbook workbook;
 Worksheets worksheets;
 _Worksheet worksheet;
 // 文件打开选项
COleVariant   vOptional((long)DISP_E_PARAMNOTFOUND,   VT_ERROR);
    // COM初始化
    if(!AfxOleInit())      
    {  
  AfxMessageBox( MSG_LIBRARY_INI_FAILED );  
  return FALSE;
    }
 // Excel 
    // Excel 
    if( !excelApp.CreateDispatch( "Excel.Application" ) )    
    {  
  AfxMessageBox( MSG_EXCEL_CREATE_FAILED );
  return FALSE;
    }
 if( !excelProtectApp.CreateDispatch( "Excel.Application" ) )    
    {  
  AfxMessageBox( MSG_EXCEL_CREATE_FAILED );
  return FALSE;
    }
#ifdef _DEBUG
 excelApp.SetVisible(TRUE);                
    excelApp.SetUserControl(TRUE);
#endif        
 workbooks = excelApp.GetWorkbooks();
//创建新的workbook
 workbook = workbooks.Add( vOptional );
 worksheets = workbook.GetWorksheets();
 while ( worksheets.GetCount() != 1 )
 {
  worksheet = worksheets.GetItem( COleVariant( ( short ) 1 ) );

//删除worksheet
  worksheet.Delete();
 }

//work保存

workbook.SaveAs((COleVariant)(strValue + cstrFileName),
        covOptional, covOptional, covOptional,
        covOptional, covOptional, C_ACCESSMODE, covOptional, covOptional,
        covOptional,covOptional, covOptional);

posted @ 2009-06-30 14:56  dzqabc  阅读(1902)  评论(0编辑  收藏  举报