我想做个程序,从自己的程序里可以打开WORD和EXCEL文件,查看文件内容,我不想用浏览器来做,听说可以用OLE来实现,想请教哪位有这方面的资料可以指导一下?

解决方案 »

  1.   

    恩,通过ole方式是比较直接的方式。vc操作office,有很多资料。楼主可以google一下:vc操作word, vc操作excel。
      

  2.   

    http://www.cnblogs.com/henryzc/archive/2005/03/20/122145.aspx
      

  3.   

    1、创建MFC对话框应用程序,在向导的第2步选择automation,其他保持默认即可。
    2,在应用程序的InitInstance()中初始化OLE,代码如下:
    // Initialize OLE libraries
    if (!AfxOleInit())
    {
        AfxMessageBox("Failed to initialize OLE");
        return FALSE;
    }
    3、运用类向导添加execl类型库,类型库默认在"C:\Program Files\Microsoft Office\Office11
    4.添加ole库中各个变量。实现
      

  4.   


    1void CControlExcelDoc::OnControlExcel() 
     2{
     3    // TODO: Add your command handler code here
     4    COleVariant vtOptional((long)DISP_E_PARAMNOTFOUND,
     5        VT_ERROR),vtTrue((short)TRUE),vtFalse((short)FALSE);
     6    _Application *ExcelApp = new _Application;
     7
     8    ExcelApp->CreateDispatch("excel.application");
     9
    10    //设置为显示
    11    ExcelApp->SetVisible(TRUE);
    12    //得到WorkBooks
    13    Workbooks  ExcelBook=ExcelApp->GetWorkbooks();
    14    Sheets     ExcelSheet=ExcelBook.Add(vtOptional);
    15    _Workbook  workBook;
    16    _Worksheet workSheet;
    17    Range    range;
    18    workBook.AttachDispatch(ExcelApp->GetApplication());
    19    ExcelSheet=workBook.GetSheets();
    20
    21    ////////////////////////////////////////////
    22    workSheet=ExcelSheet.GetItem(COleVariant((short)1));
    23    workSheet.Activate();
    24
    25
    26    Range myallrange=workSheet.GetRange(COleVariant("A1"),COleVariant("D1"));
    27    //myallrange.BorderAround(COleVariant((short)1),(long)2,(long)1,vtOptional);
    28    myallrange.SetHorizontalAlignment(COleVariant((short)3));
    29    myallrange.Merge(COleVariant((short)1));
    30    myallrange.SetValue(COleVariant("个人信息表"));
    31
    32    Range myrange=workSheet.GetRange(COleVariant("A2"),COleVariant("A2"));
    33    myrange.SetValue(COleVariant("姓名"));
    34    myrange.BorderAround(COleVariant((short)1),(long)2,(long)1,vtOptional);
    35    myrange.SetHorizontalAlignment(COleVariant((short)3));
    36    myrange.SetVerticalAlignment(COleVariant((short)2));
    37    myrange.SetColumnWidth(COleVariant((short)15));
    38
    39    myrange=workSheet.GetRange(COleVariant("B2"),COleVariant("B2"));
    40    myrange.SetValue(COleVariant("性别"));
    41    myrange.BorderAround(COleVariant((short)1),(long)2,(long)1,vtOptional);
    42    myrange.SetHorizontalAlignment(COleVariant((short)3));
    43    myrange.SetVerticalAlignment(COleVariant((short)2));
    44    myrange.SetColumnWidth(COleVariant((short)15));
    45
    46    myrange=workSheet.GetRange(COleVariant("C2"),COleVariant("C2"));
    47    myrange.SetValue(COleVariant("QQ"));
    48    myrange.BorderAround(COleVariant((short)1),(long)2,(long)1,vtOptional);
    49    myrange.SetHorizontalAlignment(COleVariant((short)3));
    50    myrange.SetVerticalAlignment(COleVariant((short)2));
    51    myrange.SetColumnWidth(COleVariant((short)15));
    52
    53    myrange=workSheet.GetRange(COleVariant("D2"),COleVariant("D2"));
    54    myrange.SetValue(COleVariant("Email"));
    55    myrange.BorderAround(COleVariant((short)1),(long)2,(long)1,vtOptional);
    56    myrange.SetHorizontalAlignment(COleVariant((short)3));
    57    myrange.SetVerticalAlignment(COleVariant((short)2));
    58    myrange.SetColumnWidth(COleVariant((short)17));
    59
    60    CString str;
    61    for(int i=3;i<=500;i++)
    62    {
    63        str.Format("A%d",i);
    64        Range myrange=workSheet.GetRange(COleVariant(str),COleVariant(str));
    65        myrange.SetValue(COleVariant("名字"));
    66        myrange.BorderAround(COleVariant((short)1),(long)2,(long)1,vtOptional);
    67        myrange.SetHorizontalAlignment(COleVariant((short)3));
    68        myrange.SetVerticalAlignment(COleVariant((short)2));
    69
    70        str.Format("B%d",i);
    71        myrange=workSheet.GetRange(COleVariant(str),COleVariant(str));
    72        myrange.SetValue(COleVariant("男"));
    73        myrange.BorderAround(COleVariant((short)1),(long)2,(long)1,vtOptional);
    74        myrange.SetHorizontalAlignment(COleVariant((short)3));
    75        myrange.SetVerticalAlignment(COleVariant((short)2));
    76
    77        str.Format("C%d",i);
    78        myrange=workSheet.GetRange(COleVariant(str),COleVariant(str));
    79        myrange.SetValue(COleVariant("12345678"));
    80        myrange.BorderAround(COleVariant((short)1),(long)2,(long)1,vtOptional);
    81        myrange.SetHorizontalAlignment(COleVariant((short)3));
    82        myrange.SetVerticalAlignment(COleVariant((short)2));
    83
    84        str.Format("D%d",i);
    85        myrange=workSheet.GetRange(COleVariant(str),COleVariant(str));
    86        myrange.SetValue(COleVariant("[email protected]"));
    87        myrange.BorderAround(COleVariant((short)1),(long)2,(long)1,vtOptional);
    88        myrange.SetHorizontalAlignment(COleVariant((short)3));
    89        myrange.SetVerticalAlignment(COleVariant((short)2));
    90    }
    91
    92    delete ExcelApp;
    93
    94}
      

  5.   

    感谢各位帮助我,我在网上找了下面这段代码来打开一个WORD文件
    BeginWaitCursor();
    CEmbedWordCntrItem* pItem = NULL;
    TRY
    {
    CEmbedWordDoc* pDoc = GetDocument();
    ASSERT_VALID(pDoc);
    pItem = new CEmbedWordCntrItem(pDoc);
    ASSERT_VALID(pItem);
    CLSID clsid;
    if (FAILED(::CLSIDFromProgID(L"Word.document", &clsid)))
    AfxThrowMemoryException();
    if (!pItem->CreateFromFile(m_sPath, clsid))
    AfxThrowMemoryException();
    pItem->openState;
    pItem->DoVerb(OLEIVERB_SHOW, this);
    m_pSelection = pItem;
    pDoc->UpdateAllViews(NULL);
    LPDISPATCH lpDisp;
    lpDisp = pItem->GetIDispatch();
    }
    CATCH(CException, e)
    {
    if (pItem != NULL)
    {
    ASSERT_VALID(pItem);
    pItem->Delete();
    }
    AfxMessageBox(IDP_FAILED_TO_CREATE); 
    }
    END_CATCHEndWaitCursor();
    但打开文件后还是可以编辑保存复制,就跟用WORD打开一个文件一样的效果,不知道该怎么做可以使打开的文件只能浏览,不能编辑保存复制
      

  6.   

    你编码把要打开的文件的文件属性改为只读。和word不word没关系