Excel转DataSet的一个例子


public static DataSet ExcelToDS(string Pathm,string TableName)
    ...{
      DataSet ds = new DataSet();
      try
      ...{
        string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + Path + ";" + "Extended Properties=Excel 8.0;";
        OleDbConnection conn = new OleDbConnection(strConn);
        conn.Open();
        string strExcel = "";
        OleDbDataAdapter myCommand = null;
        strExcel = string.Format("select * from [{0}$]",TableName);
        myCommand = new OleDbDataAdapter(strExcel, strConn);
  
        myCommand.Fill(ds, TableName);
      }
      catch(Exception ex)
      ...{
        System.Windows.Forms.MessageBox.Show(ex.Message);
      }
      return ds;
    }
« 
» 
快速导航

Copyright © 2016 phpStudy | 豫ICP备2021030365号-3