Recent

Author Topic: fpspread - how to open a workbook in read only mode  (Read 4648 times)

kjteng

  • Sr. Member
  • ****
  • Posts: 259
fpspread - how to open a workbook in read only mode
« on: November 30, 2017, 09:45:50 am »
Currently I can open an excel workbook by setting the filename property of the TsWorkbook component. However, this would raise an exception if the workbook is already opened in ms-office (in readwrite mode).
 
Is there a way  to open a workbook in 'read only' TsWorkbook?   

wp

  • Hero Member
  • *****
  • Posts: 11858
Re: fpspread - how to open a workbook in read only mode
« Reply #1 on: November 30, 2017, 10:12:51 am »
What exactly are you doing?

This program opens the file 'test.xls' although it is opened by Excel or LibreOffice:
Code: Pascal  [Select][+][-]
  1. program Project2;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. uses
  6.   Classes, fpspreadsheet, fpstypes, fpsallformats;
  7.  
  8. var
  9.   wb: TsWorkbook;
  10.   ws: TsWorksheet;
  11. begin
  12.   wb := TsWorkbook.Create;
  13.   try
  14.     wb.ReadFromFile('test.xls', sfExcel8);
  15.     ws := wb.GetFirstWorksheet;
  16.     WriteLn(ws.ReadAsText(0, 0));
  17.   finally
  18.     wb.free;
  19.   end;
  20.  
  21.   ReadLn;
  22. end.

kjteng

  • Sr. Member
  • ****
  • Posts: 259
Re: fpspread - how to open a workbook in read only mode
« Reply #2 on: November 30, 2017, 01:07:15 pm »
What exactly are you doing?

Just want to read some info from an excel file only. Your code works. TQ.
BTW, how come I got error for ws.ReadAsText(ii, 0) ?   I can only use ws.ReadAsUTF8Text(ii, 0) ?

wp

  • Hero Member
  • *****
  • Posts: 11858
Re: fpspread - how to open a workbook in read only mode
« Reply #3 on: November 30, 2017, 01:53:38 pm »
how come I got error for ws.ReadAsText(ii, 0) ?   I can only use ws.ReadAsUTF8Text(ii, 0) ?
Then you must have a very old version. 1.8.4 is current release (or use trunk from svn).

kjteng

  • Sr. Member
  • ****
  • Posts: 259
Re: fpspread - how to open a workbook in read only mode
« Reply #4 on: December 01, 2017, 03:00:49 pm »
how come I got error for ws.ReadAsText(ii, 0) ?   I can only use ws.ReadAsUTF8Text(ii, 0) ?
Then you must have a very old version. 1.8.4 is current release (or use trunk from svn).

TQ.  Downloaded and installed.  Now I have problem in opening .xlsx file.

I  get  "[TsSpreadBIFFReader.InternalReadFromStream] Reading o OLE document failed " when I call wb.ReadFromFile('test.xlsx', sfExcel8) method; 

The same file can be opened without any problem if I drop a sWorkbookSource1 component onto the form and set sWorkbookSource1.Filename := 'test.xlsx'

Any clue?

wp

  • Hero Member
  • *****
  • Posts: 11858
Re: fpspread - how to open a workbook in read only mode
« Reply #5 on: December 01, 2017, 04:17:52 pm »
In order to read a spreadsheet file you must add the corresponding reader unit to "uses", in case of sfExcel8 (.xls) this is xlsBiff8, in case of the newer .xlsx (sfOOXML) this is xlsOOXML (or: fpsallformats for all built-in speadsheet formats). And you must select the correct format specifier in the wb.ReadFromFile call (or drop it for automatic detection):

Code: Pascal  [Select][+][-]
  1. uses
  2.   xlsOOXML;
  3. [...]
  4.   wb.ReadfromFile('test.xlsx', sfOOXML);  // or: wb.ReadFromFile('test.xlsx');

In your call your call  ('ReadFromfile('tst.xlsx', sfExcel8)) you are forcing fpspreadsheet to open the file 'test.xlsx' as an xls file which does not work, of course.
« Last Edit: December 01, 2017, 04:57:59 pm by wp »

kjteng

  • Sr. Member
  • ****
  • Posts: 259
Re: fpspread - how to open a workbook in read only mode
« Reply #6 on: December 02, 2017, 01:57:49 am »
Thanks again for the answer. 
Call wb.ReadFromFile(filename)  without the second parameter solve the problem.

 

TinyPortal © 2005-2018