Tuesday, August 23, 2011

Excel Value to String

Query: How can i create a generic method for returning string values from excel.

Answer:

// convert into str from excel cell value
str COMVariant2Str(COMVariant _cv,
                    int _decimals = 0,
                        int _characters = 0,
                        int _separator1 = 0,
                        int _separator2 = 0)
{
    switch (_cv.variantType())
    {
        case (COMVariantType::VT_BSTR):
            return _cv.bStr();

        case (COMVariantType::VT_R4):
            return num2str(_cv.float(),_characters,_decimals,_separator1,_separator2);

        case (COMVariantType::VT_R8):
            return num2str(_cv.double(),_characters,_decimals,_separator1,_separator2);

        case (COMVariantType::VT_DECIMAL):
            return num2str(_cv.decimal(),_characters,_decimals,_separator1,_separator2);

        case (COMVariantType::VT_DATE):
            return date2str(_cv.date(),123,2,1,2,1,4);

        case (COMVariantType::VT_EMPTY):
            return "";

        default:
            throw error(strfmt("@SYS26908", _cv.variantType()));
    }
    return "";
}

No comments:

Post a Comment