示例#1
0
        public void GenerateFields <T>(List <T> list, CustomFormat customFormat)
        {
            // Create new stopwatch
            System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch();
            stopwatch.Start();
            foreach (var data in list)
            {
                foreach (var excelColumn in _excelColumns)
                {
                    excelColumn.LastIndex++;

                    var value          = Convert.ToString(data.GetReflectedValue(excelColumn.PropertyName), CultureInfo.InvariantCulture);
                    var style          = excelColumn.Style;
                    var excelCellValue = excelColumn.ExcelCellValue;

                    if (excelColumn.DataFormatString.ToLower() == "custom" && customFormat != null)
                    {
                        value = customFormat(Type.GetType(excelColumn.PropertyType), value);
                    }
                    else
                    {
                        if (excelColumn.PropertyType == typeof(TimeSpan).FullName)
                        {
                            var tmv = (TimeSpan)data.GetReflectedValue(excelColumn.PropertyName);
                            value = string.Format("{0:00}:{1:00}:{2:00}", tmv.Hours, tmv.Minutes, tmv.Seconds);
                        }
                        else if (excelColumn.PropertyType == typeof(DateTime).FullName ||
                                 excelColumn.PropertyType == typeof(DateTime?).FullName)
                        {
                            try
                            {
                                var tmv = (DateTime)data.GetReflectedValue(excelColumn.PropertyName);
                                value = string.Format(excelColumn.DataFormatString, tmv);
                                //tmv.ToString(excelColumn.DataFormatString.Replace("{0:", "").Replace("}", ""));
                            }
                            catch (Exception)
                            {
                                value = string.Empty;
                            }
                            excelCellValue = CellValues.SharedString;
                        }
                    }

                    var address = excelColumn.ExcelCol + (excelColumn.ExcelRow + excelColumn.LastIndex);
                    _wbPart.UpdateValue(SheetName, address, value, style, excelCellValue, _sharedStrings);
                }
            }
            // Actualizo los condicionales
            foreach (var ex in _excelColumns.Where(e => !string.IsNullOrEmpty(e.ConditionalFormatStart)))
            {
                _wbPart.UpdateConditionalOver(SheetName, ex.ConditionalFormatStart, ex.ConditionalFormatStart + ":" + ex.ExcelCol + (ex.ExcelRow + ex.LastIndex));
            }

            stopwatch.Stop();
        }