日期:2014-05-17 浏览次数:20904 次
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + FileName + ";Extended Properties='Excel 8.0;HDR=False;IMEX=1'"; OleDbConnection OleConn = new OleDbConnection(strConn); OleConn.Open(); String sql = "SELECT * FROM [Sheet1$]";//可是更改Sheet名称,比如sheet2,等等 OleDbDataAdapter OleDaExcel = new OleDbDataAdapter(sql, OleConn); DataSet OleDsExcle = new DataSet(); OleDaExcel.Fill(OleDsExcle, "Sheet1"); OleConn.Close(); int rowCount = OleDsExcle.Tables["Sheet1"].Rows.Count; int columnCount = OleDsExcle.Tables["Sheet1"].Columns.Count; myListView.BeginUpdate(); myListView.Items.Clear(); string[] lvitem = new string[columnCount-1]; for (int i = 0; i < rowCount; i++) { for (int j = 0; j < columnCount-1; j++) { lvitem[j] =OleDsExcle.Tables["Sheet1"].Rows[i][j+1].ToString(); } ListViewItem lvi = new ListViewItem(lvitem); myListView.Items.Add(lvi); } myListView.EndUpdate();