public void ToDataTableShouldSetNamespace() { using (var package = new ExcelPackage()) { var sheet = package.Workbook.Worksheets.Add("test"); sheet.Cells["A1"].Value = "Id"; sheet.Cells["B1"].Value = "Name"; sheet.Cells["A2"].Value = 1; sheet.Cells["B2"].Value = "John Doe"; var options = ToDataTableOptions.Create(o => { o.DataTableNamespace = "ns1"; }); var dt = sheet.Cells["A1:B2"].ToDataTable(options); Assert.AreEqual("ns1", dt.Namespace); } }
public void ToDataTableShouldHandleDateTimeWithMapping() { using (var package = new ExcelPackage()) { var date = DateTime.UtcNow; var sheet = package.Workbook.Worksheets.Add("test"); sheet.Cells["A1"].Value = "Id"; sheet.Cells["B1"].Value = "Date"; sheet.Cells["A2"].Value = 1; sheet.Cells["B2"].Value = date; var options = ToDataTableOptions.Create(o => { o.Mappings.Add(1, "MyDate", typeof(DateTime)); }); var dt = sheet.Cells["A1:B2"].ToDataTable(options); Assert.AreEqual(1, dt.Rows.Count); Assert.AreEqual(1, dt.Rows[0]["Id"]); Assert.AreEqual(date.ToOADate(), ((DateTime)dt.Rows[0]["MyDate"]).ToOADate()); } }
public void ToDataTableShouldHandleTransform() { using (var package = new ExcelPackage()) { var date = DateTime.UtcNow; var sheet = package.Workbook.Worksheets.Add("test"); sheet.Cells["A1"].Value = "Id"; sheet.Cells["B1"].Value = "Date"; sheet.Cells["A2"].Value = 1; sheet.Cells["B2"].Value = date; var options = ToDataTableOptions.Create(o => { o.Mappings.Add(0, "Id", typeof(string), true, c => "Id: " + c.ToString()); }); var dt = sheet.Cells["A1:B2"].ToDataTable(options); Assert.AreEqual(1, dt.Rows.Count); Assert.AreEqual("Id: 1", dt.Rows[0]["Id"]); Assert.AreEqual(date, dt.Rows[0]["Date"]); } }