none
VC操作Excel文件,怎么没法保存 RRS feed

  • 问题

  • 我执行下面代码,

    {

    _Application app;//
     if (!app.CreateDispatch("Excel.Application"))  
     {  
      AfxMessageBox("无法启动Excel服务器");  
     }
       
     app.SetVisible(TRUE); // 使Excel可见  
        app.SetUserControl(TRUE); // 允许其他用户控制Exce

     Workbooks  books;  
     _Workbook  book;  
     Worksheets  sheets;  
     _Worksheet  sheet;  
     LPDISPATCH  lpDisp;  
     Range   range;  
        COleVariant covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);

     CString strFileName="C:\\1.xls";

        //
     lpDisp = books.Open(strFileName, covOptional, covOptional,  covOptional,   covOptional, covOptional, covOptional, covOptional, covOptional, covOptional, covOptional,  
      covOptional, covOptional, covOptional, covOptional);
     
     
     
     // Get the Workbooks collection so that you can add a new workbook
     books.AttachDispatch(app.GetWorkbooks());  // 得到Workbooks
     book = books.Add(covOptional);     // 得到Workbook
     
     // Get the Worksheets collection so that you can get the IDispatch for the first Worksheet
     sheets = book.GetWorksheets();     // 得到Worksheets
     sheet = sheets.GetItem(COleVariant((short)1)); // 得到Worksheet
     
     
     
     range  = sheet.GetRange(COleVariant("A1"), covOptional);  // 获取A1 Range
     range.SetColumnWidth(COleVariant("12"));

     book.SetSaved(TRUE);     // 将Workbook的保存状态设置为已保存,即不让系统提示是否人工保存

        range.ReleaseDispatch();    // 释放Range对象
        sheet.ReleaseDispatch();    // 释放Sheet对象
        sheets.ReleaseDispatch();    // 释放Sheets对象
     
        book.ReleaseDispatch();     // 释放Workbook对象
        books.ReleaseDispatch();    // 释放Workbooks对象
     
        book.Close (covOptional, covOptional,covOptional);// 关闭Workbook对象
        books.Close();           // 关闭Workbooks对象
       
        app.Quit();          // 退出_Application
        app.ReleaseDispatch ();       // 释放_Application 

    }

    程序执行完后,打开1.xls文件发现并没有更改A1的列宽,怎没回事

    2011年3月21日 2:58

答案