We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

ImportCustomObjects only recognises Properties- not fields

Hi,

I have just finished investigating why my collections have not been importing into my Aspose workbooks correctly. The issue seems to be that the ImportCustomObjects method only recognises properties with the { get; set; } on them, and not fields. Eg if you paste the code below into a console app and run it, the output file only includes ‘age’ but not name or salary.

Any thoughts on how to resolve the issue?

Many thanks!


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Aspose.Cells;
using System.Linq;
using FileHelpers;

namespace Sandbox2
{
class Program
{
static void Main(string[] args)
{


//Instantiate a new Workbook
Workbook book = new Workbook();

//Clear all the worksheets
book.Worksheets.Clear();

List p = new List();
p.Add(new person { name = “bob”, age = 10, salary = 500.5M });
p.Add(new person { name = “joe”, age = 102, salary = 612.5M });
p.Add(new person { name = “harry”, age = 12, salary = null });

Worksheet sheet2 = book.Worksheets.Add(“TestWithClass”);

sheet2.Cells.ImportCustomObjects((System.Collections.ICollection)p, null, true, 0, 0, p.Count(), true, “dd/mm/yyyy”, false);
sheet2.AutoFitColumns();
sheet2 = null;

//Save the Excel file
book.Save(@“C:/tesssst2.xls”);///SaveFormat.Excel97To2003);

}

}

public sealed class person
{
public string name;
public int age { get; set; }
public decimal? salary;
}

}

Hi,


Well, the Cells.ImportCustomObjects() method only works on properties and not fields. I am afraid, you should change / have to change the fields to properties for your requirements.

Thank you.