Giter VIP home page Giter VIP logo

exceldatatables's Introduction

ExcelDataTables

Replace a worksheet within an Excel workbook (.xlsx) without changing any other properties of the file.

License

This program is free software; see LICENSE for more details.

Details

The main purpose of this library is adding a "data table" to an existing excel file without modifing any other components. This is especially useful if you have a template file, e.g. for reporting including "advanced" features like charts, pivot tables, macros and you'd like to change a base data table within a PHP application.

Setup

Use composer to add this repository to your dependencies:

{
	"require": {
		"svrnm/exceldatatables": "dev-master"
	}
}

If you use the laravel framework you can additionally add the ServiceProvider to your app/config.php:

...
'providers' => array(
	...
	'Svrnm\ExcelDataTables\ExcelDataTablesServiceProvider'
)
...

Examples

The following example demonstrates how to use ExcelDataTables. The following is also contained as example.php within the folder "examples/":

<?php
        require_once('../vendor/autoload.php');
		// Create a new instance 
        $dataTable = new Svrnm\ExcelDataTables\ExcelDataTable();
		// Specify the source file
        $in = 'spec.xlsx';
		// Specify the output file
        $out = 'test.xlsx';
		// Specify the data for the worksheet. 
        $data = array(
                        array("Date" => new \DateTime('2014-01-01 13:00:00'), "Value 1" => 0, "Value 2" => 1),
                        array("Date" => new \DateTime('2014-01-02 14:00:00'), "Value 1" => 1, "Value 2" => 0),
                        array("Date" => new \DateTime('2014-01-03 15:00:00'), "Value 1" => 2, "Value 2" => -1),
                        array("Date" => new \DateTime('2014-01-04 16:00:00'), "Value 1" => 3, "Value 2" => -2),
                        array("Date" => new \DateTime('2014-01-05 17:00:00'), "Value 1" => 4, "Value 2" => -3),
        );
		// Attach the data table and copy the new xlsx file to the output file.
        $dataTable->showHeaders()->addRows($data)->attachToFile($in, $out);
?>

In this example the method "attachToFile" creates a new excel file. If you use this library within a web application you might prefer the "fillXLSX()" function which returns a string representation of the excel document. The following examples demonstrates this case within a laravel application

<?php
	class ReportController extends Controller {
		protected $dataTable;

	    public function __construct(\Svrnm\ExcelDataTables\ExcelDataTable $dataTable) {
	        $this->dataTable = $dataTable;
		}

		public function show($month) {
			$data = DB::select('select date,value1,value2 from reporting where MONTH(date) = ?', array($month));
			$path = storage_path() . '/reports/example.xlsx';
			$xlsx = $this->dataTable->showHeaders()->addRows($data)->fillXLSX($path);
			return Response::make($xlsx, 200, array(
				'Content-Type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
				'Content-Disposition' => 'attachment; filename="report.xlsx"',
				'Content-Length' => strlen($xlsx)
			));
			// ...
		}
	}
?>

Contact

For any questions you can contact Severin Neumann [email protected]

exceldatatables's People

Contributors

svrnm avatar adael avatar deinonychuscowboy avatar lucentlogix avatar

Watchers

James Cloos avatar xingxiangwei avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.