Recent

Author Topic: Data type  (Read 3455 times)

krzynio

  • Full Member
  • ***
  • Posts: 110
    • Krzynio's home page
Data type
« on: March 08, 2013, 02:13:20 pm »
Hello!

I am writing a procedure for exporting data from Excel file to SQL database.
My question id how to determine cell data type in worksheet, if it is generally possible?
Some fields are strings but contains numbers and some are pure floats.
I do it like this:

    for j:=0 to MyWorksheet.GetLastColNumber do begin
      try
        f := MyWorksheet.ReadAsNumber(i,j);
        s := s + FloatToStr(f)+',';
      except
        s := s+''''+MyWorksheet.ReadAsUTF8Text(i,j)+''',';
      end;
    end;

but sometimes it fails because strings with numbers only are threaten as floats and strings with letters are read as '0'.
Therefore I need to distinguish pure floats, then convert it string, then replace coma with dot and put to SQL script.
Please help.

Regards,
Krzysztof
« Last Edit: March 08, 2013, 02:26:48 pm by krzynio »
Debian 12.8 x64, / Windows 11 PL - latest updates
Lazarus 3.6

paweld

  • Hero Member
  • *****
  • Posts: 1571
Re: Data type
« Reply #1 on: March 08, 2013, 03:34:36 pm »
Code: [Select]
var
  MyWorkbook: TsWorkbook;
  MyWorksheet: TsWorksheet;
  i: Integer;
  CurCell: PCell;
begin
  MyWorkbook := TsWorkbook.Create;
  MyWorkbook.ReadFromFile('c:\test.xls');
  MyWorksheet := MyWorkbook.GetWorksheetByIndex(0);

  CurCell := MyWorkSheet.GetFirstCell();
  for i := 0 to MyWorksheet.GetCellCount - 1 do
  begin
    case CurCell^.ContentType of
    cctNumber: sg.Cells[CurCell^.Col, CurCell^.Row]:='Liczba: '+MyWorkSheet.ReadAsUTF8Text(CurCell^.Row, CurCell^.Col);
    cctUTF8String: sg.Cells[CurCell^.Col, CurCell^.Row]:='Tekst: '+MyWorkSheet.ReadAsUTF8Text(CurCell^.Row, CurCell^.Col);
    end;
    CurCell := MyWorkSheet.GetNextCell();
  end;
end;
Best regards / Pozdrawiam
paweld

krzynio

  • Full Member
  • ***
  • Posts: 110
    • Krzynio's home page
Re: Data type
« Reply #2 on: March 08, 2013, 04:58:58 pm »
Thanks Pawel!

CurCell^.ContentType solves the problem.

Regards,
Krzysztof
Debian 12.8 x64, / Windows 11 PL - latest updates
Lazarus 3.6

 

TinyPortal © 2005-2018