2010-02-22 19 views
2

J'essaie de me connecter à la table Excel à partir de Delphi 7 en utilisant le composant TAdoConnection. Le problème est quand je sélectionne Microsoft.Jet.OLEDB.4.0, Propriétés étendues = "Excel 8.0;", je reçois parfois erreur,Sélection du fournisseur, lors de la tentative d'accès à la table Excel dans Delphi 7

that external table is not in the expected format.

Lorsque je sélectionne: Provider = Microsoft.ACE.OLEDB.12.0 Propriétés étendues = Excel 12.0; alors certains utilisateurs reçoivent l'erreur suivante:

"Provider cannot be found. It may not be properly installed".

est-il un moyen de résoudre mon problème?

+0

Intéressant que dans les deux cas, c'est le client qui reçoit cette erreur, pas moi. Les méthodes de Bot fonctionnent pour moi, pour le même fichier Excel, qui est utilisé par le client. –

+0

J'ai résolu le problème "Le fournisseur ne peut pas être trouvé, il peut ne pas être installé correctement" en installant le fournisseur de la page Microsoft, mais maintenant l'utilisateur obtient "cette table externe n'est pas dans le format attendu". Je suis totalement confus. J'ai également essayé Excel Version 11, parce que je pense que le fichier a été préparé sur Excel 2003, mais alors l'erreur "Impossible de trouver l'ISAM installable" est reçue. –

Répondre

4

Cela faisait trop longtemps que je n'avais pas fait de recherches pour me rappeler les détails, mais voici un exemple de ce que nous faisons avec Excel. Espérons que cela aide ...

type 
    TConvertExcel = class(TAgCustomPlugin) 
    ADOConnection1: TADOConnection; 
    procedure FormCreate(Sender: TObject); 
    private 
    FSheetName: string; 
    FExcelVersion: Currency; 

    procedure ConnectToExcel; 
    function ExcelSupported: Boolean; 
    function GetExcelVersion: Currency; 
    end; 

var 
    ConvertExcel: TConvertExcel; 

implementation 

uses ... 

{$R *.dfm} 

{ 
    TConvertExcel.FormCreate 
    --------------------------------------------------------------------------- 
} 
procedure TConvertExcel.FormCreate(Sender: TObject); 
begin 
    FExcelVersion := GetExcelVersion; 

    if ExcelSupported = False then 
    begin 
    grpConvertExcel.Visible := False; 
    if FExcelVersion = 0 then 
     lblNoExcel.Caption := 'Microsoft Excel Not Installed!' 
    else 
     lblNoExcel.Caption := 'Microsoft Excel Version Not Supported!'; 
    lblNoExcel.Caption := lblNoExcel.Caption + AsciiCRLF + AsciiCRLF + 
     'Microsoft Excel 2003 or 2007 must be installed before an excel file can be converted.'; 
    lblNoExcel.Visible := True; 
    exit; 
    end; 

end; 

{ 
    TConvertExcel.GetExcelVersion 
    --------------------------------------------------------------------------- 
} 
function TConvertExcel.GetExcelVersion: Currency; 
var 
    ClassID: TCLSID; 
    strOLEObject: string; 
    Excel: OleVariant; 
begin 

    result := 0; 

    strOLEObject := 'Excel.Application'; 

    if (CLSIDFromProgID(PWideChar(WideString(strOLEObject)), ClassID) = S_OK) then 
    begin 
    Excel := CreateOleObject(strOLEObject); 
    // qqqxxx - Should we be casting this differently? 
    result := Excel.Version; 
    end; 

end; 

{ 
    TConvertExcel.ExcelSupported 
    --------------------------------------------------------------------------- 
} 
function TConvertExcel.ExcelSupported: Boolean; 
begin 
    result := False; 
    if (FExcelVersion = 11.0) or // Excel 2003 
    (FExcelVersion = 12.0) then // Excel 2007 
    result := True; 
end; 

{ 
    TExcelConverterPreview.ConnectToExcel 
    --------------------------------------------------------------------------- 
} 
procedure TConvertExcel.ConnectToExcel; 
var 
    strConn: widestring; 
    SheetNameList: TStringList; 
begin 

/* 
when connecting to Excel "database", 
extended properties are used to set the Excel file version. 
For an Excel95 workbook this value is "Excel 5.0" (without the quotes), 
for versions Excel 97, Excel 2000, Excel 2002 or ExcelXP the value is "Excel 8.0". 

IMEX=1;" tells the driver to always read the registry at 
Hkey_Local_Machine/Software/Microsoft/Jet/4.0/Engines/Excel/ImportMixedTypes. 
If ImportMixedTypes=Text then intermixed types will always be cast to Text. 
If ImportMixedTypes=Majority Type then intermixed types will result in Null values. 
Luckily Text seems to be the default. 
*/ 

    SheetNameList := nil; 

    if FExcelVersion = 11.0 then 
    strConn :='Provider=Microsoft.Jet.OLEDB.4.0;' + 
     'Data Source="' + txtInputFile.Text + '";' + 
     'Extended Properties="Excel 8.0;HDR=No;IMEX=1"' 
    else if FExcelVersion = 12.0 then 
    strConn := 'Provider=Microsoft.ACE.OLEDB.12.0;' + 
     'Data Source="' + txtInputFile.Text + '";' + 
     'Extended Properties="Excel 12.0 Xml;HDR=No;IMEX=1"' 
    else 
    raise (Exception.Create(
     'The Excel Version "' + CurrToStr(FExcelVersion) + 
     '" is not supported by the Excel Conversion.')); 

    AdoConnection1.Connected := False; 
    AdoConnection1.ConnectionString := strConn; 

    try 
    SheetNameList := TStringList.Create(); 
    try 
     AdoConnection1.Open; 

     ADOConnection1.GetTableNames(SheetNameList, False); 
     FSheetName := SheetNameList[0]; 
    except 
     ShowMessage('Unable to connect to Excel!' + AsciiCRLF + 
        'Make sure the Excel file ' + txtInputFile.Text + ' exists with '+ 
        'sheet name ' + FSheetName + '.'); 
     raise; 
    end; 
    finally 
    FreeAndNil(SheetNameList); 
    end; 

end; 

end. 
+0

Qu'est-ce que TAgCustomPlugin? Et pouvez-vous décrire brièvement comment utiliser cette classe? –

+0

TAgCustomPlugin est fondamentalement identique à un objet TForm. C'est un objet personnalisé que nous avons créé qui est dérivé de TForm. –