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)
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.
– anonimo
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.
– Marcelo
If the customer uses certain software and you sell your support services to this customer you need to have the software he uses.
– anonimo
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
– Marcelo
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
@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.
– Junior Moreira
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.
– Marcelo