【发布时间】:2018-09-01 19:54:24
【问题描述】:
我正在使用listView 导入excel 文件,因为我的所有表格都是listView。
如何将excel文件的选定列和行导入listView?因为它只有在我在第一行或“A1”中创建一列时才有效,如果可能的话,我可以使用与他们的名字或 ID 匹配的 where 查询吗?谢谢你帮我一把!
这是我的示例 Excel 文件,它将导入我的 listView。
我的代码
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 + "$]", 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["1ST"].ToString());
// Add the list items to the ListView
listView1.Items.Add(lvi);
}
}
【问题讨论】: