private string GetCountExcel(string in_filepath) { string _return_str = ""; int count = 0; double _sum_price = 0.0; string strCon = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source =" + in_filepath + ";Extended Properties=Excel 8.0;"; //Response.Write(strCon); OleDbConnection myConn = new OleDbConnection(strCon); OleDbDataReader datareader; OleDbCommand myCmd; myConn.Open(); string aa = in_filepath; try { DataTable ExcelSheets = myConn.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" }); myCmd = new OleDbCommand("SELECT * FROM [" + ExcelSheets.Rows[0]["TABLE_NAME"].ToString() + "]"); //Response.Write(myCmd.CommandText); myCmd.Connection = myConn; datareader = myCmd.ExecuteReader(); while (datareader.Read()) { try { _sum_price += double.Parse(datareader[39].ToString().Trim().Replace(",", "").Replace("$", "")); } catch { _sum_price += 0; } count++; } Lab_TotalSalePrice.Text = "$" + _sum_price.ToString("###,##0.00"); _return_str = count.ToString("###,##0"); datareader.Close(); } catch (Exception ex) { throw ex; } finally { //Response.Write(_return_str); myConn.Close(); } return _return_str; }
利用GetOleDbSchemaTable 计算Excel中字段的值(asp.net c#)
最新推荐文章于 2021-05-15 16:22:31 发布