c++excelfile-handling

how to handle excel files using C++?


I'm new to C++, And I want to enter values into an excel spreadsheet using C++, I know we can handle files using fstream but how to get a specific column or row using this method.


Solution

  • If you want to persist in using C++ (despite the comments above), this sample will give you an idea of the coding work needed to use C++ to automate the Excel application (as you might do in VBA or C#) rather than manipulate the file using a known file format (using a third-party library). The sample opens an existing worksheet in the background, adds 1 to the value in cell A1 on Sheet1, and then saves it.

    Whether this is a suitable or efficient solution for your case will depend on what exactly you are trying to do with the files.

    NB. Only works with the MS Visual Studio compiler.

    The hard-coded paths to the import libraries may be different on your computer, and may depend on your Excel version.

    //Import all the type libraries
    
    #import "C:\Program Files\Microsoft Office\Root\VFS\ProgramFilesCommonX86\Microsoft Shared\OFFICE16\MSO.dll" \
        rename("RGB","RGB_mso") rename("DocumentProperties","DocumentProperties_mso") 
    
    using namespace Office;
    
    #import "C:\Program Files\Microsoft Office\root\vfs\ProgramFilesCommonX86\Microsoft Shared\VBA\VBA6\VBE6EXT.OLB"
    
    using namespace VBIDE;
    
    #import "C:\Program Files\Microsoft Office\root\Office16\EXCEL.EXE" \
        rename( "DialogBox", "ExcelDialogBox" ) \
        rename( "RGB", "ExcelRGB" ) \
        rename( "CopyFile", "ExcelCopyFile" ) \
        rename( "ReplaceText", "ExcelReplaceText" ) \
        exclude( "IFont", "IPicture" )
    
    #include <iostream>
    using namespace std;
    
    int main()
    {
        HRESULT hr = CoInitializeEx(0, COINIT_MULTITHREADED);
      
        Excel::_ApplicationPtr pXL;
        if (FAILED(pXL.CreateInstance("Excel.Application")))
        {
            cout << "Could not create instance of Excel" << endl;
            return 1;
        }
    
        try
        {
            //Uncomment this to see what is going on during each step
            //pXL->Visible = true;
    
            Excel::_WorkbookPtr pWb = pXL->Workbooks->Open(L"c:\\temp\\testbook.xlsx");
    
            //Gets number from cell A1 in Sheet1 and increments
            Excel::_WorksheetPtr pSheet = pWb->Worksheets->Item[L"Sheet1"];
            Excel::RangePtr pRng = pSheet->Cells;
            _variant_t val = pRng->Item[1][1];
            double dVal{ val };
            pRng->Item[1][1] = ++dVal;
    
            pWb->Save();
            pWb->Close();
        }    
        catch (_com_error ce) 
        {
            cout << "Something went wrong" << endl;
            _bstr_t bstrDesc = ce.Description();
            if( ! bstrDesc )
            {
                cout << "  Unknown Error" << endl;
            }
            else
            {
                cout << "  Error text: " << bstrDesc << endl;
            }
        }
    
        pXL->Quit();
    }
    

    EDIT: In answer to the unspoken question why is it Excel::_ApplicationPtr, Excel::_WorkbookPtr etc, but for a Range it is Excel::RangePtr (no _)? Absolutely no idea.