到目前为止,我可以从电子表格中轻松获取数据,只需获取参考编号行,但在获取正确的数据之前,我目前还不知道如何将行与数据部分匹配。
我目前必须从以下excel电子表格示例中提取一些数据:
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
主要问题是是否可以从设定的日期,行中读取数据,并从设定日期获取参考号码,例如开始日期。
例如,如果我只想要将日期设置为上个月1日及以上的数据。
如何最好地实施。
使用基本OleDb获取列的当前代码示例:
using System;
using System.Data.OleDb;
using System.Text.RegularExpressions;
namespace Number_Cleaner
{
public class NumberCleanerReport
{
public void runExcel_Report()
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("[*][START OF: NumberExt.xls, Number Extraction]");
Console.ResetColor();
string con =
@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=NumberExt.xls;" +
@"Extended Properties='Excel 8.0;HDR=Yes;'";
string connectionString = ExcelWriter.GetConnectionString();
using (OleDbConnection conn = new OleDbConnection(connectionString))
{
conn.Open();
OleDbCommand cmd = new OleDbCommand();
cmd.Connection = conn;
using (OleDbConnection connection = new OleDbConnection(con))
{
connection.Open();
OleDbCommand command = new OleDbCommand("select * from [Sheet1$]", connection);
System.IO.StreamWriter files = new System.IO.StreamWriter(Controller.fpath + "NumberExtOutput.txt");
using (OleDbDataReader dr = command.ExecuteReader())
{
while (dr.Read())
{
var row1Col0 = dr[0];
string ExcelData = row1Col0.ToString();
string subStr = "null";
try
{
subStr = ExcelData.Substring(0, 6);
}
catch
{
//Console.WriteLine("Found Nulls.");
}
if (subStr == "00")
{
string result = Regex.Replace(ExcelData, "^00", "0");
Console.WriteLine(result);
files.WriteLine(result);
cmd.CommandText = "INSERT INTO [table1]('MainNmbers') VALUES(" + result + ");";
cmd.ExecuteNonQuery();
}
}
files.Close();
conn.Close();
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("[*][END OF: NumberExt.xls, RefNumber Extraction]");
Console.ResetColor();
}
}
}
}
}
}
答案 0 :(得分:1)
您可以将其加载到DataTable
,然后使用Linq-To-DataTable
对其进行过滤。 Here's a method以字符串形式读取所有内容。您可以对其进行修改,以便将第一列解析为DateTime
,将第二列解析为int
或DateTime.Parse
和DateTime.ParseExact
:
int.Parse
在表格中输入正确的类型后,查询很简单:
public static 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();
DataTable tbl = new 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)
{
row[cell.Start.Column - 1] = cell.Text;
}
}
return tbl;
}
}
如果您需要var rowsOfInterest = table.AsEnumerable()
.Where(row => row.Field<DateTime>("Start date") >= new DateTime(2016, 7, 1))
.ToList();
:
DataTable