【问题标题】:Assign Custom property name in Aspose ImportCustomObjects在 Aspose ImportCustomObjects 中分配自定义属性名称
【发布时间】:2015-06-03 10:34:50
【问题描述】:

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

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

我将isPropertyNameShown 参数设置为true,因为我希望将这些属性名称作为第一行导入,但同时我不想显示Name,而是希望First Name 作为标题所以我在属性中添加了DisplayName 属性,如下所示:-

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

但它仍然导入Name 而不是First Name。我做得对吗?

【问题讨论】:

    标签: c# excel aspose


    【解决方案1】:

    使用不同的属性不会更改 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 合作,担任开发人员宣传员。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-18
      • 1970-01-01
      • 1970-01-01
      • 2016-07-26
      相关资源
      最近更新 更多