PHP Classes

Basic Excel: Import and export Excel files to XLS, XLSX and CSV

Recommend this page to a friend!
     
  Info   Example   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2024-01-09 (11 months ago) RSS 2.0 feedStarStarStar 48%Total: 5,224 This week: 2All time: 503 This week: 25Up
Version License PHP version Categories
basic-excel 1.12BSD License5.4PHP 5
Description 

Author

This package can import and export Excel files to XLS, XLSX and CSV file formats.

It provides reader and writer factory classes that can create specific objects of classes depending on the file format that is meant to be imported or exported.

Currently it provides specific classes for reading or writing data to CSV, XLS or XLSX formats.

Picture of Craig Smith
  Performance   Level  
Name: Craig Smith <contact>
Classes: 5 packages by
Country: New Zealand New Zealand
Age: 46
All time rank: 3373 in New Zealand New Zealand
Week rank: 18 Up1 in New Zealand New Zealand Up

Recommendations

Example

<?php

require_once('BasicExcel/Reader.php');
\
BasicExcel\Reader::registerAutoloader();


$data = array(
    array(
'Nr.', 'Name', 'E-Mail'),
    array(
1, 'Jane Smith', 'jane.smith@fakemail.com'),
    array(
2, 'John Smith', 'john.smith@fakemail.com'));

try {
   
$csvwriter = new \BasicExcel\Writer\Csv(); //or \Xls || \Xlsx
   
$csvwriter->fromArray($data);
   
//$csvwriter->writeFile('myfilename.csv');
    //OR
   
$csvwriter->download('myfilename.csv');
} catch (
Exception $e) {
    echo
$e->getMessage();
    exit;
}




Details

BasicExcel

Lightweight Basic Excel Read / Writer for PHP 5.3+

This Class can both read and write to CSV, XLS and XLSX. Added in the abiliity to guess which file type it is and parse accordingly to array

Examples - Reading

1) From a file

   try {
	$xmldata = \BasicExcel\Reader::readFile('/path/to/abc.csv'); //or abc.xls or abc.xlsx
    echo '<pre>' . print_r($xmldata->toArray() , 1) . '</pre>';
   }catch(Exception $e){
	echo $e->getMessage();
	exit;
   }

2) from an upload

   try {
	$xmldata = \BasicExcel\Reader::readUpload($_FILES['upload']);
    echo '<pre>' . print_r($xmldata->toArray() , 1) . '</pre>';
   }catch(Exception $e){
	echo $e->getMessage();
	exit;
   }

3) You can even get the file type if that is all you seek.

   try {
	$type = \BasicExcel\Reader::identify('/path/to/file');
	echo $type;
   }catch(Exception $e){
	echo $e->getMessage();
	exit;
   }

Examples - Writing

1) write a csv / xls / xlsx from an array

$data = array(
    array('Nr.', 'Name', 'E-Mail'),
    array(1, 'Jane Smith', 'jane.smith@fakemail.com'),
    array(2, 'John Smith', 'john.smith@fakemail.com'));
		
   try {
	$csvwriter = new \BasicExcel\Writer\Csv(); //or \Xsl || \Xslx
	$csvwriter->fromArray($data);
	$csvwriter->writeFile('myfilename.csv');
	//OR
	$csvwriter->download('myfilename.csv');
   }catch(Exception $e){
	echo $e->getMessage();
	exit;
   }

2)Usingn XLS or XLSX you can have multiple sheets.


$data = array(
    'Names' => array(
        array('Nr.', 'Name', 'E-Mail'),
        array(1, 'Jane Smith', 'jane.smith@fakemail.com'),
        array(2, 'John Smith', 'john.smith@fakemail.com')
    ),
    'Ages' => array(
        array('Nr.', 'Age'),
        array(1, 103),
        array(2, 21)
    ),
    'Genders' => array(
        array('Nr.', 'Gender'),
        array(1, 'Male'),
        array(2, 'Female')
    )
);
		
   try {
	$csvwriter = new \BasicExcel\Writer\Xls(); //or \Xslx
	$csvwriter->fromArray($data);
	$csvwriter->writeFile('myfilename.csv');
	//OR
	$csvwriter->download('myfilename.csv');
   }catch(Exception $e){
	echo $e->getMessage();
	exit;
   }

Full documentation available soon at http://www.omnihost.co.nz


  Files folder image Files (14)  
File Role Description
Files folder imageBasicExcel (4 files, 2 directories)
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file example.php Example Example script
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file README.md Data Auxiliary data

  Files folder image Files (14)  /  BasicExcel  
File Role Description
Files folder imageReader (3 files)
Files folder imageWriter (3 files)
  Plain text file AbstractReader.php Class Class source
  Plain text file AbstractWriter.php Class Class source
  Plain text file Exception.php Class Class source
  Plain text file Reader.php Class Class source

  Files folder image Files (14)  /  BasicExcel  /  Reader  
File Role Description
  Plain text file Csv.php Class Class source
  Plain text file Xls.php Class Class source
  Plain text file Xlsx.php Class Class source

  Files folder image Files (14)  /  BasicExcel  /  Writer  
File Role Description
  Plain text file Csv.php Class Class source
  Plain text file Xls.php Class Class source
  Plain text file Xlsx.php Class Class source

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 100%
Total:5,224
This week:2
All time:503
This week:25Up
User Ratings User Comments (4)
 All time
Utility:72%StarStarStarStar
Consistency:66%StarStarStarStar
Documentation:-
Examples:59%StarStarStar
Tests:-
Videos:-
Overall:48%StarStarStar
Rank:2956
 
Your Reader::readFile() method is not working.
5 years ago (Jordan Mendes Prestes)
25%StarStar
The example worked which is great (as often examples o not wo...
5 years ago (Rick Ruggiero)
62%StarStarStarStar
Parses xls and xlsx good!
9 years ago (oleg proskurin)
60%StarStarStarStar
not working
11 years ago (synthetron)
0%Star