I have defined [Display(Name = "From Date")]
for columns in a Model and I am using it from a View with the help of tag helpers like asp-for
and @Html.DisplayNameFor()
,
But I am exporting data to excel sheet, and that code is in a controller. Now to set the column header in excel I want to take column names from [Display(Name = "From Date")]
instead of hard coding it,
How can I use it?
To make my point more clear, my existing code is -> row.CreateCell(1).SetCellValue("PCard Dt");
Now, I want to replace the hard coded string "PCard Dt" with [Display(Name = "From Date")]
I tried various answers on google, but its not serving my purpose
CodePudding user response:
The key point mentioned in the comment is you need System.Reflection to extract the DisplayAttribute
.
Implement an extension method to extract the value from DisplayAttribute
.
public static class ReflectionExtensions
{
public static string ToName(this PropertyInfo propertyInfo)
{
try
{
object[] attributes = propertyInfo.GetCustomAttributes(typeof(DisplayAttribute), false);
if (attributes != null && attributes.Any())
return ((DisplayAttribute)attributes[0]).Name;
return propertyInfo.Name;
}
catch
{
return propertyInfo.Name;
}
}
}
I wrote a similar implementation in GitHub.
With the below way to iterate all properties in class and get the value of DisplayAttribute
.
Caller
PropertyInfo[] props = typeof(Model).GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.GetProperty);
foreach (PropertyInfo prop in props)
{
string displayName = prop.ToName();
}
Another approach to work with System.Linq.Expression.
public static class ReflectionExtensions
{
public static string ToNameByExpression<T, P>(Expression<Func<T, P>> propertyExpression) where T : new ()
{
MemberExpression expression = propertyExpression.Body as MemberExpression;
if (expression == null)
return null;
return (expression.Member as PropertyInfo)
.ToName();
}
}
Provide the expression to specify the property that you want.
Caller
string displayName = ReflectionExtensions.ToNameByExpression((Model m) => m.FromDate);
CodePudding user response:
You get display name attribute by this way:
public class TestModel
{
[DisplayName("From Date")]
public string FromDate { get; set; }
}
//generic method to get name
private string GetDisplayName<T>(string propertyName)
{
MemberInfo property = typeof(T).GetProperty(propertyName);
var attribute = property.GetCustomAttributes(typeof(DisplayNameAttribute), true)
.Cast<DisplayNameAttribute>().FirstOrDefault();
return attribute?.DisplayName ?? propertyName;
}
//get display name
var displayName = GetDisplayName<TestModel>(nameof(TestModel.FromDate));
CodePudding user response:
To use the value of the Display attribute in a controller action when exporting data to an Excel sheet, you can use reflection to get the value of the attribute. Here is an example of how you could do this:
public ActionResult ExportToExcel(int id)
{
// Get the data to be exported
var data = GetDataById(id);
// Create a new Excel package
var package = new ExcelPackage();
var worksheet = package.Workbook.Worksheets.Add("Sheet1");
// Get the properties of the data object
var properties = typeof(DataType).GetProperties(BindingFlags.Instance |
BindingFlags.Public |
BindingFlags.GetProperty);
// Set the column headers using the Display attribute
for (int i = 0; i < properties.Length; i )
{
var displayAttr = properties[i].GetCustomAttribute<DisplayAttribute>();
worksheet.Cells[1, i 1].Value = displayAttr != null ?
displayAttr.Name : properties[i].Name;
}
// Add the data to the worksheet
for (int i = 0; i < data.Count; i )
{
for (int j = 0; j < properties.Length; j )
{
worksheet.Cells[i 2, j 1].Value =
properties[j].GetValue(data[i]);
}
}
// Save the Excel package and send it to the client
var content = package.GetAsByteArray();
return File(content, "application/vnd.openxmlformats-
officedocument.spreadsheetml.sheet", "ExportedData.xlsx");
}
In this example, the ExportToExcel action gets the data to be exported and creates a new Excel package. It then gets the properties of the data object using reflection and sets the column headers using the Display attribute, if it exists. Finally, it adds the data to the worksheet and sends the Excel package to the client.
Note that you will need to import the System.ComponentModel.DataAnnotations namespace in order to use the DisplayAttribute class.