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