请问如何实现VC对Excel的控制

lyj 2000-02-20 02:52:00
我想第一步应该是向工程中导入excel8.olb文件,却不知道接下来该使用哪些导入类和使用方法,请赐教!
...全文
187 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
softdoctor 2000-02-20
  • 打赏
  • 举报
回复
可以 用如下方法实现:

// NOTE: This example will only work with Excel8 in Office97
// Compile with cl /GX comexcel.cpp
// TO DO: Edit the #import paths

#import <mso97.dll> no_namespace rename("DocumentProperties", "DocumentPropertiesXL")
#import <vbeext1.olb> no_namespace
#import <excel8.olb> rename("DialogBox", "DialogBoxXL") rename("RGB", "RBGXL") rename("DocumentProperties", "DocumentPropertiesXL") no_dual_interfaces

#include <stdio.h>
#include <tchar.h>

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);
}

// If this is placed in the scope of the smart pointers, they must be
// explicitly Release(d) before CoUninitialize() is called. If any reference
// count is non-zero, a protection fault will occur.
struct StartOle {
StartOle() { CoInitialize(NULL); }
~StartOle() { CoUninitialize(); }
} _inst_StartOle;

void main()
{
using namespace Excel;

_ApplicationPtr pXL;

try {
pXL.CreateInstance(L"Excel.Application.8");

pXL->Visible = VARIANT_TRUE;

WorkbooksPtr pBooks = pXL->Workbooks;
_WorkbookPtr pBook = pBooks->Add((long)xlWorksheet);

_WorksheetPtr pSheet = pXL->ActiveSheet;

try {
// This one will fail
pSheet->Name = "Market Share?";
} catch (_com_error &e) {
dump_com_error(e);
}

pSheet->Name = "Market Share!";

pSheet->Range["A2"]->Value = "Company A";
pSheet->Range["B2"]->Value = "Company B";
pSheet->Range["C2"]->Value = "Company C";
pSheet->Range["D2"]->Value = "Company D";

pSheet->Range["A3"]->Value = 75.0;
pSheet->Range["B3"]->Value = 14.0;
pSheet->Range["C3"]->Value = 7.0;
pSheet->Range["D3"]->Value = 4.0;

Sleep(1000);

RangePtr pRange = pSheet->Range["A2:D3"];
_ChartPtr pChart = pBook->Charts->Add();

pChart->ChartWizard((Range*) pRange, (long) xl3DPie, 7L, (long) xlRows,
1L, 0L, 2L, "Market Share");

Sleep(6000);

pBook->Saved = VARIANT_TRUE;
pXL->Quit();
} catch(_com_error &e) {
dump_com_error(e);
}
}

Sniper 2000-02-20
  • 打赏
  • 举报
回复
用ODBC编程可以吗?
茂奇软件 2000-02-20
  • 打赏
  • 举报
回复
use ole
OleCreate

in VB. you can use.
Set ExcelSheet = CreateObject("Excel.Sheet")
土豆 2000-02-20
  • 打赏
  • 举报
回复

16,471

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧