timsoft

Excel文件路径C:\ExcelExample.xls
Excel数据有

ID 姓名 年龄 籍贯
1 孙强 25 山东
2 粱建全 23 河北唐山
3 宋修舵 23 河北

string ConnStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\ExcelExample.xls;Extended Properties=Excel 8.0";
                
string query = "SELECT * FROM [Sheet1$]";

                OleDbCommand oleCommand 
= new OleDbCommand(query, new OleDbConnection(ConnStr));
                OleDbDataAdapter oleAdapter 
= new OleDbDataAdapter(oleCommand);
                DataSet myDataSet 
= new DataSet();

                
// 将 Excel 的[Sheet1]表内容填充到 DataSet 对象
                oleAdapter.Fill(myDataSet, "[Sheet1$]");

                
// 数据绑定
                DataGrid1.DataSource = myDataSet;
                DataGrid1.DataMember 
= "[Sheet1$]";
                DataGrid1.DataBind();

分类:

技术点:

相关文章:

  • 2021-08-09
  • 2021-08-04
  • 2022-12-23
  • 2022-12-23
  • 2021-12-19
  • 2021-07-13
  • 2021-11-23
猜你喜欢
  • 2021-08-27
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-22
  • 2022-02-12
  • 2021-06-08
相关资源
相似解决方案