Data Management (ImportSettings/Schema)

StatusIDPriorityType
Closed37312MajorQuestion
ProfileReply
gabossoloClient

Hi -- Could please help me the how to create an import Schema for ExcelDataSource?

Replies

UserDescriptionPosted On
MariyaVoytovichAgent

Hello gabossolo!

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.

Regards,
Mariya
On behalf of Xafari Client Services Team

× This ticket is closed.

Write US