Exemplo n.º 1
0
        private Model.Data.DataTable GridToExecl(List <TemplateStock> tempStocks, out List <DataHeader> cellRanges)
        {
            TSGrid hsGrid      = _gridConfig.GetGid(GridStock);
            var    gridColumns = hsGrid.Columns;

            var sheetConfig = ConfigManager.Instance.GetImportConfig().GetSheet("stocktemplate");

            cellRanges = sheetConfig.Columns;
            var sheetColumns = ConfigManager.Instance.GetImportConfig().GetColumnHeader(cellRanges);
            Dictionary <string, DataColumnHeader> colHeadMap = new Dictionary <string, DataColumnHeader>();

            foreach (var column in sheetColumns)
            {
                //colHeadMap.Add(column.Name, column);
                var gridColumn = gridColumns.Find(p => p.Text.Equals(column.Name));
                if (gridColumn != null)
                {
                    if (!colHeadMap.ContainsKey(gridColumn.Name))
                    {
                        colHeadMap.Add(gridColumn.Name, column);
                    }
                }
            }

            //实体类标签名与Excel DataTable DataRow.Columns中列索引映射表
            Dictionary <string, int> excelNameColumnIndexMap = new Dictionary <string, int>();

            for (int i = 0, count = sheetColumns.Count; i < count; i++)
            {
                if (!excelNameColumnIndexMap.ContainsKey(sheetColumns[i].Name))
                {
                    excelNameColumnIndexMap.Add(sheetColumns[i].Name, i);
                }
            }

            Dictionary <string, int> fieldNameColumnIndexMap = new Dictionary <string, int>();

            for (int i = 0, count = gridColumns.Count; i < count; i++)
            {
                var column = gridColumns[i];
                int index  = sheetColumns.FindIndex(p => p.Name.Equals(column.Text));
                if (index >= 0)
                {
                    if (!fieldNameColumnIndexMap.ContainsKey(column.Name))
                    {
                        fieldNameColumnIndexMap.Add(column.Name, index);
                    }
                }
            }

            Model.Data.DataTable table = new Model.Data.DataTable
            {
                ColumnIndex = new Dictionary <string, int>(),
                Rows        = new List <Model.Data.DataRow>()
            };

            table.ColumnIndex = excelNameColumnIndexMap;

            var attFieldMap = GridViewBindingHelper.GetPropertyBinding(typeof(TemplateStock));

            foreach (var tempStock in tempStocks)
            {
                Model.Data.DataRow dataRow = new Model.Data.DataRow
                {
                    Columns = new List <DataValue>(new DataValue[table.ColumnIndex.Count])
                };

                foreach (var kv in attFieldMap)
                {
                    if (!attFieldMap.ContainsKey(kv.Key) || !fieldNameColumnIndexMap.ContainsKey(kv.Key))
                    {
                        continue;
                    }

                    string fieldName = attFieldMap[kv.Key];
                    int    valIndex  = fieldNameColumnIndexMap[kv.Key];

                    var field = tempStock.GetType().GetProperty(fieldName);
                    if (field == null)
                    {
                        continue;
                    }

                    Model.Data.DataValue dataValue = new DataValue();
                    if (colHeadMap.ContainsKey(kv.Key))
                    {
                        dataValue.Type = colHeadMap[kv.Key].Type;
                    }

                    dataValue.Value = field.GetValue(tempStock, null);

                    dataRow.Columns[valIndex] = dataValue;
                    if (valIndex >= 0 && valIndex < dataRow.Columns.Count)
                    {
                        dataRow.Columns[valIndex] = dataValue;
                    }
                }

                table.Rows.Add(dataRow);
            }

            return(table);
        }
Exemplo n.º 2
0
        private List <TemplateStock> ExcelToGrid(Model.Data.DataTable excelData)
        {
            List <TemplateStock> stockList = new List <TemplateStock>();
            var template = GetSelectTemplate();

            if (template == null)
            {
                return(stockList);
            }

            //用于获取二维表表头名字与Excel中表头进行匹配,获取Excel DataTable中列的位置
            TSGrid hsGrid      = _gridConfig.GetGid(GridStock);
            var    gridColumns = hsGrid.Columns;

            //标签与属性名映射表
            var attFieldMap = GridViewBindingHelper.GetPropertyBinding(typeof(TemplateStock));

            //实体类标签名与Excel DataTable DataRow.Columns中列索引映射表
            Dictionary <string, int> fieldNameColumnIndexMap = new Dictionary <string, int>();

            for (int i = 0, count = gridColumns.Count; i < count; i++)
            {
                var    column   = gridColumns[i];
                string origtext = column.Text;
                string text     = origtext;
                if (text.Contains("(%)"))
                {
                    text = text.Replace("(%)", string.Empty);
                }

                if (excelData.ColumnIndex.ContainsKey(origtext))
                {
                    if (!fieldNameColumnIndexMap.ContainsKey(column.Name))
                    {
                        fieldNameColumnIndexMap.Add(column.Name, excelData.ColumnIndex[origtext]);
                    }
                }
                else if (excelData.ColumnIndex.ContainsKey(text))
                {
                    if (!fieldNameColumnIndexMap.ContainsKey(column.Name))
                    {
                        fieldNameColumnIndexMap.Add(column.Name, excelData.ColumnIndex[text]);
                    }
                }
            }

            foreach (var row in excelData.Rows)
            {
                TemplateStock stock = new TemplateStock();
                stock.TemplateNo = template.TemplateId;

                foreach (var kv in fieldNameColumnIndexMap)
                {
                    if (!attFieldMap.ContainsKey(kv.Key) || !fieldNameColumnIndexMap.ContainsKey(kv.Key))
                    {
                        continue;
                    }

                    string fieldName = attFieldMap[kv.Key];
                    int    valIndex  = fieldNameColumnIndexMap[kv.Key];

                    var field = stock.GetType().GetProperty(fieldName);
                    if (field == null || !field.CanWrite)
                    {
                        continue;
                    }

                    DataValueType valType    = DataValueType.String;
                    var           gridColomn = gridColumns.Find(p => p.Name.Equals(kv.Key));
                    if (gridColomn != null)
                    {
                        valType = gridColomn.ValueType;
                    }

                    var val = row.Columns[valIndex];
                    if (val != null)
                    {
                        switch (val.Type)
                        {
                        case DataValueType.Int:
                            field.SetValue(stock, val.GetInt());
                            break;

                        case DataValueType.Float:
                            field.SetValue(stock, val.GetDouble());
                            break;

                        case DataValueType.String:
                            if (valType == DataValueType.Int)
                            {
                                field.SetValue(stock, val.GetInt());
                            }
                            else if (valType == DataValueType.Float)
                            {
                                field.SetValue(stock, val.GetDouble());
                            }
                            else
                            {
                                field.SetValue(stock, val.GetStr());
                            }
                            break;

                        default:
                            break;
                        }
                    }
                    else
                    {
                    }
                }

                if (!string.IsNullOrEmpty(stock.SecuCode))
                {
                    var secuInfo = _securityInfoList.Find(p => p.SecuCode.Equals(stock.SecuCode) && p.SecuType == SecurityType.Stock);
                    if (secuInfo != null)
                    {
                        stock.SecuName = secuInfo.SecuName;
                        stock.Exchange = secuInfo.ExchangeCode;
                    }
                }

                stockList.Add(stock);
            }

            return(stockList);
        }