You can open excel in Excel without Excel installed;

Asked

Viewed 1,575 times

0

I am supporting a system that the programmer made use of Oleobject to open and read an Excel file It turns out that I work with brOffice and error Delphi when I try to open excel because the Ole object needs excel installed. I searched the web and saw that some suggest the inclusion of uses Comobj, already included but the same error continues.

---------------------------
Debugger Exception Notification
---------------------------
Project perfil.exe raised exception class EOleSysError with message 'Cadeia de caracteres de classe inválida'.
---------------------------
Break   Continue   Help   
---------------------------

Searching saw that the only way is to install the same excel!

The routine used to open the file is:

class function TFuncoes.XlsToStringGrid(AGrid: TStringGrid; AXLSFile: string):
  Boolean;
const
  xlCellTypeLastCell = $0000000B;
var
  XLApp, Sheet: OLEVariant;
  RangeMatrix: Variant;
  x, y, k, R: Integer;
begin
  Result := False;
  // Cria Excel- OLE Object
  XLApp := CreateOleObject('Excel.Application');
  try
    // Esconde Excel
    XLApp.Visible := False;
    // Abre o Workbook
    XLApp.Workbooks.Open(AXLSFile);
    Sheet := XLApp.Workbooks[ExtractFileName(AXLSFile)].WorkSheets[1];
    Sheet.Cells.SpecialCells(xlCellTypeLastCell, EmptyParam).Activate;
    // Pegar o número da última linha
    x := XLApp.ActiveCell.Row;
    // Pegar o número da última coluna
    y := XLApp.ActiveCell.Column;
    // Seta Stringgrid linha e coluna
    AGrid.RowCount := x;
    AGrid.ColCount := y;
    // Associaca a variant WorkSheet com a variant do Delphi
    RangeMatrix := XLApp.Range['A1', XLApp.Cells.Item[x, y]].Value;
    // Cria o loop para listar os registros no TStringGrid
    k := 1;
    repeat
      for R := 1 to y do
        AGrid.Cells[(R - 1), (k - 1)] := RangeMatrix[k, R];
      Inc(k, 1);
    until k > x;
    RangeMatrix := Unassigned;
  finally
    // Fecha o Excel
    if not VarIsEmpty(XLApp) then
    begin
      XLApp.Quit;
      XLApp := Unassigned;
      Sheet := Unassigned;
      Result := true;
    end;
  end;
end;

The function works on machines that have excel.

I didn’t want to have to install excel on my development machine.

Does anyone suggest another option to read these files? (I can’t exchange them for another file type as they come from third parties)

  • 1

    If you want to work with Broffice, the best would be to work with the latest version of Libreoffice, you will have to use Libreoffice objects and not Excel. Such objects are different in two software.

  • I use Libreoffice too, but the company that uses ERP does not use, then I get a snooker, I have to buy an excel only to support... ai fica dificil :) if you have an alternative without involving the company.

  • If the customer uses certain software and you sell your support services to this customer you need to have the software he uses.

  • My system does not necessarily need Excel, this was a request from him, in fact it was for the system to read a CNAB file that is type text, but the laziness of the client to talk to the manager to make this file available in the client area of the bank ended up generating me this setback, to help the client ended up reading an excel file even without a standard, now he changed bank and wants to keep the file, because the other bank also uses, but with another layout, I warned him that the correct is CNAB files, we will help and fall into these

  • 1

    Dude, this is the biggest Gambi. It goes from CNAB even, the more it gets worse. or so save in CSV, which is plain text.

  • @Bacco here we chose this, we import nothing from xls, we implement only csv. A lot of effort on the software/development side when the simplicity in saving in another format is two clicks in the user’s hand.

  • Worse than the system is full of these Pogs, I already told him to get the CNAB, but who knows when I will have these files, for now I have no other option but to patch, I managed to download a version of the office on the microsoft site that is not locked (it may catch later), with it installed I was able to open the files by Delphi, it is called Office365proplus - it is a release for testers, but for my case this great :) - I do not install these pirate versions around because they are full of viruses.

Show 2 more comments

1 answer

1

I know the Flexcel component of TMS.

Flexcel for VCL / Firemonkey is a suite of Delphi Components allowing to Manipulate Excel files. It includes an extensive API allowing to natively read/write Excel files. If you need to read or create Complex spreadsheets, on Windows or macOS on machines without Excel installed, Flexcel can do the job.

https://www.tmssoftware.com/site/flexcel.asp

Browser other questions tagged

You are not signed in. Login or sign up in order to post.