Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cell with float numbers and datetimes #31

Open
soft-user opened this issue Aug 23, 2019 · 10 comments
Open

cell with float numbers and datetimes #31

soft-user opened this issue Aug 23, 2019 · 10 comments

Comments

@soft-user
Copy link

Hi,
I tried TZEXMLSS with delphi 10.0.
If I create a cell of type string or int everything is ok, but with float or datetime cells I have some problems.
I tried with
MyDoc.Sheets [0] .Cell [i, j] .Data: = FloatToStr (Mytab.Fields [i] .AsFloat); MyDoc.Sheets [0] .Cell [i, j] .CellType: = ZENumber;
and
MyDoc.Sheets [0] .Cell [i, j] .Data: = DateTimeToStr (Mytab.Fields [i] .Asdatetime); MyDoc.Sheets [0] .Cell [i, j] .CellType: = ZEDateTime;
and
TZXMLSSave.From(MyDoc).Charset('utf-8', TextConverter).Save(my_name);
but
in excel date cell is empty and float cell contain a date like 30/12/1899

can you help me?

@Avemey
Copy link
Owner

Avemey commented Aug 23, 2019

Hi!

It seems like you forget to use number formats. For float/datetime cells it is required to set concrete number format.
Here is example:

 ```
  MyDoc.Styles.Count := 2;

  MyDoc.Styles[0].NumberFormat := '#.###';
  MyDoc.Styles[1].NumberFormat := 'YYYY.MM.DD';

  MyDoc.Sheets[0].RowCount := 20;
  MyDoc.Sheets[0].ColCount := 20;

  MyDoc.Sheets[0].Cell[0, 0].AsDouble := 20.5;
  MyDoc.Sheets[0].Cell[0, 0].CellStyle := 0;

  MyDoc.Sheets[0].Cell[0, 1].AsDateTime := now();
  MyDoc.Sheets[0].Cell[0, 1].CellStyle := 1;

More about number formats http://avemey.com/zexmlss/numformat.php?lang=en

@soft-user
Copy link
Author

soft-user commented Aug 23, 2019

Thanks.
Now is better.
Number it's ok.

for date i had always same problem.
after
MyDoc.Sheets[0].Cell[0, 1].AsDateTime := now();
MyDoc.Sheets[0].Cell[0, 1].AsDateTime is 23/08/2019 22:58:25
but in excel is empty cell
i set
MyDoc.Sheets[0].Cell[0, 1].CellType := ZEDateTime; MyDoc.Sheets[0].Cell[0, 1].AsDateTime := now(); MyDoc.Sheets[0].Cell[0, 1].CellStyle := 1
and i try with
MyDoc.Styles[1].NumberFormat := 'YYYY.MM.DD';
and
MyDoc.Styles[1].NumberFormat := 'DD/MM/YYYY';

where am I wrong?

@Avemey
Copy link
Owner

Avemey commented Aug 24, 2019

Can you say version of MS Office that you use?
I check this with Office 365 (last updates 2019) - it is ok, date is visible and correct.
But old ms office 2003 with installed file format converters (xlsx) not shows dates because it is very old.
Do you still use excel 2003? If yes try to use excel xml format instead of xlsx.

@jonyrh
Copy link

jonyrh commented Aug 26, 2019

Hi!

What NumberFormat to use for the separation of bits?
I tryet set NumberFormat:='# ##0', but in document format "#\ ##0"

Using Office 2019 last updates, RAD 10.3.2 last updates & fixes

@Avemey
Copy link
Owner

Avemey commented Aug 26, 2019

Hi!
What do you mean about separation of bits?
Do you need to view number cell 200051 as "200 051"?
If yes than correct number formats are both "# ##0" and "#\ ##0" because backslash ("\") followed by letter means something similar to escape sequence like in c/c++/c# strings.
For example you can not use hash (#) symbol directly in number format because it has meaning of significant digit, but using escape you can show number cell "20" as "20#" by using number format like "#\#".
Some signs required to be escaped (#, ", ') but white space is not required to be escaped.

@jonyrh
Copy link

jonyrh commented Aug 26, 2019

Я пытаюсь разделить число на разряды:

tz.Styles[0].NumberFormat:='##'; - что использовать?
with tz.Sheets[0] do
begin
RowCount := 1;
ColCount := 1;
Title := 'Sheet 1';
Cell[0, 0].CellStyle := 0;
Cell[0, 0].AsInteger:=100000000;
end;

Как я только не пытался:
tz.Styles[0].NumberFormat:='### ### ### ##0'; - Даёт разделение числа на разряды, но Еxcel добавляет пробелы, и если посмотреть формат в свойствах ячейки, то он выглядит так ###\ ###\ ###\ ##0
Если так:
tz.Styles[0].NumberFormat:='#.##0'; - то результат есть, но только до сотых, число больше 1000 уже не делится на разряды...

Подскажите, пожалуйста, какой формат использовать, чтобы число делилось на разряды так 999 999 999 999 и если число ноль, то ставилась одна цифра 0.

@Avemey
Copy link
Owner

Avemey commented Aug 26, 2019

Вообще-то это странно, т.к. в данном случае должно хватать формата "# ##0". И у меня даже excel 2003 с патчами разделяет нормально.
Как вариант - если уж экселю что-то не нравится, можно попробовать: "###\ ###\ ###\ ###\ ###\ ##0".
Ну и для отрицательных чисел можно задействовать формат, чтобы минус не отъезжал далеко:
# ###;"-"# ###;0

@jonyrh
Copy link

jonyrh commented Aug 27, 2019

Все равно не правильно проставляет формат, использовал несколько вариантов:
1) # ##0 - показывает только разряд до тысячи
Снимок 1
2) ###\ ###\ ###\ ##0 или ### ### ### ##0 - Более похож на правду, но числа не по центру ячейки, впереди добавляются пробелы.
Снимок 2
3) # ###;"-"# ###;0 - как и в первом варианте
Снимок 3

Как видно, откуда то добавляются обратные слэши, даже если их не использовать в формате... Excel 2019 все обновления...
Мини-проект тут exe-файл удалять не стал, для проверки...

@jonyrh
Copy link

jonyrh commented Aug 27, 2019

Нашел правильный формат методом тыка!
Удивительно, но если вместо пробела поставить запятую, то формат будет идеальный!
#,##0 - при таком формате правильно отображается в Excel, но если этот формат применить в самом Excel, то получим, как и ожидалось, число с запятой! Костыль конечно, но хоть так работает как необходимо.
Спасибо за уделенное время!

@the-Arioch
Copy link
Contributor

the-Arioch commented Feb 12, 2020

This kind of questions is almost impossible to solve without

  1. Making and uploading specific sample of code
  2. Making and uploading specific resulting XLSX file
  3. At least reporting Delphi version and Windows codepage, but better to report all the contents of TFormatSettings global variable (for example Delphi Xe2 has RTL bug and corrupts the variable on Russian Windows).

There is probably some quirk inside generated XLSX, and why not - i found and documented few others back in 2013 - which can not properly be fixed without samples.

Otherwise it will always be just randomly finding ad hoc work-arounds, that only can work with one specific project, and then next another projects would have to find different hacks.

@MtwStark MtwStark mentioned this issue Feb 12, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants