The code below shows an example of importing data from Excel.
For the example, the Shipper object was used from the demo Northwind.
public void ImportShippersFromExcel()
{
// Arrange
var shipperType = typeof(Shipper);
ITypeInfo shipperTypeInfo = XafTypesInfo.CastTypeToTypeInfo(shipperType);
ISettings settings = new ImportSettings();
ISchema schema = new Schema();
const string tableName = "Shippers";
ITypeMap shipperTypeMap = new TypeMap(shipperTypeInfo.FullName, tableName);
shipperTypeMap.KeyMemberName = "Oid";
shipperTypeMap.KeyMemberType = shipperTypeInfo.FindMember("Oid").MemberType.FullName;
shipperTypeMap.KeyColumnName = "ShipperID";
shipperTypeMap.KeyColumnType = "System.Int32";
shipperTypeMap.MemberMapping.Add(new MemberMap(shipperTypeInfo.FindMember("Name").Name, "CompanyName"));
shipperTypeMap.MemberMapping.Add(new MemberMap(shipperTypeInfo.FindMember("Phone").Name, "Phone"));
schema.TypeMapping.Add(shipperTypeMap);
settings.Schema = schema;
const string sourcePath = "Northwind.xls";
IDataSource dataSource = new ExcelDataSource(sourcePath);
dataSource.Schema = settings.Schema;
IOperation operation = new ImportOperation(Application);
operation.DataSource = dataSource;
operation.Settings = settings;
// Act
operation.Execute();
}
Feel free to contact us if you need further assistance or have additional questions.