COleVariant
covTrue((short)TRUE),
covFalse((short)FALSE),
covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
sheet.SaveAs(_T("c:\\temp\\book2.xls"),covOptional, 
covOptional, covOptional, covOptional, covOptional,
covOptional, covOptional, covOptional, covOptional);除了第一个参数,其余都用缺省值就好了

解决方案 »

  1.   

    还要继续请教:
      worksheet与workbook,worksheets,workbooks这些有什么区别呀,
    是调用worksheet.saveas,还是workbook.saveas呀,在线等呀
      

  2.   

    _Worksheet sheet;
    //完全的程序:
    void CExcelsaveDlg::OnOK() 
    {
    try
    {
    _Application app; // app is an _Application object.
    _Workbook book; // More object declarations.
    _Worksheet sheet;
    Workbooks books;
    Worksheets sheets;
    Range range; // Used for Microsoft Excel 97 components.
    LPDISPATCH lpDisp; // Often reused variable.
    COleVariant
    covTrue((short)TRUE),
    covFalse((short)FALSE),
    covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
    if(!app.CreateDispatch("Excel.Application"))
    {
    AfxMessageBox("Couldn't CreateDispatch() for Excel");
    return;
    }
    app.SetVisible(TRUE);
    lpDisp = app.GetWorkbooks(); // Get an IDispatch pointer.
    ASSERT(lpDisp);
    books.AttachDispatch(lpDisp); // Attach the IDispatch pointer
    // to the books object.
    lpDisp = books.Open("C:\\temp\\book1.xls", // Test.xls is a workbook.
    covOptional, covOptional, covOptional, covOptional, covOptional,
    covOptional, covOptional, covOptional, covOptional, covOptional,
    covOptional, covOptional , covOptional, covOptional); // Return Workbook's IDispatch
    // pointer.
    book.AttachDispatch( lpDisp );
    lpDisp = book.GetSheets();
    ASSERT(lpDisp);
    sheets.AttachDispatch(lpDisp);
    // Get sheet #1 and attach the IDispatch pointer to your sheet
    // object.
    lpDisp = sheets.GetItem( COleVariant((short)(1)) ); 
    ASSERT(lpDisp);
    sheet.AttachDispatch(lpDisp);

    lpDisp = sheet.GetRange(COleVariant("B2"), COleVariant("b6"));
    range.AttachDispatch(lpDisp);
    range.SetNumberFormat(COleVariant("@"));
    range.SetItem(COleVariant((long)(1)),COleVariant((long)(1)),COleVariant(LPCTSTR("000666"))); 
    //void _Worksheet::SaveAs(LPCTSTR Filename, const VARIANT& FileFormat, 
    // const VARIANT& Password, const VARIANT& WriteResPassword, 
    // const VARIANT& ReadOnlyRecommended, const VARIANT& CreateBackup, 
    // const VARIANT& AddToMru, const VARIANT& TextCodepage, 
    // const VARIANT& TextVisualLayout, const VARIANT& Local)
    sheet.SaveAs(_T("c:\\temp\\book2.xls"),covOptional, 
    covOptional, covOptional, covOptional, covOptional,
    covOptional, covOptional, covOptional, covOptional);
    // Release dispatch pointers.
    range.ReleaseDispatch();
    sheet.ReleaseDispatch();
    // This is not really necessary because
    // the default second parameter of AttachDispatch releases
    // when the current scope is lost.

    } // End of processing.

    catch(COleException *e)
    {
    char buf[1024]; // For the Try...Catch error message.
    sprintf(buf, "COleException. SCODE: %08lx.", (long)e->m_sc);
    ::MessageBox(NULL, buf, "COleException", MB_SETFOREGROUND | MB_OK);
    }

    catch(COleDispatchException *e)
    {
    char buf[1024]; // For the Try...Catch error message.
    sprintf(buf,
    "COleDispatchException. SCODE: %08lx, Description: \"%s\".",
    (long)e->m_wCode,(LPSTR)e->m_strDescription.GetBuffer(512));
    ::MessageBox(NULL, buf, "COleDispatchException",
    MB_SETFOREGROUND | MB_OK);
    }

    catch(...)
    {
    ::MessageBox(NULL, "General Exception caught.", "Catch-All",
    MB_SETFOREGROUND | MB_OK);

    CDialog::OnOK();
    }
      

  3.   

    book.SaveAs(COleVariant("c:\\temp\\book3.xls"),covOptional, 
    covOptional, covOptional, covOptional, covOptional,
    0, covOptional, covOptional,covOptional,
    covOptional, covOptional);
      

  4.   

    masterz,非常感谢您的帮助,你能讲讲book与sheet的区别吗?
    还有这方面的资料哪儿可以找到呀,我要做的还有很多,
    如:我要每天产生这样的一个报表,而且每小时写一次,不必要将excel显示给用户,因为不让用户编辑的,到第二天重新生成一个报表,一直下去....
    有这方面的程序吗,给我发一个吧,多谢了,bow!
    email:[email protected]