1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
|
#import "C:\Program Files\Microsoft Office\Office\MSO9.DLL"
#import "C:\Program Files\Fichiers communs\Microsoft Shared\VBA\VBA6\VBE6EXT.OLB" no_namespace
#import "C:\Program Files\Microsoft Office\Office\EXCEL9.OLB"no_namespace
using namespace std;
int main()
{
// Initialize COM
CoInitialize(NULL);
try
{
Excel::_ApplicationPtr excel;
// Initialize Excel and make sure it's initialized
HRESULT hr = excel.CreateInstance(L"Excel.Application");
if(FAILED(hr))
{
char msg[1024] = {0};
sprintf(msg, "E: There was an error initializing Excel: %d", hr);
throw std::runtime_error(msg);
}
Excel::_WorkbookPtr workbook = excel->Workbooks->Add(static_cast<long>(Excel::xlWorksheet)); // Create the workbook
Excel::_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)
{
// Handle the error
}
CoUninitialize(); |