Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/delphi/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Delphi 7中尝试访问Excel表时选择提供程序_Delphi_Excel_Oledb_Ado_Jet - Fatal编程技术网

在Delphi 7中尝试访问Excel表时选择提供程序

在Delphi 7中尝试访问Excel表时选择提供程序,delphi,excel,oledb,ado,jet,Delphi,Excel,Oledb,Ado,Jet,我正在尝试使用TAdoConnection组件从Delphi7连接到excel表。 问题是,当我选择Microsoft.Jet.OLEDB.4.0,Extended Properties=“Excel 8.0;”时,有时会收到错误 该外部表不在列表中 预期格式 当我选择: Provider=Microsoft.ACE.OLEDB.12.0;扩展属性=Excel 12.0; 然后,一些用户收到以下错误: “找不到提供程序。可能找不到 正确安装” 有办法解决我的问题吗 我已经很久没有研究过这个了,记

我正在尝试使用TAdoConnection组件从Delphi7连接到excel表。 问题是,当我选择Microsoft.Jet.OLEDB.4.0,Extended Properties=“Excel 8.0;”时,有时会收到错误

该外部表不在列表中 预期格式

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

“找不到提供程序。可能找不到 正确安装”


有办法解决我的问题吗

我已经很久没有研究过这个了,记不清细节了,但下面是我们使用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.

有趣的是,在这两种情况下,都是客户收到了这个错误,而不是我。对于客户端使用的同一excel文件,Bot方式对我有效。通过从microsoft page安装提供程序,我解决了“无法找到提供程序。它可能未正确安装”的问题,但现在用户再次获得“该外部表不是预期格式”。我完全糊涂了。我也尝试过Excel版本11,因为我认为该文件是在Excel 2003上准备的,但随后收到错误“找不到可安装的ISAM”。什么是TAgCustomPlugin?您能简单描述一下如何使用这个类吗?TAgCustomPlugin基本上与TForm对象相同。这是我们创建的一个自定义对象,它派生自TForm。