110,534
社区成员
发帖
与我相关
我的任务
分享
/// <summary>
/// 将Excel转换位dataTable
/// </summary>
/// <param name="strExcelFileName">Excel的路径</param>
/// <param name="strSheetName"></param>
/// <returns></returns>
public static System.Data.DataTable ExcelToDataTable(string strExcelFileName, string strSheetName)
{
//源的定义
string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=" + strExcelFileName + ";" + "Extended Properties='Excel 12.0;HDR=Yes;IMEX=1;'";
//Sql语句
//string strExcel = string.Format("select * from [{0}$]", strSheetName); 这是一种方法
string strExcel = "select * from [sheet1$]";
//定义存放的数据表
DataSet ds = new DataSet();
//连接数据源
OleDbConnection conn = new OleDbConnection(strConn);
conn.Open();
//适配到数据源
OleDbDataAdapter adapter = new OleDbDataAdapter(strExcel, strConn);
adapter.Fill(ds, strSheetName);
conn.Close();
return ds.Tables[strSheetName];
}
这个直接使用