/******************************************** SetDataType Purpose Sets the data type that the cells text property is to be treaded as. valid values: UGCELLDATA_STRING UGCELLDATA_NUMBER UGCELLDATA_BOOL UGCELLDATA_TIME UGCELLDATA_CURRENCY Params type Return UG_SUCCESS success UG_ERROR fail *********************************************/ int CUGCell::SetDataType(short type) { if ( type == m_dataType && ( m_propSetFlags&UGCELL_DATATYPE_SET )) return UG_SUCCESS; if ( type == UGCELLDATA_NUMBER && m_propSetFlags&UGCELL_STRING_SET ) { CString cellVal = GetText(); StringToNumber( &cellVal, &m_nNumber ); m_string = ""; } else if ( type == UGCELLDATA_TIME && m_propSetFlags&UGCELL_STRING_SET ) { CString cellVal = GetText(); COleDateTime dateTime; dateTime.ParseDateTime( cellVal ); m_nNumber = dateTime; } else if ( type == UGCELLDATA_CURRENCY && m_propSetFlags&UGCELL_STRING_SET ) { if ( m_dataType == UGCELLDATA_NUMBER ) { m_string.Format(_T("%.2f"), m_nNumber ); } else { CString cellVal = GetText(); COleCurrency currency; currency.ParseCurrency( cellVal ); m_nNumber = (double)(currency.m_cur.int64 / 10000); } } m_dataType = type; m_propSetFlags |= UGCELL_DATATYPE_SET; return UG_SUCCESS; }
/*----------------------ADO读取类型的转换----------------------*/ CString vartostr(const _variant_t &var) { CString strValue; switch (var.vt) { case VT_BSTR://字符串 case VT_LPSTR://字符串 case VT_LPWSTR://字符串 strValue = (LPCTSTR)(_bstr_t)var; break; case VT_I1: case VT_UI1: strValue.Format("%d", var.bVal); break; case VT_I2://短整型 strValue.Format("%d", var.iVal); break; case VT_UI2://无符号短整型 strValue.Format("%d", var.uiVal); break; case VT_INT://整型 strValue.Format("%d", var.intVal); break; case VT_I4: //整型 strValue.Format("%d", var.lVal); break; case VT_I8: //长整型 strValue.Format("%d", var.lVal); break; case VT_UINT://无符号整型 strValue.Format("%d", var.uintVal); break; case VT_UI4: //无符号整型 strValue.Format("%d", var.ulVal); break; case VT_UI8: //无符号长整型 strValue.Format("%d", var.ulVal); break; case VT_VOID: strValue.Format("%8x", var.byref); break; case VT_R4://浮点型 strValue.Format("%.4f", var.fltVal); break; case VT_R8://双精度型 strValue.Format("%.8f", var.dblVal); break; case VT_DECIMAL: //小数 strValue.Format("%.8f", (double)var); break; case VT_CY: { COleCurrency cy = var.cyVal; strValue = cy.Format(); } break; case VT_BLOB: case VT_BLOB_OBJECT: case 0x2011: strValue = "[BLOB]"; break; case VT_BOOL://布尔型 strValue = var.boolVal ? "TRUE" : "FALSE"; break; case VT_DATE: //日期型 { DATE dt = var.date; COleDateTime da = COleDateTime(dt); strValue = da.Format("%Y-%m-%d %H:%M:%S"); } break; case VT_NULL://NULL值 strValue = ""; break; case VT_EMPTY://空 strValue = ""; break; case VT_UNKNOWN://未知类型 default: strValue = "UN_KNOW"; break; } return strValue; }
CString vartostr(const _variant_t &var) { CString value; switch (var.vt) { case VT_LPSTR: //×Ö·û´® case VT_BSTR: //×Ö·û´® case VT_LPWSTR: //×Ö·û´® value = (LPCTSTR)(_bstr_t)var; break; case VT_I1: case VT_UI1: value.Format(_T("%d"), var.bVal); break; case VT_I2://¶ÌÕûÐÍ value.Format(_T("%d"), var.iVal); break; case VT_INT://ÕûÐÍ value.Format(_T("%d"), var.intVal); break; case VT_I4: //ÕûÐÍ value.Format(_T("%d"), var.lVal); break; case VT_I8: //³¤ÕûÐÍ value.Format(_T("%d"), var.lVal); break; case VT_UI2://ÎÞ·ûºÅ¶ÌÕûÐÍ value.Format(_T("%u"), var.uiVal); break; case VT_UINT://ÎÞ·ûºÅÕûÐÍ value.Format(_T("%u"), var.uintVal); break; case VT_UI4: //ÎÞ·ûºÅÕûÐÍ value.Format(_T("%u"), var.ulVal); break; case VT_UI8: //ÎÞ·ûºÅ³¤ÕûÐÍ value.Format(_T("%u"), var.ulVal); break; case VT_VOID: value.Format(_T("%8x"), var.byref); break; case VT_R4://¸¡µãÐÍ value.Format(_T("%.4f"), var.fltVal); break; case VT_R8://Ë«¾«¶ÈÐÍ value.Format(_T("%.8f"), var.dblVal); break; case VT_DECIMAL: //СÊý value.Format(_T("%.8f"), (double)var); break; case VT_CY: { COleCurrency cy = var.cyVal; value = cy.Format(); } break; case VT_BOOL://²¼¶ûÐÍ value = var.boolVal ? _T("1") : _T("0"); break; case VT_DATE: //ÈÕÆÚÐÍ { COleDateTime da = COleDateTime(var.date); value = da.Format(_T("%Y-%m-%d %H:%M:%S")); } break; case VT_NULL: case VT_EMPTY: value = _T(""); break; default: _com_error e(TYPE_CAST_ERROR); throw e; } return value.Trim(); }
// even an empty COleCurrency is valid COleCurrency cy; ASSERT(cy.GetStatus() == COleCurrency::valid); // always valid after being set cy.SetCurrency(4, 500); ASSERT(cy.GetStatus() == COleCurrency::valid); // some conversions aren't possible and will // cause an invalid state, like this: CByteArray array; COleVariant varBogus(array); cy = varBogus; ASSERT(cy.GetStatus() == COleCurrency::invalid);
CString vartostr(const _variant_t &var) { CString value; switch (var.vt) { case VT_LPSTR: //字符串 case VT_BSTR: //字符串 case VT_LPWSTR: //字符串 value = (LPCTSTR)(_bstr_t)var; break; case VT_I1: case VT_UI1: value.Format(_T("%d"), var.bVal); break; case VT_I2://短整型 value.Format(_T("%d"), var.iVal); break; case VT_INT://整型 value.Format(_T("%d"), var.intVal); break; case VT_I4: //整型 value.Format(_T("%d"), var.lVal); break; case VT_I8: //长整型 value.Format(_T("%d"), var.lVal); break; case VT_UI2://无符号短整型 value.Format(_T("%u"), var.uiVal); break; case VT_UINT://无符号整型 value.Format(_T("%u"), var.uintVal); break; case VT_UI4: //无符号整型 value.Format(_T("%u"), var.ulVal); break; case VT_UI8: //无符号长整型 value.Format(_T("%u"), var.ulVal); break; case VT_VOID: value.Format(_T("%8x"), var.byref); break; case VT_R4://浮点型 value.Format(_T("%.4f"), var.fltVal); break; case VT_R8://双精度型 value.Format(_T("%.8f"), var.dblVal); break; case VT_DECIMAL: //小数 value.Format(_T("%.8f"), (double)var); break; case VT_CY: { COleCurrency cy = var.cyVal; value = cy.Format(); } break; case VT_BOOL://布尔型 value = var.boolVal ? _T("1") : _T("0"); break; case VT_DATE: //日期型 { COleDateTime da = COleDateTime(var.date); value = da.Format(_T("%Y-%m-%d %H:%M:%S")); } break; case VT_NULL: case VT_EMPTY: value = _T(""); break; default: _com_error e(TYPE_CAST_ERROR); throw e; } return value.Trim(); }