部分代码展示:
public DataTable GetData(string Path) //写一个方法 { string connStr = "Provider=Microsoft.ACE.OLEDB.12.0;Extended Properties=Excel 12.0;Data Source=" + Path; System.Data.OleDb.OleDbConnection oledbconn = new System.Data.OleDb.OleDbConnection(connStr); oledbconn.Open(); System.Data.DataTable _table = oledbconn.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" }); string strTableName = string.Empty; if (_table.Rows.Count > 0) { strTableName = _table.Rows[0]["TABLE_NAME"].ToString().Trim(); string sql = string.Format("SELECT * FROM [{0}]", strTableName); _table = new System.Data.DataTable(); System.Data.OleDb.OleDbDataAdapter da = new System.Data.OleDb.OleDbDataAdapter(sql, oledbconn); da.Fill(_table); } oledbconn.Close(); //listview的Header需要对应Excel中的Header return _table; } int j = 0; public DataTable GetTxt(string pths) { StreamReader sr = new StreamReader(pths, Encoding.GetEncoding("GB2312")); string txt = sr.ReadToEnd().Replace("\r\n", "-"); string[] nodes = txt.Split('-'); foreach (string node in nodes) { string node1 = nodes[0]; string[] strs = node.Split(','); string[] strs1 = node1.Split(','); for (int i = 0; i < strs.Length; i++) { DataRow dr = dt.NewRow(); for (int j = 0; j < strs1.Length; j++) { if(strs1[0]==strs[0]) { dt.Columns.Add(new DataColumn(strs1[j])); } dr[strs1[j]] = strs[i]; i++; } dt.Rows.Add(dr); } } return dt; }