Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
APP_NAME="Laravel Excel To X"
APP_ENV=testing
APP_KEY=
APP_DEBUG=true
APP_URL=http://localhost

LOG_CHANNEL=stack
LOG_LEVEL=debug

DB_CONNECTION=sqlite
DB_DATABASE=:memory:
53 changes: 53 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
name: Run Tests

on:
push:
branches: [main, develop, feature/*]
pull_request:
branches: [main, develop]

jobs:
test:
name: Run Tests
runs-on: ubuntu-latest

strategy:
matrix:
php: [8.2, 8.3]
laravel: [10.*]

steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite
coverage: xdebug

- name: Get composer cache directory
id: composer-cache
run: echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT

- name: Cache dependencies
uses: actions/cache@v3
with:
path: ${{ steps.composer-cache.outputs.dir }}
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.json') }}
restore-keys: ${{ runner.os }}-composer-

- name: Install dependencies
run: |
composer require "laravel/framework:${{ matrix.laravel }}" --no-interaction --no-update
composer update --prefer-dist --no-interaction

- name: Execute tests
run: vendor/bin/phpunit

- name: Upload coverage to Codecov
uses: codecov/codecov-action@v3
with:
file: ./coverage.xml
fail_ci_if_error: true
1 change: 1 addition & 0 deletions .phpunit.result.cache
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"version":1,"defects":{"Tests\\Feature\\ExcelToTest::test_array_conversion":4,"Tests\\Feature\\ExcelToTest::test_collection_conversion":4,"Tests\\Feature\\ExcelToTest::test_handles_merged_cells":3,"Knackline\\ExcelTo\\Tests\\Feature\\ExcelToTest::it_can_convert_excel_to_json":4,"Knackline\\ExcelTo\\Tests\\Feature\\ExcelToJsonTest::it_can_convert_excel_to_json":4,"Tests\\Feature\\ExcelToTest::test_handles_empty_sheets":3,"Tests\\Feature\\ExcelToTest::test_handles_date_formats":4},"times":{"Tests\\Feature\\ExcelToTest::test_json_conversion_single_sheet":0.008,"Tests\\Feature\\ExcelToTest::test_json_conversion_multiple_sheets":0.007,"Tests\\Feature\\ExcelToTest::test_array_conversion":0.013,"Tests\\Feature\\ExcelToTest::test_collection_conversion":0.008,"Tests\\Feature\\ExcelToTest::test_handles_empty_sheets":0.008,"Tests\\Feature\\ExcelToTest::test_handles_merged_cells":0.011,"Tests\\Feature\\ExcelToTest::test_handles_date_formats":0.009,"Tests\\Feature\\ExcelToTest::test_handles_invalid_file":0.005,"Tests\\Feature\\ExcelToTest::test_handles_invalid_file_type":0.017,"Knackline\\ExcelTo\\Tests\\Feature\\ExcelToTest::it_can_convert_excel_to_json":0.011,"Knackline\\ExcelTo\\Tests\\Feature\\ExcelToStreamingFeatureTest::test_stream_processes_large_files_in_chunks":0.01,"Knackline\\ExcelTo\\Tests\\Feature\\ExcelToStreamingFeatureTest::test_stream_handles_custom_chunk_size":0.007,"Knackline\\ExcelTo\\Tests\\Feature\\ExcelToStreamingFeatureTest::test_stream_handles_empty_sheets":0.007,"Knackline\\ExcelTo\\Tests\\Feature\\ExcelToJsonTest::it_can_convert_excel_to_json":0.078}}
141 changes: 105 additions & 36 deletions Readme.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
# Laravel Excel to JSON / Collection / Array
# Laravel Excel To X

This Laravel package provides utilities for converting Excel files to JSON format, Laravel Collections, or PHP Arrays. It also supports reading data from multiple sheets within an Excel file.
A Laravel package for converting Excel files to various formats (JSON, Array, Collection) with support for streaming large files.

## Features

- Convert Excel files to JSON
- Convert Excel files to PHP Arrays
- Convert Excel files to Laravel Collections
- Support for multiple sheets
- Handle merged cells
- Process date formats
- Stream large Excel files efficiently
- Memory-efficient processing of large datasets

## Installation

Expand All @@ -12,62 +23,118 @@ composer require knackline/excel-to-x

## Usage

### JSON Conversion

To convert an Excel file to JSON format, use the `json` method of the `ExcelTo` class:
### Basic Conversion

```php
use Knackline\ExcelTo\ExcelTo;

$jsonData = ExcelTo::json('path/to/your/excel_file.xlsx');
```
// Convert to JSON
$json = ExcelTo::json('path/to/file.xlsx');

// Convert to Array
$array = ExcelTo::array('path/to/file.xlsx');

This will return a JSON-encoded string representing the Excel data. If the Excel file contains multiple sheets, the data will be organized by sheet names.
// Convert to Collection
$collection = ExcelTo::collection('path/to/file.xlsx');
```

### Collection Conversion
### Streaming Large Files

To convert an Excel file to a Laravel Collection, use the `collection` method of the `ExcelTo` class:
For processing large Excel files efficiently:

```php
use Knackline\ExcelTo\ExcelTo;

$collection = ExcelTo::collection('path/to/your/excel_file.xlsx');
// Process in chunks of 1000 rows (default)
$data = ExcelTo::stream('path/to/large_file.xlsx');

// Specify custom chunk size
$data = ExcelTo::stream('path/to/large_file.xlsx', 500);
```

This will return a Laravel Collection containing the Excel data. When multiple sheets are present, each sheet's data will be a collection keyed by the sheet name.
The streaming method returns data in the same format as other conversion methods:

- For single sheet files: Array of rows
- For multiple sheet files: Associative array with sheet names as keys

## Response Format

### Single Sheet

```json
[
{
"First Name": "John",
"Last Name": "Doe",
"Age": "30",
"Date": "2023-01-01"
},
{
"First Name": "Jane",
"Last Name": "Smith",
"Age": "25",
"Date": "2023-01-02"
}
]
```

### Array Conversion
### Multiple Sheets

```json
{
"Sheet1": [
{
"First Name": "John",
"Last Name": "Doe",
"Age": "30",
"Date": "2023-01-01"
}
],
"Sheet2": [
{
"Product": "Laptop",
"Price": "1000",
"Stock": "10"
}
]
}
```

To convert an Excel file to a PHP Array, use the `array` method of the `ExcelTo` class:
## Features in Detail

```php
use Knackline\ExcelTo\ExcelTo;
### Memory Efficiency

$arrayData = ExcelTo::array('path/to/your/excel_file.xlsx');
```
The package uses chunking to process large files efficiently:

This will return a PHP array containing the Excel data. Similar to JSON and Collection, multiple sheets will be keyed by their names.
- Processes data in configurable chunks
- Prevents memory exhaustion with large datasets
- Maintains consistent output format

## Example
### Date Handling

```php
use Knackline\ExcelTo\ExcelTo;
- Automatically detects and formats date values
- Preserves date formats from Excel
- Converts Excel date values to standard formats

// Convert Excel to JSON
$jsonData = ExcelTo::json('path/to/your/excel_file.xlsx');
### Merged Cells

// Convert Excel to Collection
$collection = ExcelTo::collection('path/to/your/excel_file.xlsx');
- Properly handles merged cells in Excel
- Preserves data integrity
- Maintains cell relationships

// Convert Excel to Array
$arrayData = ExcelTo::array('path/to/your/excel_file.xlsx');
```
### Error Handling

## Requirements
- Validates file existence and readability
- Checks for valid Excel file types
- Provides clear error messages

- PHP >= 8.2
- Laravel >= 8.x
- PhpSpreadsheet >= 1.20
## Testing

Run the test suite:

```bash
./vendor/bin/phpunit
```

## Author

Expand All @@ -81,15 +148,17 @@ Contributions are welcome! Feel free to submit pull requests or open an issue if

This package is open-source software licensed under the [MIT license](https://opensource.org/licenses/MIT).


### Key Updates:
1. **Support for Multiple Sheets:**

1. **Support for Multiple Sheets:**

- Described how the package handles multiple sheets, with data organized by sheet names.

2. **Array Conversion:**

- Added a new section for array conversion, including an example of how to use the new `array` method.

3. **Clarified Output Format:**
- Explained the structure of the data returned by each method, emphasizing the handling of single vs. multiple sheets.

Feel free to modify any section further if you have additional details or preferences for the README content.
Feel free to modify any section further if you have additional details or preferences for the README content.
36 changes: 23 additions & 13 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,23 +1,33 @@
{
"name": "knackline/excel-to-x",
"description": "Laravel Package that converts excel to JSON/collection",
"description": "Laravel package for converting Excel files to various formats",
"type": "library",
"require": {
"php": "^8.2",
"phpoffice/phpspreadsheet": "^1.29",
"illuminate/support": "^10.0",
"illuminate/collections": "^10.0",
"illuminate/queue": "^10.0",
"illuminate/bus": "^10.0",
"laravel/framework": "^10.0"
},
"require-dev": {
"phpunit/phpunit": "^9.0"
},
"license": "MIT",
"autoload": {
"psr-4": {
"Knackline\\ExcelTo\\": "src/"
}
},
"authors": [
{
"name": "RAJKUMAR SAMRA",
"email": "rajkumarsamra@gmail.com"
"autoload-dev": {
"psr-4": {
"Knackline\\ExcelTo\\Tests\\": "tests/"
}
],
"require": {
"php": "^8.2",
"phpoffice/phpspreadsheet": "^1.29"
},
"require-dev": {
"phpunit/phpunit": "^9.5"
}
}
"authors": [{
"name": "Rajkumar Samra",
"email": "rajkumar.samra@gmail.com"
}],
"minimum-stability": "stable"
}
25 changes: 25 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd"
bootstrap="vendor/autoload.php"
colors="true"
>
<testsuites>
<testsuite name="Feature">
<directory suffix="Test.php">./tests/Feature</directory>
</testsuite>
</testsuites>
<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">./src</directory>
</include>
<report>
<clover outputFile="coverage.xml"/>
<text outputFile="php://stdout" showUncoveredFiles="true"/>
</report>
</coverage>
<php>
<env name="APP_ENV" value="testing"/>
<env name="APP_DEBUG" value="true"/>
</php>
</phpunit>
Loading
Loading