2.1.13.18 og_get_excel_sheet_names


Description

Read Excel file to get list of sheet names

Syntax

int og_get_excel_sheet_names( LPCSTR lpcszFilename, StringArray * psaSheets )

Parameters

lpcszFilename
[input] full path Excel file name
psaSheets
[output] array of sheet names

Return

Number of sheets if success, EXCEL_ERR_FILE_NO_FOUND if file not found

Examples

EX1

#include <ocGrid.h>
//run by
//dlgfile g:=*.xls;excel_import fname$;wautosize;
void excel_import(string strFile)
{
    vector<string> vsSheets;
    int nn = og_get_excel_sheet_names(strFile, &vsSheets);
    if(nn <= 0)
        return; // not a valid Excel file
    //import to active book
    WorksheetPage wp = Project.Pages();
    if(!wp)
        return;

    ExcelImportOptions stR;
    stR.xlsctrl.m_dwRXC = RXC_VALUE|RXC_STYLE;
    stR.xlsctrl.m_nMaxNumEmptyColumnsBeyondLastData = 0;
    
    for(int ii = 0; ii < vsSheets.GetSize(); ii++)
    {
        Worksheet wks = wp.Layers(vsSheets[ii]);
        if(!wks)
        {
            int nSht = page_find_empty_sheet(wp);
            if(nSht < 0)
                nSht = wp.AddLayer();
            wks = wp.Layers(nSht);
            wks.SetName(vsSheets[ii]);
        }
        int nErr = wks.ImportExcel(strFile, ii, &stR);
        if(nErr < 0)
        {
            printf("import failed when trying to read %s\n", vsSheets[ii]);
            return;
        }
    }
}

Remark

See Also

Header to Include

ocgrid.h

Reference