Ich versuche also, eine Excel-Datei zu lesen und die Daten in Klasseneigenschaften einzufügen. Ich denke, es gibt ein Problem, wenn ich die Klasse instanitae, aber ich mache es, bevor ich analysieren und die Werte der Klasse hinzufügen.Datenbindung Nullwerte zurückgeben
Sobald das Parsing abgeschlossen ist - ich rufe die gebundene Klasse, aber es gibt null zurück.
The Calls
TestData td = new TestData();
XLReader.GetClassFromExcel<TestData>(1,1,1);
var ddddd = td.Title; //this is null.
Die Klasse
public class TestData
{
public string Title { get; set; }
public string Site { get; set; }
public string StartDate { get; set; }
public string EndDate { get; set; }
public string Email { get; set; }
public string OrderRef { get; set; }
public string ReqBy { get; set; }
public string ApprovedBy { get; set; }
}
Die Parsing des Excel
public static class XLReader
{
/// <summary>
/// The deployment files folder name
/// </summary>
private static readonly string deploymentFilesFolderName =
Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
public static List<T> GetClassFromExcel<T>(int fromRow, int fromColumn, int toColumn = 0)
{
var path = Path.Combine(deploymentFilesFolderName, @"Tools\TestData.xlsx");
List<T> retList = new List<T>();
using (var pck = new ExcelPackage())
{
using (var stream = File.OpenRead(path))
{
pck.Load(stream);
}
//Retrieve first Worksheet
var ws = pck.Workbook.Worksheets.First();
//If the to column is empty or 0, then make the tocolumn to the count of the properties
//Of the class object inserted
toColumn = toColumn == 0 ? typeof(T).GetProperties().Count() : toColumn;
//Read the first Row for the column names and place into a list so that
//it can be used as reference to properties
List<string> columnNames = new List<string>();
// wsRow = ws.Row(0);
foreach (var cell in ws.Cells[1, 1, 1, ws.Cells.Count()])
{
columnNames.Add(cell.Value.ToString());
}
//Loop through the rows of the excel sheet
for (var rowNum = fromRow; rowNum <= ws.Dimension.End.Row; rowNum++)
{
//create a instance of T
T objT = Activator.CreateInstance<T>();
//Retrieve the type of T
Type myType = typeof(T);
//Get all the properties associated with T
PropertyInfo[] myProp = myType.GetProperties();
var wsRow = ws.Cells[rowNum, fromColumn, rowNum, ws.Cells.Count()];
foreach (var propertyInfo in myProp)
{
if (columnNames.Contains(propertyInfo.Name))
{
int position = columnNames.IndexOf(propertyInfo.Name);
//To prevent an exception cast the value to the type of the property.
var blah = Convert.ChangeType(wsRow[rowNum, position + 1].Value, propertyInfo.PropertyType);
propertyInfo.SetValue(objT,blah);
}
}
retList.Add(objT);
}
}
return retList;
}
}
'var td = XLReader.GetClassFromExcel (1,1,1) [0];' –
Dank @KooKiz, das funktioniert. Der nächste Schritt für mich ist, wie bekomme ich die Daten in der Klasse, also wann immer ich TestData() aufrufen. Site (in jeder anderen Klasse) wird es die Daten zurückgeben? –
Würden Sie nicht nur 'td.Site' machen, um an die Daten zu kommen? – Ernie