1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
|
using System.Data.OleDb;
namespace Test_Excel
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string chemin = "C:/Users/IMANE/Documents ";
string Feille = "feuil1";
string strCnx = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + chemin + ";Extended Properties=" + (char)34 + "Excel 8.0;HDR=Yes;" + (char)34 + ";";
System.Data.DataTable dtRetour = null;
System.Data.OleDb.OleDbConnection oCnx = new System.Data.OleDb.OleDbConnection(strCnx);
System.Data.OleDb.OleDbCommand objCmdSelect = null;
System.Data.OleDb.OleDbDataAdapter objAdapter = null;
System.Data.DataSet oDs = new DataSet();
if (chemin != string.Empty && Feille != string.Empty)
{
if (System.IO.File.Exists(chemin))
{
oCnx.Open();
objCmdSelect = new System.Data.OleDb.OleDbCommand("SELECT * FROM [" + Feille + "$]", oCnx);
objAdapter = new System.Data.OleDb.OleDbDataAdapter();
objAdapter.Fill(oDs, "XLData");
if (oDs != null && oDs.Tables != null && oDs.Tables.Count > 0)
{
dtRetour = oDs.Tables[0];
}
else
{
dtRetour = null;
}
}
oCnx.Close();
}
GridView1.DataSource = dtRetour;
} |
Partager