标签:
参考:C#通过OLEDB读写Excel2013显示到datagrid控件,修改数据集并更新excel2013
目录:
2. 代码实现
string strCon = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + @"d:/成绩表2013.xlsx" + ";Extended Properties=‘Excel 12.0;HDR=YES;IMEX=0‘";
OleDbConnection myConn = new OleDbConnection(strCon); string strCom = "SELECT * FROM [Sheet1$]"; myConn.Open(); OleDbDataAdapter myDataAdapter = new OleDbDataAdapter(strCom, myConn); DataSet myDataSet = new DataSet(); myDataAdapter.Fill(myDataSet, "[Sheet1$]"); myConn.Close();
代码实现:(默认将第一行设置为标题,表格内容从第二行开始计算)
//扩展名是 xls 会出错,xlsx 就不会出错 string str_filename = @"D:\01-Working\综合业务平台数据\浮标\FB.xls"; string strCon = String.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=‘Excel 12.0;HDR=YES;IMEX=0‘", str_filename); //新建连接
OleDbConnection myConn = new OleDbConnection(strCon);
myConn.Open(); //新建SQL语句,获取 Sheet1 全部数据 string strCom = "SELECT * FROM [Sheet1$]";
//用于将数据添加到DataSet中 OleDbDataAdapter myDataAdapter = new OleDbDataAdapter(strCom, myConn); DataSet myDataSet = new DataSet(); myDataAdapter.Fill(myDataSet, "[Sheet1$]"); myConn.Close();
//初始化DataTable实例 DataTable dt = myDataSet.Tables[0];
【210】通过OleDb读写Excel数据到DataTable
标签:
原文地址:http://www.cnblogs.com/alex-bn-lee/p/5765355.html