【发布时间】:2019-11-14 08:07:24
【问题描述】:
我必须在 C# .NET 中编写一个程序,在 datagridview 中加载并显示 Excel 文件中的数据,然后我想使用 date to date(filter) 根据选择的日期显示 Excel 文件数据。 我的 Excel 中有 7 列和 1800 行,我有 DateTimePicker1 和 DateTimePicker2 来选择 from_to(date) 和按钮来显示过滤器(date to date)。 我编写了一个程序,可以在 datagridview 中下载并显示 excel 文件,但我无法显示过滤日期。 您能帮帮我吗,我如何在 C# 中对我的 excel 文件数据进行过滤。
在这里我需要帮助,请问如何显示 Date to Date excel 数据,我尝试使用此代码,但它不起作用,
//======================Filter button ========================
private void Button3_Click(object sender, EventArgs e)
{
try
{
DateTime startSchedule = startDate.Value.Date;
DateTime endSchedule = endDate.Value.Date;
string constr = string.Format("Provider = Microsoft.ACE.OLEDB.12.0; Data Source =" + tb_path.Text + ";Extended Properties = \"Excel 12.0; HDR=Yes;\"; ");
OleDbConnection con = new OleDbConnection(constr);
OleDbCommand command = new OleDbCommand("Select * From[" + drop_down_sheet.SelectedValue + "]", con);
OleDbDataAdapter sda = new OleDbDataAdapter(command);
DataTable dt = new DataTable();
sda.Fill(dt);
/* foreach (DataRow row in dt.Rows)
{
dataGridView1.DataSource = dt;
dt.DefaultView.RowFilter = string.Format(tb_path.Text); //?.....
}*/
string filter = "Date > '" + startDate.Value.ToString("yyyy-MM-dd") + "' AND Date < '" + endDate.Value.ToString("yyyy-MM-dd") + "'";
DataRow[] filteredRows = dt.Select(filter);
dataGridView1.DataSource = filteredRows.CopyToDataTable();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message,
"Important Note",
MessageBoxButtons.OK,
MessageBoxIcon.Error,
MessageBoxDefaultButton.Button1);
}
}
【问题讨论】: