Quick overview: The main goal is to read in the data from a set date, from the row and get the ref number form the set date e.g. Start date.
For example if i just wanted the data from the date set to the 1st of last month and upwards.
I currently have to extract some data from the excel spreadsheet example below:
Start date Ref number
29/07/2015 2342326
01/07/2016 5697455
02/08/2016 3453787
02/08/2016 5345355
02/08/2015 8364456
03/08/2016 1479789
04/07/2015 9334578
Output using EPPlus:
29/07/2015
2342326
29/07/2016
5697455
02/08/2016
3453787
02/08/2016
5345355
02/08/2015
8364456
03/08/2016
1479789
04/07/2015
9334578
This part is fine, but when i try to strip the output via a date range i'm getting errors, e.g. using LINQ i get the following error output.
An unhandled exception of type 'System.InvalidCastException' occurred in System.Data.DataSetExtensions.dll
Additional information: Specified cast is not valid.
LINQ code:
var rowsOfInterest = tbl.AsEnumerable()
.Where(row => row.Field<DateTime>("Start date") >= new DateTime(2016, 7, 1))
.ToList();
I've also tried to modify the from date range using the datatable:
DataRow[] result = tbl.Select("'Start date' >= #1/7/2016#");
But get the following error:
An unhandled exception of type 'System.Data.EvaluateException' occurred in System.Data.dll
Additional information: Cannot perform '>=' operation on System.String and System.Double.
The last attempt was to try and see if i can strip the date from within the loop.
Code used:
DateTime dDate;
row[cell.Start.Column - 1] = cell.Text;
string dt = cell.Text.ToString();
if (DateTime.TryParse(dt, out dDate))
{
DateTime dts = Convert.ToDateTime(dt);
}
DateTime date1 = new DateTime(2016, 7, 1);
if (dDate >= date1)
{
Console.WriteLine(row[cell.Start.Column - 1] = cell.Text);
}
This sort of works but just lists the set dates and not there values, which is understandable, If i take this route how would i get the dates with there values?
Output:
29/07/2016
02/08/2016
02/08/2016
03/08/2016
The full code example used:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.OleDb;
using System.Text.RegularExpressions;
using Microsoft.Office.Interop.Excel;
using System.Data;
using System.IO;
namespace Number_Cleaner
{
public class NumbersReport
{
//ToDo: Look in to fixing the code so it filters the date correctly with the right output data.
public System.Data.DataTable GetDataTableFromExcel(string path, bool hasHeader = true)
{
using (var pck = new OfficeOpenXml.ExcelPackage())
{
using (var stream = File.OpenRead(path))
{
pck.Load(stream);
}
var ws = pck.Workbook.Worksheets.First();
System.Data.DataTable tbl = new System.Data.DataTable();
foreach (var firstRowCell in ws.Cells[1, 1, 1, ws.Dimension.End.Column])
{
tbl.Columns.Add(hasHeader ? firstRowCell.Text : string.Format("Column {0}", firstRowCell.Start.Column));
}
var startRow = hasHeader ? 2 : 1;
for (int rowNum = startRow; rowNum <= ws.Dimension.End.Row; rowNum++)
{
var wsRow = ws.Cells[rowNum, 1, rowNum, ws.Dimension.End.Column];
DataRow row = tbl.Rows.Add();
foreach (var cell in wsRow)
{
DateTime dDate;
row[cell.Start.Column - 1] = cell.Text;
string dt = cell.Text.ToString();
//Console.WriteLine(dt);
if (DateTime.TryParse(dt, out dDate))
{
DateTime dts = Convert.ToDateTime(dt);
}
DateTime date1 = new DateTime(2016, 7, 1);
if (dDate >= date1)
{
Console.WriteLine(row[cell.Start.Column - 1] = cell.Text);
}
//Console.WriteLine(row[cell.Start.Column - 1] = cell.Text);
}
}
//var rowsOfInterest = tbl.AsEnumerable()
// .Where(row => row.Field<DateTime>("Start date") >= new DateTime(2016, 7, 1))
//.ToList();
//Console.WriteLine(tbl);
//DataRow[] result = tbl.Select("'Start date' >= #1/7/2016#");
return tbl;
}
}
Modified from: How to match date to row then get the final column value using EPPlus?