我很难将Excel导入我的ListView
。我如何像这样在列表视图中导入与其名称匹配的Excel数据?
这是我的列表视图数据,我想在excel中导入该列总数,使其与Name
列中的名称相匹配,如下所示:
这是我的代码。我应该使用什么查询来匹配列Name
中的所有数据名称?
ListViewItem lvis = new ListViewItem();
string connStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + txtFileName.Text + ";Extended Properties=Excel 12.0;";
DataTable table = new DataTable();
string excelName = "Sheet1";
string strConnection = string.Format(connStr);
OleDbConnection conn = new OleDbConnection(strConnection);
conn.Open();
OleDbDataAdapter oada = new OleDbDataAdapter("select * from [" + excelName + "$] Where Name = '" + Name + "';", strConnection);
table.TableName = "TableInfo";
oada.Fill(table);
conn.Close();
// Clear the ListView control
listView1.Items.Clear();
// Display items in the ListView control
for (int i = 0; i < table.Rows.Count; i++)
{
DataRow drow = table.Rows[i];
// Only row that have not been deleted
if (drow.RowState != DataRowState.Deleted)
{
// Define the list items
ListViewItem lvi = new ListViewItem(drow["Id"].ToString());
//lvi.SubItems.Add(drow["Name"].ToString());
//lvi.SubItems.Add(drow["Details"].ToString());
//lvi.SubItems.Add(drow["Detail2"].ToString());
lvi.SubItems.Add(drow["Total"].ToString());
// Add the list items to the ListView
listView1.Items.Add(lvi);
}
}
答案 0 :(得分:0)
您可以为此使用linq
List<object> listResult = from p in DataTable1.AsEnumerable()
join pm in DataTable2.AsEnumerable()on p.Name equals pm.Name
select new { Id= p, Total = pm };