在Aspose ImportCustomObjects中分配自定义属性名称

我正在使用Aspose ImportCustomObjects方法将数据导出到Excel文件。 我有以下C#类: –

public class ChildAccountDetails { public string Name{ get; set; } public string Phone{get; set; } ...Other properties } 

我将isPropertyNameShown参数设置为true,因为我想要这些属性名称作为第一行导入,但同时我不希望Name被显示,而是我想Full Name作为标题,所以我将DisplayName属性添加到属性喜欢这个:-

 [DisplayName("First Name")] public string Name{ get; set; } 

但仍然是导入Name而不是First Name 。 我是否正确?

使用不同的属性不会更改Excel中的行标题。 您可以使用不同的方法。

  1. 将isPropertyNameShown设置为false
  2. 手动设置标题

我从http://www.aspose.com/docs/display/cellsnet/Importing+Data+to+Worksheets获取了原始代码,并针对您的scheme进行了更新。

 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); 

我作为开发者传播者与Aspose合作。