Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在Aspose ImportCustomObjects中指定自定义属性名称_C#_Excel_Aspose - Fatal编程技术网

C# 在Aspose ImportCustomObjects中指定自定义属性名称

C# 在Aspose ImportCustomObjects中指定自定义属性名称,c#,excel,aspose,C#,Excel,Aspose,我正在使用Aspose方法将数据导出到excel文件。我有以下C类课程:- 我将isPropertyNameShowed参数设置为true,因为我希望将这些属性名称作为第一行导入,但同时我不希望显示Name,而是希望first Name作为标题,因此我将DisplayName属性添加到属性中,如下所示:- [DisplayName("First Name")] public string Name{ get; set; } 但它仍然在导入名称,而不是名字。是否正确?使用其他属性不会更改Exce

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

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

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

但它仍然在导入
名称
,而不是
名字
。是否正确?

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

  • 将isPropertyNameShowed设置为false
  • 手动设置标题
  • 我从中获取了原始代码,并针对您的场景进行了更新

    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);
    
    String dst=dataDir+@“ImportedCustomObjects.xlsx”;
    //实例化新工作簿
    工作簿=新工作簿();
    //清除所有工作表
    book.Worksheets.Clear();
    //添加新的工作表“数据”;
    工作表=book.Worksheets.Add(“数据”);
    //定义自定义对象的列表
    列表=新列表();
    //将数据添加到对象列表中
    添加(新的ChildAccountDetails(){Name=“Saqib”,Phone=“123-123-1234”});
    添加(新的ChildAccountDetails(){Name=“John”,Phone=“111-000-1234”});
    //手动添加行标题
    表.单元格[“A1”].PutValue(“名字”);
    表.单元格[“B1”].PutValue(“电话号码”);
    //我们选择了一些列,并不是全部导入到工作表中
    sheet.Cells.ImportCustomObjects((System.Collections.ICollection)列表,
    新字符串[]{“Name”,“Phone”},//字段名必须与类中的属性名匹配
    false,//不显示字段名
    1,//从第二行开始
    0,
    列表,计数,
    是的,
    “dd/mm/yyyy”,
    假);
    //拯救
    book.Worksheets[0]。AutoFitColumns();
    预订保存(dst);
    
    我与Aspose一起工作,担任开发人员宣传员

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