Code Monkey home page Code Monkey logo

xlsbundle's Introduction

Xls Bundle

Trivia read and write of .xls files for Symfony2, built on top of the PHPExcel

Installation

Via composer:

require: {
    ...
    "arodiss/xls-bundle": "*@stable"
}

Usage examples:

Read a small file

$reader = $container->get("arodiss.xls.reader");
$content = $reader->readAll("/path/to/file.xls");
var_dump($content);
//array(2) =>
//    array(2) =>
//        0 => string(10) "First line"
//        1 => null
//    array(2) =>
//        0 => string(10) "Line number"
//        1 => int 2

Read a big file

Previous method can exhaust your memory if the file is big. However this is safe:

$reader = $container->get("arodiss.xls.reader");
$iterator = $reader->getReadIterator("/path/to/file.xls");
while($iterator->valid())
{
    var_dump($iterator->current());
    $iterator->next();
}
//same output format

Read even a bigger file

Sometime PHPOffice just can't provide reasonable performance. For this case bundle provides alternative reader which wraps python implementation. It is rudimentary in terms of functionality and especially interactions (like error handling), but performs faster, especially on large files. In order to use it, you have to install openpyxl (for xlsx) and xlrd (for xls) libraries, which you can easily do through pip package manager

$reader = $container->get("arodiss.xls.reader.python");
$iterator = $reader->getReadIterator("/path/to/file.xls");
while($iterator->valid())
{
    var_dump($iterator->current());
    $iterator->next();
}
//same output format

Return XLS file from Symfony controller

$file = $container
    ->get("arodiss.xls.builder")
    ->buildXlsFromArray(array(
        array("row one field one", "row one field two"),
        array("row two field one")
    ))
;

//now $file is path to tmp file with data

$response = new Response();
$response->headers->set("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
$response->headers->set("Content-Disposition", "attachment;filename=excelfile.xlsx");
$response->setContent(file_get_contents($file));
return $response;

Write in a file (recommended)

$writer = $container->get("arodiss.xls.writer.buffered");
$writer->create("users.xls", array("name", "email")); //second argument represents first row

foreach ($userProvider->getUsers() as $user) {
    $writer->appendRow("users.xls", array($user->getName(), $user->getEmail()));
}
$writer->flush();

Write in a file (not recommended)

Write operations for xls format are extremely expensive, so previous example uses BufferedWriter which stores your data in buffer and writes in file only once, when flushing the buffer. If for some reason this is not what you want to achieve, you may use service xls.writer in stead of xls.writer.buffered.

Formats supported

Files are always written in Excel2007 (.xlsx) format. Read operations, however, MAY work also for other formats supported by PHPExcel (Excel5, Office Open XML, SpreadsheetML, OASIS, CSV, Gnumeric, SYLK) however there is no guarantee for it.

xlsbundle's People

Contributors

arodiss avatar girayk avatar reggin avatar robholmes avatar wampirusy avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

xlsbundle's Issues

error in Readme

Return XLS file from Symfony controller

$file = $container
->get("arodiss.xls.builder")
->createAndWrite(array(
array("row one field one", "row one field two"),
array("row two field one")
))
;

But there is no method createAndWrite() in XlsBuilder class. There is buildXlsFromArray() method.

PS thanks for you bundle. It simple to use and it works. Perfect for my project.

Could you please release a new version?

This would be great ๐Ÿ‘

Otherwise Symfony 4 is not supported:

  Problem 1
    - Installation request for arodiss/xls-bundle ^0.5.12 -> satisfiable by arodiss/xls-bundle[v0.5.12].
    - arodiss/xls-bundle v0.5.12 requires symfony/framework-bundle ~2.3|~3.0 -> satisfiable by symfony/framework-bundle[2.3.x-dev, 2.4.x-dev, 2.5.x-dev, 2.6.x-dev, 2.7.x-dev, 2.8.x-dev, 3.0.x-dev, 3.1.x-dev, 3.2.x-dev, 3.3.x-dev, 3.4.x-dev, v2.3.0, v2.3.1, v2.3.10, v2.3.11, v2.3.12, v2.3.13, v2.3.14, v2.3.15, v2.3.16, v2.3.17, v2.3.18, v2.3.19, v2.3.2, v2.3.20, v2.3.21, v2.3.22, v2.3.23, v2.3.24, v2.3.25, v2.3.26, v2.3.27, v2.3.28, v2.3.29, v2.3.3, v2.3.30, v2.3.31, v2.3.32, v2.3.33, v2.3.34, v2.3.35, v2.3.36, v2.3.37, v2.3.38, v2.3.39, v2.3.4, v2.3.40, v2.3.41, v2.3.42, v2.3.5, v2.3.6, v2.3.7, v2.3.8, v2.3.9, v2.4.0, v2.4.0-BETA1, v2.4.0-BETA2, v2.4.0-RC1, v2.4.1, v2.4.10, v2.4.2, v2.4.3, v2.4.4, v2.4.5, v2.4.6, v2.4.7, v2.4.8, v2.4.9, v2.5.0, v2.5.0-BETA1, v2.5.0-BETA2, v2.5.0-RC1, v2.5.1, v2.5.10, v2.5.11, v2.5.12, v2.5.2, v2.5.3, v2.5.4, v2.5.5, v2.5.6, v2.5.7, v2.5.8, v2.5.9, v2.6.0, v2.6.0-BETA1, v2.6.0-BETA2, v2.6.1, v2.6.10, v2.6.11, v2.6.12, v2.6.13, v2.6.2, v2.6.3, v2.6.4, v2.6.5, v2.6.6, v2.6.7, v2.6.8, v2.6.9, v2.7.0, v2.7.0-BETA1, v2.7.0-BETA2, v2.7.1, v2.7.10, v2.7.11, v2.7.12, v2.7.13, v2.7.14, v2.7.15, v2.7.16, v2.7.17, v2.7.18, v2.7.19, v2.7.2, v2.7.20, v2.7.21, v2.7.22, v2.7.23, v2.7.24, v2.7.25, v2.7.26, v2.7.27, v2.7.28, v2.7.29, v2.7.3, v2.7.30, v2.7.31, v2.7.32, v2.7.33, v2.7.34, v2.7.35, v2.7.36, v2.7.37, v2.7.38, v2.7.39, v2.7.4, v2.7.40, v2.7.41, v2.7.42, v2.7.43, v2.7.44, v2.7.45, v2.7.46, v2.7.47, v2.7.48, v2.7.49, v2.7.5, v2.7.6, v2.7.7, v2.7.8, v2.7.9, v2.8.0, v2.8.0-BETA1, v2.8.1, v2.8.10, v2.8.11, v2.8.12, v2.8.13, v2.8.14, v2.8.15, v2.8.16, v2.8.17, v2.8.18, v2.8.19, v2.8.2, v2.8.20, v2.8.21, v2.8.22, v2.8.23, v2.8.24, v2.8.25, v2.8.26, v2.8.27, v2.8.28, v2.8.29, v2.8.3, v2.8.30, v2.8.31, v2.8.32, v2.8.33, v2.8.34, v2.8.35, v2.8.36, v2.8.37, v2.8.38, v2.8.39, v2.8.4, v2.8.40, v2.8.41, v2.8.42, v2.8.43, v2.8.44, v2.8.45, v2.8.5, v2.8.6, v2.8.7, v2.8.8, v2.8.9, v3.0.0, v3.0.0-BETA1, v3.0.1, v3.0.2, v3.0.3, v3.0.4, v3.0.5, v3.0.6, v3.0.7, v3.0.8, v3.0.9, v3.1.0, v3.1.0-BETA1, v3.1.0-RC1, v3.1.1, v3.1.10, v3.1.2, v3.1.3, v3.1.4, v3.1.5, v3.1.6, v3.1.7, v3.1.8, v3.1.9, v3.2.0, v3.2.0-BETA1, v3.2.0-RC1, v3.2.0-RC2, v3.2.1, v3.2.10, v3.2.11, v3.2.12, v3.2.13, v3.2.14, v3.2.2, v3.2.3, v3.2.4, v3.2.5, v3.2.6, v3.2.7, v3.2.8, v3.2.9, v3.3.0, v3.3.0-BETA1, v3.3.0-RC1, v3.3.1, v3.3.10, v3.3.11, v3.3.12, v3.3.13, v3.3.14, v3.3.15, v3.3.16, v3.3.17, v3.3.18, v3.3.2, v3.3.3, v3.3.4, v3.3.5, v3.3.6, v3.3.7, v3.3.8, v3.3.9, v3.4.0, v3.4.0-BETA1, v3.4.0-BETA2, v3.4.0-BETA3, v3.4.0-BETA4, v3.4.0-RC1, v3.4.0-RC2, v3.4.1, v3.4.10, v3.4.11, v3.4.12, v3.4.13, v3.4.14, v3.4.15, v3.4.2, v3.4.3, v3.4.4, v3.4.5, v3.4.6, v3.4.7, v3.4.8, v3.4.9] but these conflict with your requirements or minimum-stability.

You have requested a non-existent service "arodiss.xls.reader"

Hello
I thank you for this bundle
I am actually facing an issue i hope you can help me
After the installation i am getting this error

Notice: Undefined variable: container

Here is my code

$em = $this->getDoctrine()->getManager();

$requested = $em->getRepository('UserBundle:User')->byRegister();

$writer = $this->get("arodiss.xls.writer.buffered");
$writer->create("futursVendeurOORUKA.xls", array("Nom d'utilisateur", "Nom","Prenoms","Email","Commune","Adresse","Complement","Telephone")); //second argument represents first row

foreach ($requested as $rq)
{
$writer->appendRow("users.xls", array($rq->getUser(),
$rq->getUser()->getAdresses()->last()->getNom(),
$rq->getUser()->getAdresses()->last()->getPrenoms(),
$rq->getUser()->getEmail(),
$rq->getUser()->getAdresses()->last()->getCommune(),
$rq->getUser()->getAdresses()->last()->getAdresse(),
$rq->getUser()->getAdresses()->last()->getComplement(),
$rq->getUser()->getAdresses()->last()->getTelephone()));
}
$response = new Response();
$response->headers->set("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
$response->headers->set("Content-Disposition", "attachment;filename=excelfile.xlsx");
$response->setContent(file_get_contents($file));
$response->send();

Add support for Symfony 3.0

Currently the composer.json requires "symfony/framework-bundle": "~2.3", it seems that everyone else is adding this |~3.0 to their requirements, resulting in: "symfony/framework-bundle": "~2.3|~3.0"

_endRow default value is "1000"

I need to add validation for values from an excel file, but I get errors for some of the rows because the iterator goes until row 1000 even if in my excel file I have only 15 lines of data.
Is there a way to check if the row is populated? Or to get last row with data?

image

Packagist Update

Hi! Could you update packagist so we can use your latest release?
Thanks!

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.