搜尋套件、儲存庫與 APP 分類

套件詳情v2.0.0

excel_to_json

一個 Flutter 套件,可將 Excel (.xlsx) 檔案轉換為 JSON 格式,支援多個工作表,並具備完整的錯誤處理功能。

AndroidiOSWebmacOSWindowsLinux
近 30 天下載
668
喜歡數
24
Pub 分數
160

安裝套件

flutter pub add excel_to_json

套件介紹

根據套件公開資料產生的選型評估,僅供參考。

一個 Flutter 套件,可將 Excel (.xlsx) 檔案轉換為 JSON 格式,支援多個工作表並提供強大的錯誤處理功能。它能實現跨所有主要平台的電子試算表資料與 Flutter 應用程式的無縫整合。此套件非常適合將結構化資料從 Excel 檔案匯入應用程式,確保即使在複雜試算表中也能準確解析並獲得可靠結果。專為需要高效地將 Excel 轉換為 JSON 而無需外部相依性的開發者設計。

適用情境

  • 將 Excel 檔案中的資料匯入 Flutter 應用
  • 將多工作表 Excel 檔案轉換為 JSON
  • 在網頁和行動應用中解析結構化電子表格資料
  • 從 Excel 源自動獲取資料
  • 處理大型或複雜的 Excel 檔案時具備錯誤容錯能力

主要特色

  • 支援單一 Excel 檔案中的多個工作表
  • 對格式錯誤檔案的全面錯誤處理
  • 跨平台相容性(Android、iOS、Web、macOS、Windows、Linux)
  • 將 Excel 轉換為 JSON 的簡單 API
  • 在轉換過程中保留資料類型

適合對象

  • 開發需要匯入Excel資料的應用程式之開發者
  • 處理結構化電子表格資料的團隊
  • 需要從Excel進行自動化數據處理的應用程式
  • 需要離線資料匯入的行動與網頁應用程式
  • 涉及從 Excel 到 JSON 的數據遷移的項目

使用建議

  • 需要有效的 .xlsx 檔案才能成功轉換
  • 大檔案可能會影響低階裝置的效能
  • 僅限 Excel (.xlsx) 格式,不包含 .xls
  • 沒有內建的檔案選擇介面(必須與檔案選擇器整合)
  • 取決於底層解析庫的準確性

原始 README

以下為關聯原始碼儲存庫的 README 原文快照,最新內容請造訪原始碼儲存庫。

Excel to JSON

pub package License: MIT pub points popularity

A Flutter package that converts Excel (.xlsx) files to JSON format, making it easy to work with spreadsheet data in your Flutter applications.

🚀 Quick Example

import 'package:excel_to_json/excel_to_json.dart';

// Simple conversion
final converter = ExcelToJson();
final jsonString = await converter.convert(); // Opens file picker

// With custom configuration  
final converter = ExcelToJson(
  config: ExcelToJsonConfig(
    includeEmptyRows: true,
    verbose: true,
  ),
);
final result = await converter.convert();

Features

✅ Simple Integration - Easy to use API with just a few lines of code
✅ Multiple Worksheets - Supports Excel files with multiple sheets
✅ Type Safety - Handles various cell types (text, numbers, dates, formulas, booleans)
✅ Configurable - Customizable conversion options
✅ Error Handling - Comprehensive error reporting and logging
✅ Cross Platform - Works on Android, iOS, Web, and Desktop

Installation

Add this to your package's pubspec.yaml file:

dependencies:
  excel_to_json: ^1.5.0

Then run:

flutter pub get

Quick Start

Basic Usage
import 'package:excel_to_json/excel_to_json.dart';

void convertExcelToJson() async {
  final converter = ExcelToJson();
  
  try {
    final jsonString = await converter.convert();
    if (jsonString != null) {
      print('Conversion successful!');
      print(jsonString);
    } else {
      print('No file selected or conversion cancelled.');
    }
  } on ExcelToJsonException catch (e) {
    print('Conversion failed: ${e.message}');
  }
}
Advanced Usage with Configuration
import 'package:excel_to_json/excel_to_json.dart';

void convertWithConfig() async {
  const config = ExcelToJsonConfig(
    includeEmptyRows: true,        // Include rows with all empty cells
    useDateTimeStrings: false,     // Keep date objects as native types
    verbose: true,                 // Enable detailed logging
  );
  
  final converter = ExcelToJson(config: config);
  
  try {
    final jsonString = await converter.convert();
    if (jsonString != null) {
      // Process your JSON data
      final data = jsonDecode(jsonString);
      
      // Access worksheets by name
      final sheet1Data = data['Sheet1'] as List<dynamic>;
      for (final row in sheet1Data) {
        print('Row data: $row');
      }
    }
  } on ExcelToJsonException catch (e) {
    print('Error: ${e.message}');
    if (e.cause != null) {
      print('Caused by: ${e.cause}');
    }
  }
}

Configuration Options

The ExcelToJsonConfig class provides several options to customize the conversion:

Option Type Default Description
includeEmptyRows bool false Include rows where all cells are empty
useDateTimeStrings bool true Convert date/time values to strings
dateFormat String? null Custom date format pattern (future feature)
verbose bool false Enable detailed logging during conversion

Output Format

The package converts Excel files to JSON with the following structure:

{
  "Sheet1": [
    {
      "Name": "John Doe",
      "Age": 30,
      "Email": "john@example.com"
    },
    {
      "Name": "Jane Smith",
      "Age": 25,
      "Email": "jane@example.com"
    }
  ],
  "Sheet2": [
    {
      "Product": "Widget",
      "Price": 19.99,
      "InStock": true
    }
  ]
}

Each worksheet becomes a top-level property, and each row (except the header) becomes an object in the array.

Supported Cell Types

The package handles all standard Excel cell types:

  • Text - String values
  • Numbers - Integer and floating-point numbers
  • Booleans - TRUE/FALSE values
  • Dates - Date values (converted to strings by default)
  • Times - Time values (converted to strings by default)
  • DateTime - Combined date and time values
  • Formulas - Formula expressions (shows the formula, not the result)
  • Empty cells - Represented as null in JSON

Error Handling

The package provides detailed error information through the ExcelToJsonException class:

try {
  final result = await converter.convert();
} on ExcelToJsonException catch (e) {
  print('Conversion failed: ${e.message}');
  
  // Check if there's an underlying cause
  if (e.cause != null) {
    print('Underlying error: ${e.cause}');
  }
}

Platform Support

This package supports all platforms where Flutter runs:

  • ✅ Android
  • ✅ iOS
  • ✅ Web
  • ✅ Windows
  • ✅ macOS
  • ✅ Linux

Limitations

  • Only supports .xlsx files (Excel 2007+ format)
  • Does not support .xls files (Excel 97-2003 format)
  • Formula results are not calculated (shows formula text instead)
  • Charts, images, and other embedded objects are ignored

Example App

Check out the /example folder for a complete Flutter app demonstrating how to use this package.

Contributing

We welcome contributions! Please see our Contributing Guide for details.

Development Setup
  1. Fork the repository
  2. Clone your fork: git clone https://github.com/yourusername/excel_to_json.git
  3. Install dependencies: flutter pub get
  4. Run tests: flutter test
  5. Make your changes and add tests
  6. Submit a pull request

Changelog

See CHANGELOG.md for a detailed list of changes.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Support

If you find this package helpful, please give it a ⭐ on GitHub and pub.dev!

所屬分類

依賴

依賴清單 2 項

原始碼儲存庫

使用此套件的專案

暫無相關資料。