Esempio n. 1
0
        private void GenerateExcelBody(ExcelWorksheet excelWorksheet, ExcelSheetAttribute excelSheetAttribute, Dictionary <PropertyInfo, ExcelColumnAttribute> propertyDictionary)
        {
            TModel[]       modelArray        = Model.ToArray();
            PropertyInfo[] propertyInfoArray = propertyDictionary.Select(pd => pd.Key).ToArray();

            for (int row = 2; row <= modelArray.Length + 1; row++)
            {
                TModel currentModel = modelArray[row - 2];
                for (int col = 1; col <= propertyInfoArray.Length; col++)
                {
                    PropertyInfo         currentProperyInfo   = propertyInfoArray[col - 1];
                    ExcelColumnAttribute excelColumnAttribute = propertyDictionary[currentProperyInfo];
                    ExcelRange           currentCell          = excelWorksheet.Cells[row, col];
                    var displayFormatAttribute = currentProperyInfo.GetCustomAttribute <DisplayFormatAttribute>();
                    if (displayFormatAttribute != null && typeof(IFormattable).IsAssignableFrom(currentProperyInfo.PropertyType))
                    {
                        currentCell.Value = ((IFormattable)currentProperyInfo.GetValue(currentModel)).ToString(displayFormatAttribute.DataFormatString, CultureInfo.InvariantCulture);
                    }
                    else
                    {
                        currentCell.Value = currentProperyInfo.GetValue(currentModel);
                    }
                }

                excelWorksheet.Row(row).Height = excelSheetAttribute.RowHeight;
            }
        }
Esempio n. 2
0
        private void GenerateExcelHeader(ExcelWorksheet excelWorksheet, ExcelSheetAttribute excelSheetAttribute, Dictionary <PropertyInfo, ExcelColumnAttribute> propertyDictionary)
        {
            PropertyInfo[] propertyInfoArray = propertyDictionary.Select(pd => pd.Key).ToArray();

            for (int col = 1; col <= propertyInfoArray.Length; col++)
            {
                PropertyInfo currentProperyInfo = propertyInfoArray[col - 1];

                DisplayAttribute displayAttribute = currentProperyInfo.GetCustomAttribute <DisplayAttribute>();

                DisplayNameAttribute displayNameAttribute = currentProperyInfo.GetCustomAttribute <DisplayNameAttribute>();

                string displayName = displayAttribute?.GetName() ?? displayNameAttribute?.DisplayName ?? currentProperyInfo.Name;

                ExcelRange currentCell = excelWorksheet.Cells[1, col];
                currentCell.Value = displayName;
            }

            excelWorksheet.Row(1).Height = excelSheetAttribute.RowHeight;
        }
Esempio n. 3
0
        private void FormatExcelSheet(ExcelWorksheet excelWorksheet, ExcelSheetAttribute excelSheetAttribute, Dictionary <PropertyInfo, ExcelColumnAttribute> propertyDictionary)
        {
            PropertyInfo[] propertyInfoArray = propertyDictionary.Select(pd => pd.Key).ToArray();

            excelWorksheet.Cells.AutoFitColumns();

            for (int col = 1; col <= propertyInfoArray.Length; col++)
            {
                PropertyInfo         currentProperyInfo   = propertyInfoArray[col - 1];
                ExcelColumnAttribute excelColumnAttribute = propertyDictionary[currentProperyInfo];
                ExcelColumn          currentExcelColumn   = excelWorksheet.Column(col);
                currentExcelColumn.Style.Numberformat.Format = excelColumnAttribute.Format;
                currentExcelColumn.Style.QuotePrefix         = excelColumnAttribute.Quote;
                currentExcelColumn.Style.VerticalAlignment   = ExcelVerticalAlignment.Center;
                currentExcelColumn.Style.HorizontalAlignment = excelColumnAttribute.IsCenter ? ExcelHorizontalAlignment.Center : ExcelHorizontalAlignment.General;
                currentExcelColumn.Width += 3;
            }

            excelWorksheet.Row(1).Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
            excelWorksheet.Row(1).Style.Font.Bold           = true;
        }