string strConn = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source= " + v_strExcelPath + ";Extended Properties='Excel 12.0;IMEX=1'";

            OleDbDataAdapter myDataAdapter = null;
            OleDbConnection conn = new OleDbConnection(strConn);
            OleDbCommand myOleDbCommand = new OleDbCommand("SELECT * FROM [" + v_strSheetName + "$]", conn);

            try
            {
                conn.Open();
                myDataAdapter = new OleDbDataAdapter(myOleDbCommand);
                myDataAdapter.Fill(dt);
                conn.Dispose();
            }


读取Excel中的所有栏位名称:

 1 ArrayList lv_arrList = new ArrayList();
 2             string strConn = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source= " + v_strExcelPath + ";Extended Properties='Excel 12.0;IMEX=1'";
 3             OleDbConnection conn = new OleDbConnection(strConn);
 4             try
 5             {               
 6                 conn.Open();
 7                 DataTable sheetname = conn.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });
 8                 conn.Close();
 9                 conn.Dispose();
10                 foreach (DataRow dr in sheetname.Rows)
11                 {
12                     lv_arrList.Add(dr[2].ToString().Replace("$", "").Replace("'", ""));
13                 }
14             }

 

相关文章:

  • 2022-12-23
  • 2021-06-02
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-07-26
猜你喜欢
  • 2022-12-23
  • 2021-09-27
  • 2022-12-23
  • 2022-12-23
  • 2022-01-07
  • 2022-12-23
  • 2022-01-20
相关资源
相似解决方案