Looking for example code in C++ that addresses cells in excel

Gavin Jarvie 1 Reputation point
2022-02-17T15:12:47.427+00:00

Hi I'm am looking for c++ code examples that reads/writes to specific cells within Excel. I can read a row of data using getline but that will pull a whole row of data at once. Would be really useful and helpful to see examples that can target specific cells.

Thanks,

Developer technologies C++
{count} votes

2 answers

Sort by: Most helpful
  1. Castorix31 90,521 Reputation points
    2022-02-17T16:53:54.46+00:00

    A way with Excel Interop (test with a random .xls with Office 2016 on Windows10 21H1):

                Excel::_ApplicationPtr pExcel(__uuidof(Excel::Application));
                pExcel->Workbooks->Open(L"E:\\employees.xls");
                // To test
                // pExcel->PutVisible(0, TRUE);
                Excel::_WorksheetPtr pWorkSheet = pExcel->ActiveSheet;
                Excel::RangePtr pRange = pWorkSheet->Cells;
    
                // Read value
                _variant_t va = pRange->Item[2][1];
                _bstr_t bstrText1(va);
                TCHAR wsValue[255];
                wcsncpy_s(wsValue, _countof(wsValue), static_cast<wchar_t const*>(bstrText1), _TRUNCATE);
                TCHAR wsBuffer[512];
                wsprintf(wsBuffer, TEXT("Cell value (A,2) : %s\r\n"), wsValue);
                OutputDebugString(wsBuffer);
                // Write value
                pRange->Item[2][1] = L"Test";
                // Read new value
                va = pRange->Item[2][1];
                _bstr_t bstrText2(va);
                wcsncpy_s(wsValue, _countof(wsValue), static_cast<wchar_t const*>(bstrText2), _TRUNCATE);
                wsprintf(wsBuffer, TEXT("Cell new value (A,2) : %s\r\n"), wsValue);
                OutputDebugString(wsBuffer);
    
                // To avoid "Want to save your changes" Dialog Box
                pExcel->PutDisplayAlerts(LOCALE_USER_DEFAULT, VARIANT_FALSE);            
    
                pExcel->Quit();
    

    At beginning :

    #import "C:\Program Files (x86)\Microsoft Office\root\VFS\ProgramFilesCommonX86\Microsoft Shared\OFFICE16\mso.dll" rename("RGB", "MSRGB") rename("DocumentProperties", "WordDocumentProperties")
    #import "C:\Program Files (x86)\Microsoft Office\root\VFS\ProgramFilesCommonX86\Microsoft Shared\VBA\VBA6\VBE6EXT.OLB" raw_interfaces_only, rename("Reference", "ignorethis"), rename("VBE", "testVBE")
    #import "C:\Program Files (x86)\Microsoft Office\root\Office16\Excel.exe" exclude("IFont", "IPicture") rename("RGB", "ignorethis"), rename("DialogBox", "ignorethis"), rename("VBE", "testVBE"), rename("ReplaceText", "EReplaceText"), rename("CopyFile","ECopyFile"), rename("FindText", "EFindText"), rename("NoPrompt", "ENoPrompt")
    

  2. Minxin Yu 13,501 Reputation points Microsoft External Staff
    2022-02-22T05:48:06.557+00:00

    Hi, @Gavin Jarvie

    Please refer to the tutorial: How to automate Excel from MFC and Visual C++ 2005 or Visual C++ .NET to fill or obtain data in a range using arrays
    You can also try to convert the Excel file to csv format first. In a C++ application, use std::ifstream to open the file, getline reads each comma-separated line, and uses strtok function.

    Best regards,

    Minxin Yu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.