在VC中创建多个Excel工作表++

发布于 2024-10-22 03:35:54 字数 36 浏览 3 评论 0原文

如何在VC++中创建包含多个工作表的Excel文件并保存。

How to create Excel file with multiple worksheet in VC++ and save it.

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

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

发布评论

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

评论(3

是伱的 2024-10-29 03:35:54

答案是XL->Worksheets->Add();工作表以相反的顺序添加。

#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;
    HRESULT hr = XL.CreateInstance(L"Excel.Application");

    Excel::_WorkbookPtr workbook = XL->Workbooks->Add(Excel::xlWorksheet); 
    Excel::_WorksheetPtr worksheet = XL->ActiveSheet;
    worksheet->Name = "last page";

    worksheet = XL->Worksheets->Add(); // adding worksheets!!
    worksheet->Name = "other page";

    worksheet = XL->Worksheets->Add();
    worksheet->Name = "some page";

    worksheet->SaveAs("c:\\test.xls");
    workbook->Close();
    XL->Quit();
  }
  catch(_com_error &ce)
  {
    std::cout<<"caught" << std::endl;
  }

  CoUninitialize();

  system("pause");
  return 0;
}

The answer is XL->Worksheets->Add(); Worksheets are added in reverse order.

#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;
    HRESULT hr = XL.CreateInstance(L"Excel.Application");

    Excel::_WorkbookPtr workbook = XL->Workbooks->Add(Excel::xlWorksheet); 
    Excel::_WorksheetPtr worksheet = XL->ActiveSheet;
    worksheet->Name = "last page";

    worksheet = XL->Worksheets->Add(); // adding worksheets!!
    worksheet->Name = "other page";

    worksheet = XL->Worksheets->Add();
    worksheet->Name = "some page";

    worksheet->SaveAs("c:\\test.xls");
    workbook->Close();
    XL->Quit();
  }
  catch(_com_error &ce)
  {
    std::cout<<"caught" << std::endl;
  }

  CoUninitialize();

  system("pause");
  return 0;
}
白首有我共你 2024-10-29 03:35:54

Excel 从 2003 年起支持 XML。 XML 可以有多个工作表。

您可以从 VC++ 生成 XML 文件。该 XML 可以通过 Excel 作为 Excel 工作表打开。

要查找 XML 的格式/结构,请创建一个 Excel 文件并在几个工作表中输入几个单元格的数据。然后将文件另存为 XML。

您可以编写代码来生成类似的 XML。

Excel supports XML from 2003 onwards. XML can have multiple worksheets.

You can generate an XML file from VC++. This XML cab be opened as an Excel worksheet by Excel.

To look for format/structure of XML, create an excel file and enter data for few cells in couple of sheets. Then save the file as XML.

You can write code to generate similar XML.

空城缀染半城烟沙 2024-10-29 03:35:54

请参阅 Excel 对象模型概述 和/或如何查找和使用 Office 对象模型文档来查找要调用的正确方法。 Workbook 界面中的 SaveAs 和 SaveCopyAs 方法可能正是您正在寻找的。

See Excel Object Model Overview and/or How to find and use Office object model documentation to find the correct methods to call. The SaveAs and SaveCopyAs methods in the Workbook interface are probably what you are looking for.

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