This is not my code but essentially this is what you need to do.
#import "C:\Program Files\Common Files\Microsoft Shared\Office11\mso.dll"
#import "C:\Program Files\Common Files\Microsoft Shared\VBA\VBA6\VBE6EXT.OLB"
#import "C:\Program Files\Microsoft Office\Office11\excel.exe" \
rename("DialogBox","ExcelDialogBox") rename("RGB","ExcelRGB") \
exclude("IFont","IPicture")
#include <stdexcept>
#include <iostream>
using namespace std;
using namespace Excel;
int main()
{
// Initialize COM
CoInitialize(NULL);
try
{
_ApplicationPtr excel;
//Excel::_ApplicationPtr excel;
//excel->Visible(true);
excel->put_Visible(10, true);
// excel->put_Visible(true);
// Initialize Excel and make sure it's initialized
HRESULT hr = excel.CreateInstance(L"Excel.Application");
//excel->visible = true;
if(FAILED(hr))
{
char msg[1024] = {0};
sprintf(msg, "E: There was an error initializing Excel: %d", hr);
throw std::runtime_error(msg);
}
cout<< "HERE!";
_WorkbookPtr workbook = excel->Workbooks->Add(static_cast<long>(Excel::xlWorksheet)); // Create the workbook
_WorksheetPtr worksheet = excel->ActiveSheet; // Get the active sheet
// This is how you put the values into the worksheet
worksheet->Range["A1"]->Value = "Hello"; // Set a value
worksheet->SaveAs("c:\\test.xls"); // Save it
workbook->Close(); // Close the workbook
excel->Quit(); // Quit excel
}
catch(_com_error &ce)
{
cout<<"caught";
// Handle the error
}
CoUninitialize();
return 0;
}