Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Excel To ADO DataSet Error
Message
De
16/02/2009 10:20:55
Cetin Basoz
Engineerica Inc.
Izmir, Turquie
 
Information générale
Forum:
ASP.NET
Catégorie:
Code, syntaxe and commandes
Divers
Thread ID:
01382016
Message ID:
01382089
Vues:
32
>I'm trying to get the connection string right. If I use:
>
>
>string sExcelConnString =
>    "provider=Microsoft.Jet.OLEDB.4.0; data source='" + sFileName + " '; " + "Extended Properties=Excel 8.0;";
>
>then it works fine.
>
>However, if I add the "Hdr=Yes" on the end...
>
>
>string sExcelConnString =
>    "provider=Microsoft.Jet.OLEDB.4.0; data source='" + sFileName + " '; " + "Extended Properties=Excel 8.0;Hdr=Yes";
>
>
>
>I get "Could not find installable ISAM". I googled it but I don't see what the problem is.
>
>Anyone?

As Yuri said already you need to use dfouble quotes for exended properties (but since you are using C# do it C# style:) I recommend ACE driver over Jet driver (to my observation some extended properties are not working at all with Jet). Anyway this works for me:
using System;
using System.Data;
using System.Data.OleDb;

using System.Windows.Forms;
using System.Drawing;

class excelRead
{
 static void Main()
 {
    //string excelConAce = 
    //  "Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=\"Excel 12.0;HDR=Yes\"";

    string excelConJet = 
      "Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=\"Excel 8.0;HDR=Yes\"";

    
    OpenFileDialog ofd = new OpenFileDialog();

    ofd.InitialDirectory = "c:\\" ;
    ofd.Filter = "Excel files (*.xls, *.xlsx)|*.xls*" ;
    ofd.FilterIndex = 2 ;
    ofd.RestoreDirectory = true ;

    if(ofd.ShowDialog() == DialogResult.OK)
    {
       string datasource = String.Format(excelConJet,ofd.FileName);
       ShowExcelSheetData(datasource);
    }
 }
 
 public static void ShowExcelSheetData(string conStr)
 {
    OleDbConnection con = new OleDbConnection(conStr);
    con.Open();

    DataTable t  = con.GetSchema("Tables",new string[] {null,null,null,"TABLE"});
    foreach(DataRow row in t.Rows)
    {
       DataTable sheet = new DataTable();
       string sheetName = (string)row["TABLE_NAME"];
       OleDbCommand cmd = new OleDbCommand(
            String.Format("select * from [{0}]", sheetName), con);
       OleDbDataReader rdr = cmd.ExecuteReader();
       sheet.Load(rdr);

       Form f = new ShowDataForm(sheet,sheetName);
       f.ShowDialog();
    }
    con.Close();
 }
}

public class ShowDataForm : Form
{
  public ShowDataForm(DataTable tbl, string caption)
  {
     this.dgv = new System.Windows.Forms.DataGridView();
     this.dgv.Location = new System.Drawing.Point(0, 0);
     this.dgv.Dock = DockStyle.Fill;
     this.dgv.DataSource = tbl;
     this.Text = caption;
     this.Controls.Add(this.dgv);
     this.ClientSize = new System.Drawing.Size(1024, 768);
  }
  private System.Windows.Forms.DataGridView dgv;
}
Cetin
Çetin Basöz

The way to Go
Flutter - For mobile, web and desktop.
World's most advanced open source relational database.
.Net for foxheads - Blog (main)
FoxSharp - Blog (mirror)
Welcome to FoxyClasses

LinqPad - C#,VB,F#,SQL,eSQL ... scratchpad
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform