vadivelu
vadivelu

Reputation: 508

Create Multiple Excel sheet in VC++

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

Upvotes: 0

Views: 6369

Answers (3)

Oleg Svechkarenko
Oleg Svechkarenko

Reputation: 2516

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

Upvotes: 1

kbjorklu
kbjorklu

Reputation: 1398

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.

Upvotes: 0

Shamit Verma
Shamit Verma

Reputation: 3827

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.

Upvotes: 0

Related Questions