提供者选择时,试图在Delphi 7中访问Excel表 [英] Provider selection, when trying to access Excel table in Delphi 7

查看:289
本文介绍了提供者选择时,试图在Delphi 7中访问Excel表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用TAdoConnection组件从Delphi 7连接到excel表。
问题是当我选择Microsoft.Jet.OLEDB.4.0,扩展属性=Excel 8.0;时,有时会收到错误,

I am trying to connect to excel table from Delphi 7 using TAdoConnection component. The problem is when I select Microsoft.Jet.OLEDB.4.0, Extended Properties="Excel 8.0;", I sometimes receive error,


外部表不在
预期格式。

that external table is not in the expected format.

当我选择:
提供者= Microsoft.ACE.OLEDB.12.0;扩展属性= Excel 12.0;
然后一些用户收到以下错误:

When i select: Provider=Microsoft.ACE.OLEDB.12.0;Extended Properties=Excel 12.0; then some users receive following error:


无法找到提供程序,可能不正确安装

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

有没有办法解决我的问题?

Is there a way to solve my problem?

推荐答案

已经太久了,因为我研究了这一点来记住细节,但这里是我们正在使用Excel的一个例子。希望这有帮助...

It's been too long since I researched this to remember the details, but here is a sample of what we are doing with Excel. Hope this helps...

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.

这篇关于提供者选择时,试图在Delphi 7中访问Excel表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆