如何从Excel中读取数据
怎么从Excel中读取数据
怎么从Excel表中从第三行开始,读取第二列的数据到没有数据为止,然后从中取出最大和最小值
------解决方案--------------------
C# 有一套单独的访问EXCEL数据的方法,类似ADO.NET 的。你可以根据需要设定where条件
------解决方案--------------------
怎么从Excel表中从第三行开始,读取第二列的数据到没有数据为止,然后从中取出最大和最小值
------解决方案--------------------
C# 有一套单独的访问EXCEL数据的方法,类似ADO.NET 的。你可以根据需要设定where条件
/// <summary>
/// Excel数据连接字符串
/// </summary>
private const string EXCELConnString = "Provider=Microsoft.ACE.OLEDB.12.0;Extended Properties='Excel 12.0;HDR=YES;IMEX=1';Data Source=";
/// <summary>
/// 读取Excel到DataTable
/// </summary>
/// <param name="Path"></param>
public static System.Data.DataTable ReadExcelToDataTable(string Path)
{
DataSet ds = new DataSet();
string queryString = "SELECT * FROM [Sheet1$] where 学号 <> ''";
string path = EXCELConnString + Path;
using (OleDbConnection connection = new OleDbConnection(path))
{
try
{
OleDbCommand oleCommand = new OleDbCommand(queryString, connection);
if (connection.State == ConnectionState.Closed)
{
connection.Open();
}
OleDbDataAdapter oleAdapter = new OleDbDataAdapter(oleCommand);
oleAdapter.Fill(ds, "[Sheet1$]");
}
catch (System.Exception)
{
return null;
}
}
// 数据绑定
return ds.Tables[0];
}
------解决方案--------------------