2010-02-22 64 views
2

我想連接到使用TAdoConnection組件的Delphi 7的Excel表。 問題是,當我選擇Microsoft.Jet.OLEDB.4.0,擴展屬性= 「Excel中8.0;」,我有時接收錯誤,供應商選擇,當試圖訪問Excel表7中的德爾福7

that external table is not in the expected format.

當我選擇: 提供商= Microsoft.ACE.OLEDB.12.0 ;擴展屬性= Excel 12.0; 那麼一些用戶會收到以下錯誤:

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

有沒有解決我的問題的方法嗎?

+0

有趣的是,在這兩種情況下,它是客戶端收到此錯誤,而不是我。對於我來說,Bot方式適用於客戶端使用的同一個excel文件。 – 2010-02-22 17:18:45

+0

我通過安裝微軟頁面的提供者解決了「提供程序無法找到,可能安裝不正確」的問題,但是現在用戶再次得到「該外部表格未處於預期格式」。我完全困惑。我也嘗試過Excel版本11,因爲我認爲該文件是在Excel 2003上準備的,但是接收到錯誤「無法找到可安裝的ISAM」。 – 2010-02-23 09:28:52

回答

4

自從我研究這個過程來記住細節已經太久了,但這裏是我們用Excel做的一個樣本。希望這有助於...

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

什麼是TAgCustomPlugin?你能簡要描述一下如何使用這個類嗎? – 2010-02-23 08:37:05

+0

TAgCustomPlugin與TForm對象基本相同。這是我們創建的自定義對象,它是從TForm派生的。 – 2010-03-09 17:28:12