Exporting a Spreadsheet as Text in C++

This example demonstrates launching an instance of Statistica, opening a spreadsheet, and exporting it as text.

//BEGIN Windows Specific inclusions #include <windows.h> #include <comdef.h> //needed for COM /*The "#import" directive imports the class library information of the specified file.  In this case, the file "statist.exe" will give us access to the Statistica interface and its documents' library.  You will either need to include this file in your project directory, or hard code the full path to the file in the #import directive.  Lastly, note that "#import" is a Visual C++ compiler specific directive; if you are using a different compiler, then please consult your compiler's documentation on how to import a COM interface.*/

 #import "statist.exe"
 //Exclude rarely-used stuff from Windows headers #define WIN32_LEAN_AND_MEAN //END Windows Specific inclusions
 #include <string> #include <cstdio>
 using namespace std; using namespace Statistica;
 //The following example will open Boston2.sta from the examples directory and //export it as a text file into the same directory
 int main()
 { //Launch Statistica //Initialize COM ::CoInitialize(NULL); 
//set up a pointer to a Statistica application Statistica::_ApplicationPtr pApp;
 //initialize a new Statistica object through COM HRESULT hr = ::CoCreateInstance(__uuidof(Statistica::Application),NULL,CLSCTX_SERVER, __uuidof(Statistica::_Application),reinterpret_cast<void**>(&pApp));
 //verify that COM was able to create an instance of Statistica if (FAILED(hr))
 { ::MessageBox(NULL,"Unable to intialize Statistica", "Initialization Failure",MB_ICONHAND); return EXIT_FAILURE; }
 //get the path of where Boston2.sta is from the path property of the application string SpreadSheetPath = pApp->Path + "\\Examples\\Datasets\\Boston2.sta"; /*pApp is now the pointer that we use to access the application's library of member methods and (public) variables.*/
 try
 { /*get the full path of the spreadsheet and make a text file of the same name into the same directory*/ string::size_type index = SpreadSheetPath.find_last_of(".",string::npos); string TextFilePath = SpreadSheetPath.substr(0,index); TextFilePath += ".txt";
 _SpreadsheetPtr pSpreadsheet = pApp->Spreadsheets->Open(SpreadSheetPath.c_str(), true);
 //export the spreadsheet to a text file pSpreadsheet->ExportText(TextFilePath.c_str(),1,pSpreadsheet->NumberOfCases,1, pSpreadsheet->NumberOfVariables,9,true,true,true,true); }
 /*opening and exporting documents will throw a "_com_error" exception if, for example, the document file doesn't exist*/
 catch(_com_error Err)
 { ::MessageBox(NULL,Err.Description(),pApp->Name,MB_ICONHAND); }
 //clean up after the COM initialization pApp.Release(); ::CoUninitialize(); return EXIT_SUCCESS;
 }