通过MFC写Excel,并给特定的单元格设超链接
//建立超链接
CString cc; //超链接¨地址
cc =_T("http://www.baidu.com/index.php?tn=maxthon2&ch=3");//
range.AttachDispatch(sheet.GetRange(_variant_t(_T("C2")),_variant_t(_T("C2")))); //获得区域
links.AttachDispatch(range.GetHyperlinks()); //获得Hyperlinks对象此句报错 直接中断 找不到有用的错误信息 
links.Add(range,cc,_variant_t(_T("")),_variant_t(_T("")),_variant_t(_T(""))); //设置超链接请大神解惑

解决方案 »

  1.   

    @weixin_38323761 
      

  2.   

    EXCEL中录制宏, 手动操作, 按宏代码编辑程序
    // Excel12.cpp : Defines the entry point for the console application.
    //#include "stdafx.h"
    #include <stdio.h>
    #include <tchar.h>#import "C:/Program Files/Microsoft Office/OFFICE12/mso.dll" \
      rename("RGB", "RBGMSO") rename("SearchPath", "SearchPathMSO") \
      rename("DocumentProperties", "DocumentPropertiesMSO") no_auto_exclude
    #import "C:/Program Files/Microsoft Office/OFFICE12/VBE6EXT.OLB" no_namespace
    #import "C:/Program Files/Microsoft Office/OFFICE12/excel.exe" \
      rename("DialogBox", "ExcelDialogBox") rename("RGB", "ExcelRGB") \
      rename("CopyFile", "ExcelCopyFile") rename("ReplaceText", "ExcelReplaceText") \
      rename("IFont", "IFontXL")void dump_com_error(_com_error &e)
    {
      _tprintf(_T("Oops - hit an error!\n"));
      _tprintf(_T("\a\tCode = %08lx\n"), e.Error());
      _tprintf(_T("\a\tCode meaning = %s\n"), e.ErrorMessage());
      _bstr_t bstrSource(e.Source());
      _bstr_t bstrDescription(e.Description());
      _tprintf(_T("\a\tSource = %s\n"), (LPCTSTR) bstrSource);
      _tprintf(_T("\a\tDescription = %s\n"), (LPCTSTR) bstrDescription);
    }
    struct StartOle {
      StartOle() { CoInitialize(NULL); }
      ~StartOle() { CoUninitialize(); }
    } _inst_StartOle;
    int main(int argc, char* argv[])

      using namespace Office;
      using namespace Excel;
      
      _ApplicationPtr pXL;
      
      try 
      {
        pXL.CreateInstance(L"Excel.Application");
        
        pXL->PutVisible(0, VARIANT_TRUE);
        
        WorkbooksPtr pBooks = pXL->Workbooks;
        _WorkbookPtr pBook  = pBooks->Add((long)xlWorksheet);
        
        _WorksheetPtr pSheet = pXL->ActiveSheet;
       
        pSheet->Name = "Market Share!";
           
        //插入超链接
        try
        {
          //Range("I4").Select
          //ActiveSheet.Hyperlinks.Add Anchor:=Selection, Address:= _
          //  "http://bbs.csdn.net/topics/392326155", TextToDisplay:="测试超链接"
          
          RangePtr pRang = pSheet->Range["I4"];
          HyperlinksPtr pLinks = pSheet->Hyperlinks;
          pLinks->Add(pRang, 
            "http://bbs.csdn.net/topics/392326155", 
            vtMissing, 
            vtMissing,
            "测试超链接");
        }
        catch(_com_error &e) 
        {
          dump_com_error(e);
        }
     
        pSheet->Range["A2"]->Value2 = "Company A";
        pSheet->Range["A3"]->Value2 = 75.0;
        pSheet->Range["A4"]->Value2 = 75.0;
     
        Sleep(6000);
        
        pBook->PutSaved(0, VARIANT_TRUE);
        pXL->Quit();
      } 
      catch(_com_error &e) 
      {
        dump_com_error(e);
      }  return 0;
    }