justkawal / excel

Excel Library for Flutter and Dart - https://pub.dev/packages/excel
MIT License
416 stars 229 forks source link

Cells with a time below `02:25:00` are recognized as `DateTimeCellValue` #356

Closed enwi closed 2 months ago

enwi commented 4 months ago

I am not sure if this is a real issue, but it seems odd to me, why a Cell with a time below 02:25:00 is interpreted as a DateTimeCellValue while a cell onwards from 02:25:00 is interpreted as a TimeCellValue.

Here is a minimal working example:

import 'dart:io';

import 'package:excel/excel.dart';
import 'package:flutter/material.dart';

void main() {
  runApp(const MainApp());
}

class MainApp extends StatefulWidget {
  const MainApp({super.key});

  @override
  State<MainApp> createState() => _MainAppState();
}

class VisualCell {
  final int index;
  final String value;

  VisualCell({
    required this.index,
    required this.value,
  });
}

class _MainAppState extends State<MainApp> {
  List<VisualCell> cells = [];

  void _readExcel() {
    File('example.xlsx')
        .readAsBytes()
        .then((bytes) => Excel.decodeBytes(bytes))
        .then((excel) {
      final table = excel['Tabelle1'];

      cells.clear();
      for (int rowIndex = 0; rowIndex <= 10; ++rowIndex) {
        final value = table
            .cell(
                CellIndex.indexByColumnRow(columnIndex: 0, rowIndex: rowIndex))
            .value;

        if (value == null) {
          cells.add(VisualCell(index: rowIndex, value: 'null'));
        }

        if (value is TimeCellValue) {
          cells.add(VisualCell(index: rowIndex, value: value.toString()));
        }

        if (value is DateTimeCellValue) {
          cells.add(VisualCell(index: rowIndex, value: value.toString()));
        }
      }
      setState(() {});
    });
  }

  @override
  void initState() {
    _readExcel();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: SingleChildScrollView(
          child: Column(
            children: [
              const Text('Cell Values'),
              ...cells.map(
                (e) => ListTile(
                  title: Text(e.value),
                  subtitle: Text('${e.index}'),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

example.xlsx

luizgpa commented 2 months ago

It seems that Excel stores the smaller values using "E notation"

02:24:00 is stored as 0.1 02:20:00 is stored as 9.7222222222222224E-2 (and not 0.097222222222222224)

There is a PR that fix the issue https://github.com/justkawal/excel/pull/359