Esempio n. 1
0
        protected string GetFormatedValue(object value, Dlm.Entities.DataStructure.DataType datatype, string format)
        {
            string tmp = value.ToString();

            if (DataTypeUtility.IsTypeOf(value, datatype.SystemType))
            {
                //Type type = Type.GetType("System." + datatype.SystemType);

                switch (DataTypeUtility.GetTypeCode(datatype.SystemType))
                {
                case DataTypeCode.Int16:
                case DataTypeCode.Int32:
                case DataTypeCode.Int64:
                {
                    Int64 newValue = Convert.ToInt64(tmp);
                    return(newValue.ToString(format));
                }

                case DataTypeCode.UInt16:
                case DataTypeCode.UInt32:
                case DataTypeCode.UInt64:
                {
                    UInt64 newValue = Convert.ToUInt64(tmp);
                    return(newValue.ToString(format));
                }

                case DataTypeCode.Decimal:
                case DataTypeCode.Double:
                {
                    Double newValue = Convert.ToDouble(tmp);
                    return(newValue.ToString(format));
                }

                case DataTypeCode.DateTime:
                {
                    DateTime dateTime;

                    return(IOUtility.ExportDateTimeString(value.ToString(), format, out dateTime));
                }

                default: return(tmp);
                }
            }

            return("");
        }
Esempio n. 2
0
        /// <summary>
        /// Convert a VariableValue to Cell
        /// </summary>
        /// <remarks></remarks>
        /// <seealso cref=""/>
        /// <param name="variableValue"></param>
        /// <param name="rowIndex"></param>
        /// <param name="columnIndex"></param>
        /// <returns></returns>
        protected Cell VariableValueToCell(VariableValue variableValue, int rowIndex, int columnIndex)
        {
            using (var uow = this.GetUnitOfWork())
            {
                DataAttribute dataAttribute = uow.GetReadOnlyRepository <Variable>().Query(p => p.Id == variableValue.VariableId).Select(p => p.DataAttribute).FirstOrDefault();



                string message = "row :" + rowIndex + "column:" + columnIndex;
                Debug.WriteLine(message);

                string cellRef = getColumnIndex(columnIndex);

                Cell cell = new Cell();
                cell.CellReference = cellRef;
                cell.StyleIndex    = ExcelHelper.GetExcelStyleIndex(dataAttribute.DataType, styleIndex);
                //cell.DataType = new EnumValue<CellValues>(getExcelType(dataAttribute.DataType.SystemType));
                //cell.CellValue = new CellValue(variableValue.Value.ToString());

                CellValues cellValueType = getExcelType(dataAttribute.DataType.SystemType);
                object     value         = variableValue.Value;

                if (value != null && !(value is DBNull) && cellValueType == CellValues.Number)
                {
                    cell.DataType = new EnumValue <CellValues>(CellValues.Number);

                    try
                    {
                        if (value.ToString() != "")
                        {
                            double d = Convert.ToDouble(value, System.Globalization.CultureInfo.InvariantCulture);
                            cell.CellValue = new CellValue(d.ToString(System.Globalization.CultureInfo.InvariantCulture));
                        }
                    }
                    catch (Exception ex)
                    {
                        throw new Exception(ex.Message + "\n|" + message);
                    }

                    return(cell);
                }
                else
                {
                    if (value != null && !(value is DBNull) && cellValueType == CellValues.Date)
                    {
                        cell.DataType = new EnumValue <CellValues>(CellValues.Number);
                        //CultureInfo provider = CultureInfo.InvariantCulture;
                        try
                        {
                            if (value.ToString() != "")
                            {
                                DateTime dt;
                                if (dataAttribute.DataType != null && dataAttribute.DataType.Extra != null)
                                {
                                    DataTypeDisplayPattern pattern = DataTypeDisplayPattern.Materialize(dataAttribute.DataType.Extra);
                                    if (!string.IsNullOrEmpty(pattern.StringPattern))
                                    {
                                        IOUtility.ExportDateTimeString(value.ToString(), pattern.StringPattern, out dt);
                                        cell.CellValue = new CellValue(dt.ToOADate().ToString());
                                    }
                                    else
                                    {
                                        if (IOUtility.IsDate(value.ToString(), out dt))
                                        {
                                            cell.CellValue = new CellValue(dt.ToOADate().ToString());
                                        }
                                    }
                                }
                                else
                                {
                                    if (IOUtility.IsDate(value.ToString(), out dt))
                                    {
                                        cell.CellValue = new CellValue(dt.ToOADate().ToString());
                                    }
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            throw new Exception(ex.Message + "|" + message);
                        }
                    }
                    else
                    {
                        cell.DataType = new EnumValue <CellValues>(CellValues.String);
                        if (value == null)
                        {
                            cell.CellValue = new CellValue("");
                        }
                        else
                        {
                            cell.CellValue = new CellValue(value.ToString());
                        }
                    }
                }

                return(cell);
            }
        }