如何改变Excel表格中某一个格子的颜色

解决方案 »

  1.   

    ...
    Range oRange;
    ...//让oRange选中某个单元格
    Font oFont2;
    oFont2=oRange.GetFont();
    oFont2.SetBackground(COleVariant((long)0x0000FF));
      

  2.   

    Range 是个什么类型的变量?需要哪个头文件我现在的情况是这样的,我要把Listview中的数据保存成EXCEL文件,保存好以后要把其中几个单元格设置成别的颜色,不知道该如何设置颜色。能不能给出具体的代码
      

  3.   

    Range是什么类型的变量,需要哪个头文件?我现在的情况是这样的,要把Listview中的数据保存到EXCEL表中,保存好以后要把其中几个单元格的背景色改成别的颜色,不知道应该怎么该。能不能给出具体的代码
      

  4.   

    学一下office自动化编程,头文件是msexcel9.h
      

  5.   

    gzshd(郁闷) ,请问哪里能够下载office自动化编程方面的资料
      

  6.   

    /////////////////////////////////////////////////////////////////////
    //change font color of a Cell in excel, change cell background color, set border
    /////////////////////////////////////////////////////////////////////
    void CCellcolorDlg::OnOK()
    {//HOWTO: Create Automation Project Using MFC and a Type Library  Q178749
    //change font color of a Cell in excel, change cell background color, set border
    //Do not forget to call AfxOleInit();
    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("B3"), COleVariant("b3"));
    range.AttachDispatch(lpDisp);
    // set number format
    // range.SetNumberFormat(COleVariant("@"));
    // range.SetItem(COleVariant((long)(1)),COleVariant((long)(1)),COleVariant(LPCTSTR("000666")));
    // set cell font here
    Font newfont;
    lpDisp=range.GetFont();
    newfont.AttachDispatch(lpDisp);
    //newfont.SetName(COleVariant("宋体"));
    //newfont.SetSize(COleVariant((long)24));
    newfont.SetColorIndex(COleVariant((short)3));//use VBA to see index value of a certain color
    newfont.ReleaseDispatch();//set background color
    lpDisp=range.GetInterior();
    Interior cellinterior;
    cellinterior.AttachDispatch(lpDisp);
    cellinterior.SetColorIndex(COleVariant((short)6));//use VBA to see index value of a certain color
    cellinterior.ReleaseDispatch();
    // Set border
    //xlDiagonalDown =5 xlDiagonalUp=6 xlEdgeLeft=7
    //xlNone = -4142 xlContinuous=1 xlThin=2 xlAutomatic=-4105
    //xlEdgeTop =8 xlEdgeBottom =9 xlEdgeRight=10
    lpDisp = range.GetBorders();
    Borders bds;
    bds.AttachDispatch(lpDisp);
    Border bd;
    lpDisp = bds.GetItem(8);
    bd.AttachDispatch(lpDisp);
    bd.SetLineStyle(COleVariant((short)1));// // 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();
    }
      

  7.   

    gzshd(郁闷),msexcel9.h这个头文件找不到啊masterz(),你的代码需要哪些头文件?
      

  8.   

    read the following and you'll know where is msexcel9.h
    support.microsoft.com/support/ kb/articles/q178/7/49.asp
      

  9.   

    masterz() ,老大,能不能解释一下啊,微软的网站上找起来太麻烦了。如果你觉得麻烦不愿意解释,那帮个忙,把微软网站上的原文拷贝下来吧
      

  10.   

    头文件已经找到了,不过每次执行到app.CreateDispatch("Excel.Application")时就会出现异常。
        
        是不是只能操作EXCEL97啊,能操作EXCEL2000吗
      

  11.   

    app.CreateDispatch("Excel.Application")每次都执行失败,谁能解释一下啊