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

Assign Custom property name in Aspose ImportCustomObjects

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

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

我将 isPropertyNameShown 参数设置为 true,因为我希望将这些 属性 名称导入为第一行,但同时我不希望 Name被显示而不是我想要 First Name 作为 header 所以我将 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 中获取了原始代码并针对您的场景进行了更新。

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 工作,担任开发人员布道师。