百科知识

C#中怎么获取Excel数据的行数?求详细代码

2013-11-12 16:40:26雷***
C#中怎么获取Excel数据的行数?求详细代码。。。C#中怎么获取Excel数据的行数?求详细代码。。。: DataTable ExcelTable; DataSet ds = new?

最佳回答

  •    DataTable ExcelTable; DataSet ds = new DataSet(); //Excel的连接 OleDbConnection objConn = new OleDbConnection("Provider=Microsoft。
      Jet。OLEDB。4。0;Data Source=EXCEL文件路径;" + "Extended Properties=Excel 8。0;"); objConn。Open(); DataTable schemaTable = objConn。
      GetOleDbSchemaTable(System。Data。OleDb。OleDbSchemaGuid。Tables, null); string tableName = schemaTable。Rows[0][2]。ToString()。
      Trim();//获取 Excel 的表名,默认值是sheet1 string strSql = "select * from [" + tableName + "]"; OleDbCommand objCmd = new OleDbCommand(strSql, objConn); OleDbDataAdapter myData = new OleDbDataAdapter(strSql, objConn); myData。
      Fill(ds, tableName);//填充数据 dgBom。DataSource =ds; dgBom。DataBind(); objConn。Close(); ExcelTable = ds。
      Tables[tableName]; int iColums = ExcelTable。Columns。Count;//列数 int iRows = ExcelTable。Rows。Count;//行数 。
      
    2013-11-12 19:51:28
  • 很赞哦! (111)