如何为我的应用程序创建自己的 excel 实例并使用它?

发布于 2024-10-25 07:17:35 字数 131 浏览 1 评论 0原文

我如何为我的应用程序创建自己的实例 excel 并使用它?现在,当我调用 CreateInstance 时出现 com 错误,并且正在处理一个 excel 实例。我想在我的应用程序中使用 excel 实例的全局处理程序,并在我的应用程序关闭时终止它

How I can create for my application one's own instance excel and use it ? Now I have com error when i call CreateInstance and there is a excel instance in process. I want to use for my application the global handler of excel instance and kill it when my application will be close

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦亿 2024-11-01 07:17:35

Excel 允许多个实例。以下代码适用于多个实例(XLXL1)。即使您之前手动启动 Excel,它也能正常工作。您可以展示一个代码示例来澄清您的问题吗?

#import "C:\Program Files (x86)\Common Files\microsoft shared\OFFICE12\mso.dll"
#import "C:\Program Files (x86)\Common Files\microsoft shared\VBA\VBA6\VBE6EXT.OLB"
#import "C:\Program Files (x86)\Microsoft Office\Office12\excel.exe" \
  rename("DialogBox","ExcelDialogBox") rename("RGB","ExcelRGB") \
  exclude("IFont","IPicture")

#include <stdexcept>
#include <iostream>

int main()
{
  CoInitialize(NULL);
  try {
    Excel::_ApplicationPtr XL, XL1;

    HRESULT hr = XL.CreateInstance(L"Excel.Application");
    if(FAILED(hr)) {
      char msg[1024] = {0};
      sprintf(msg, "E: initializing first instance failed: %d", hr);
      throw std::runtime_error(msg);
    }

    Excel::_WorkbookPtr workbook = XL->Workbooks->Add(Excel::xlWorksheet); 
    Excel::_WorksheetPtr worksheet = XL->ActiveSheet;
    worksheet->SaveAs("c:\\test.xls");

    hr = XL1.CreateInstance(L"Excel.Application");
    if(FAILED(hr)) {
      char msg[1024] = {0};
      sprintf(msg, "E: initializing second instance failed: %d", hr);
      throw std::runtime_error(msg);
    }

    workbook->Close();
    XL->Quit();
    XL1->Quit();
  }
  catch(_com_error &ce)
  {
    std::cout<<"caught" << std::endl;
    // Handle the error
  }

  CoUninitialize();

  system("pause");
  return 0;
}

Excel allows multiple instances. The following code works with multiple instances (XL, XL1). And it works even if you previously start Excel manually. Would you show a code sample to clarify your question.

#import "C:\Program Files (x86)\Common Files\microsoft shared\OFFICE12\mso.dll"
#import "C:\Program Files (x86)\Common Files\microsoft shared\VBA\VBA6\VBE6EXT.OLB"
#import "C:\Program Files (x86)\Microsoft Office\Office12\excel.exe" \
  rename("DialogBox","ExcelDialogBox") rename("RGB","ExcelRGB") \
  exclude("IFont","IPicture")

#include <stdexcept>
#include <iostream>

int main()
{
  CoInitialize(NULL);
  try {
    Excel::_ApplicationPtr XL, XL1;

    HRESULT hr = XL.CreateInstance(L"Excel.Application");
    if(FAILED(hr)) {
      char msg[1024] = {0};
      sprintf(msg, "E: initializing first instance failed: %d", hr);
      throw std::runtime_error(msg);
    }

    Excel::_WorkbookPtr workbook = XL->Workbooks->Add(Excel::xlWorksheet); 
    Excel::_WorksheetPtr worksheet = XL->ActiveSheet;
    worksheet->SaveAs("c:\\test.xls");

    hr = XL1.CreateInstance(L"Excel.Application");
    if(FAILED(hr)) {
      char msg[1024] = {0};
      sprintf(msg, "E: initializing second instance failed: %d", hr);
      throw std::runtime_error(msg);
    }

    workbook->Close();
    XL->Quit();
    XL1->Quit();
  }
  catch(_com_error &ce)
  {
    std::cout<<"caught" << std::endl;
    // Handle the error
  }

  CoUninitialize();

  system("pause");
  return 0;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文