Coder Social home page Coder Social logo

lib-excel-wrapper's Introduction

Excel wrapper

A wrapper for a streaming excel-writer.

Contacts: Richard Klees

Writing files

An excel-write has to implement a kind of functionality as defined.

  • Create a writer object
  • Define style for single column at any time
  • Fill the sheet row by row
  • Create new sheets and switch by name
  • Save to given folder and filename

To fill the sheet row by row is a recommended feature to increase the speed on mass export of data. It will not be possible to step back to rows or single columns to change values or something else.

public function addRow(array $values) {
    $this->writer->addRow($values);
}

For designing the sheet, it is possible to give any column at any time a new style. E.g. in row 1 to 5 the colum A is bold and left orientated. Starting with row 6 it will only left orientated and not longer bold. This gives the opportunity to design column headers or highlight special values.

public function setColumnStyle(Style $style, $column) {
    $this->writer->setColumnStyle($column, $style);
};

It is possible to create new sheets and switch them by name. If you creating a new sheet, it will be automaticly the current.

public function createSheet($name) {
    $this->writer->createSheet($name);
}

Public function selectSheet($name) {
    $this->writer->setCurrentSheet($name);
}

Define Styles

A column can be styled in a lot of ways. This wrapper includes the mostly used.

  • Font family
  • Font size
  • Bold
  • Italic
  • Underline
  • Text color
  • Background color
  • Orientation
  • Border
  • Border color
public function getNewStyle() {
    $style = new Style();
    $style ->setFontFamily('Aarial')
           ->setFontSize(12)
           ->setItalic(false)
           ->setBorder(Style::TOP);

    return $style;
}

Example for use

This is a small example for using this wrapper.

public function exportData($file_name, $file_path, array $header, array $values) {
    $writer = new Writer();
    $writer->setFileName($file_name);
    $writer->setPath($file_path);

    $header_style = $this->getHeaderStyle();
    $writer->setColumnStyle('A', $header_style);
    $write->addRow($header);

    $bold_style = $this->getBoldStyle();
    $basic_style = $this->getBasicStyle();

    $writer->setColumnStyle('A', $bold_style);
    $writer->setColumnStyle('B', $basic_style);
    foreach($values as $value) {
        $write->addRow($value);
    }

    $writer->setColumnStyle('A', $basic_style);
    $writer->setColumnStyle('B', $bold_style);
    foreach($values as $value) {
        $write->addRow($value);
    }

    $writer->saveFile();
    $write->close();
}

protected function getHeaderStyle() {
    $style = new Style()
    $style->setBold(true)
          ->setItalic(true);

    return $style;
}

protected function getBoldStyle() {
    $style = new Style()
    $style->setBold(true)

    return $style;
}

protected function getBsicStyle() {
    return new Style();
}

The xlsx sheet result might be look like this.

R\C A B
1 Content for header
2 Content in the first column Content in the second column
3 Content in the first column Content in the second column
4 Content in the first column Content in the second column
5 Content in the first column Content in the second column

lib-excel-wrapper's People

Contributors

shecken avatar daniwe4 avatar klees avatar

Watchers

James Cloos avatar  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.