I am using Aspose ImportCustomObjects method to export data to an excel file. I have following C# class:-
public class ChildAccountDetails
{
public string Name{ get; set; }
public string Phone{get; set; }
...Other properties
}
I am setting the isPropertyNameShown
parameter to true because I want these property names to be imported as first row, but at the same time I don't want Name
to be displayed instead I want First Name
as header so I added the DisplayName
attribute to property like this:-
[DisplayName("First Name")]
public string Name{ get; set; }
But still it is importing Name
instead of First Name
. Am I doing it correct?
Using a different attribute will not change the row title in Excel. You can use a different approach.
I took the original code from http://www.aspose.com/docs/display/cellsnet/Importing+Data+to+Worksheets and updated for your scenario.
String dst = dataDir + @"ImportedCustomObjects.xlsx";
// Instantiate a new Workbook
Workbook book = new Workbook();
// Clear all the worksheets
book.Worksheets.Clear();
// Add a new Sheet "Data";
Worksheet sheet = book.Worksheets.Add("Data");
// Define List of custom objects
List<ChildAccountDetails> list = new List<ChildAccountDetails>();
// Add data to the list of objects
list.Add(new ChildAccountDetails() { Name = "Saqib", Phone = "123-123-1234" });
list.Add(new ChildAccountDetails() { Name = "John", Phone = "111-000-1234" });
// Manually add the row titles
sheet.Cells["A1"].PutValue("First Name");
sheet.Cells["B1"].PutValue("Phone Number");
// We pick a few columns not all to import to the worksheet
sheet.Cells.ImportCustomObjects((System.Collections.ICollection)list,
new string[] { "Name", "Phone" }, // Field name must match the property name in class
false, // Don't show the field names
1, // Start at second row
0,
list.Count,
true,
"dd/mm/yyyy",
false);
// Save
book.Worksheets[0].AutoFitColumns();
book.Save(dst);
I work with Aspose as a Developer Evangelist.