Sample C++ Code: Formula Calculations with Chart

 

#include "stdafx.h"

 

#import <Pagos.Spreadsheet.COM.dll>

 

using namespace PagosSpreadSheet;

using namespace std;

 

//    Creates PSC Application interface

void CreatePSCInterface(IApplication **app)

{

      //    Initialize COM subsystem

      CoInitialize(NULL);

 

      //    Accessing com object

      HRESULT hr = CoCreateInstance(__uuidof(Application), NULL, CLSCTX_ALL, __uuidof(IApplication), static_cast<void**>(static_cast<void*>(app)));

 

      //    Checking for error

      if(!SUCCEEDED(hr))

      {

            LPVOID lpMsgBuf;

            FormatMessage(

                  FORMAT_MESSAGE_ALLOCATE_BUFFER |

                  FORMAT_MESSAGE_FROM_SYSTEM |

                  FORMAT_MESSAGE_IGNORE_INSERTS,

                  NULL,

                  hr,

                  0, // Default language

                  (LPTSTR) &lpMsgBuf,

                  0,

                  NULL

                  );         

            wcout << ((wchar_t *)lpMsgBuf);

            LocalFree( lpMsgBuf );

            cout << "Error while creating com object." << endl;

            system("PAUSE");

            exit(-1);

      }

}

 

int main(int argc, char* argv[])

{

     

      try

      {

            long start = GetTickCount();

 

            IApplication *app = NULL;

 

            //Create an instance of the application

            CreatePSCInterface(&app);

 

            //Add a workbook

            IWorkbooksPtr wbs = app->GetWorkbooks();

 

            //Open quote template workbook

            wbs->Open("..\\Spreadsheets\\RetirementWithChart\\Retirement_chart.xls");

 

            IWorkbookPtr wb = wbs->GetItem(1);

 

            //Set input values          

            float val;

 

            std::cout << "Monthly_savings: ";

            std::cin >> val;

            wb->GetNames()->Item["Monthly_savings"]->Range->Value = val;

 

            std::cout << "Annual_Investment_growth_rate: ";

            std::cin >> val;

            wb->GetNames()->Item["Annual_Investment_growth_rate"]->Range->Value = val;

 

            std::cout << "Annual_Salary_Increase_Rate: ";

            std::cin >> val;

            wb->GetNames()->Item["Annual_Salary_Increase_Rate"]->Range->Value = val;

 

            wb->GetWorksheets()->GetItem(1)->GetChartObjects()->GetItem(1)->GetChart()->SaveAs("..\\Spreadsheets\\Temp\\chart.jpg");

 

            std::cout << "Chart saved to temp directory as chart.jpg." << std::endl;

 

            //Close workbook

            wb->Close();

      }

      catch(_com_error &e)

      {

            cout << e.Description() << endl;

      }

 

      CoUninitialize();

      system("PAUSE");

      return 0;

}