Giter Club home page Giter Club logo

elxml's Introduction

elxml

A minimalistic Excel OOXML writer.

NPM Version License

The main purpose is to create simple Excel files via JavaScript. The current implementation supports

  1. Multiple sheets
  2. Creation of rows and cells
  3. Column width definition
  4. PatternFills for cells
  5. Borders for cells
  6. Number formats for cells
  7. Fonts for cells
  8. Merge cells
  9. AutoFilter

elxml allows to use string tables to save memory.

Uses xmlbuilder-js, archiver and underscore

Usage

Create a workbook:

var excel = require("elxml.js");
var wb = excel.createWorkbook();

Create a sheet within the workbook:

var sheet = wb.addSheet("mySheet");

Add a row with a single cell and set a value for the cell

var row = sheet.addRow(1);
var cell = row.addCell("A","d");  // the value is a date in ISO standard notation
cell.setValue("2014-02-02");

Last step is to save the workbook:

wb.save("test.01.xlsx", function(err) {
    if (!err) {
        console.log("File saved!");
    } else {
        console.log(err);
    }
});

Advanced usage

Number formats

The above example creates an Excel file with a date cell but without a proper number format.

To apply the number format to the cell we have to create a cell style. Cell styles are based on a default cell style which defines the number format, fill, borders and the font for those cells which don't use any cell style. In elxml the default is: Calibri with a size of 11, no borders, no fill, no number format.

Lets set a date number format to see a better formatted date in Excel:

// create a date format
var dateFrmt = wb.addNumberFormat("dd/mm/yy;@");

// get the predefined default style
var defStyle = wb.getStyle("Standard");

// derive a new style from the default style
var dateStyle = wb.addStyle(defStyle, {numFrmt: dateFrmt});

// apply the style
cell.setStyle(dateStyle);

Another Example

// create a date format
var dateFrmt = wb.addNumberFormat("DD/MM/YYYY\\ HH:MM:SS");

// create a default style
var defStyle = wb.createStyle("Standard");
// derive a new style from the default style
var dateStyle = wb.addStyle(defStyle, {numFrmt: dateFrmt});

// format value to MSDATE format
var value = moment( "2014-06-17 08:55:49" ).toOADate();

// set cell type
var cell = row.addCell( "A" , excel.CELL_TYPE_MSDATE );

// apply the style
cell.setStyle(dateStyle);

// write data to cell
cell.setValue( value );

To see which number formats are available take a look at the OOXML spec.

Strings

By default strings are saved as inline strings.

// add a cell with a string value
var cell1 = row.addCell("D");      // excel.CELL_TYPE_STRING is default
cell1.setValue("Hello World!");

var cell2 = row.addCell("D",excel.CELL_TYPE_STRING);    // set type
cell2.setValue("Hello World!");

In case you have tables with many similar string values you can enable the "shared string" function. Note that this must be done for every cell as inline strings are the default.

// add a cell with a string value
var cell = row.addCell("D",excel.CELL_TYPE_STRING_TAB);
cell.setValue("Hello World!");

This will save memory while saving and speed up loading of the created Excel file.

Fills

You can define pattern fills. Possible options for a pattern fill are: fgColor, bgColor and type. Lets create a red solid fill:

// create a color (RGBA)
var red = wb.color(255,0,0,0);
// create a pattern fill
var redFill = wb.addPatternFill({fgColor:red, type:excel.PATTERN_TYPE_SOLID});
// create a cell style with the red fill
var redFillStyle = wb.addStyle(defStyle, {fill: redFill});
// apply the style
cell.setStyle(dateStyle);

Borders

You can define the borders for a cell. First you have to create a border representation object which is used to define the border style.

// create a color (RGBA)
var black = wb.color(0,0,0,0);
// create a thick border presentation with a black color
var thickBorderPr = wb.createBorderPr(excel.BORDER_STYLE_THICK, black);
// create a border type, bottom line is set to thickBorderPr
var border = wb.addBorder({bottom:thickBorderPr});
// create a cell style with the border
var borderStyle = wb.addStyle(defStyle, {border: border});
// apply the style
cell.setStyle(borderStyle);

Fonts

You can create new fonts which are derived from the default font. You can set the size and whether the font is bold or not (default).

// create a bold font, the font is derived from the default font
var boldFont = wb.addFont({bold: true});
// create a cell style with the bold font
var boldFontStyle = wb.addStyle(defStyle, {font: boldFont});
// apply the style
cell.setStyle(boldFontStyle);

Column width

Define the width for one or more columns:

// set the width of the first column to 30
sheet.setColumn(1,1,30);
// set the width of columns 2 - 5 to 50
sheet.setColumn(2,5,50);

Merge cells

// merge cells horizontal
sheet.mergeCell("A2:D2");
// merge cells vertical
sheet.mergeCell("A3:A6");

AutoFilter

// use a range which contains data! 
sheet.setAutoFilter("A1:D1");

Change history

0.1.3 - add callback to Workbook.save method
0.1.4 - fixed issues #8, #9
0.1.5 - add support for italics, text rotation and text wrapping
0.1.6 - a 'Standard' style is created by default, access it via the getStyle function
0.2.0 - add Workbook.saveToStream to save the resulting ZIP directly to a file stream
0.2.1 - fixed issue with wrong style IDs
0.3.0 - add basic support for AutoFilter, fixed error in string table
0.3.1 - add Row.setStyleForAllCells convenience method

elxml's People

Contributors

alevkon avatar chevett avatar cowabunga1 avatar dunkelrot avatar grin0c avatar kamikat avatar khamasaki avatar lordfriend avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

elxml's Issues

Formula cells don't save correctly

When setting a cell type to "CELL_TYPE_FORMULAR" and the value to a formula string e.g. =SUM(B2:H2) the resulting spreadsheet loads the formula as if it was a string. The cell displays the formula as opposed to actually doing the calculation and displaying the result.

Big Data - process out of memory

FATAL ERROR: CALL_AND_RETRY_2 Allocation failed - process out of memory

on a large amount of rows and columns
more than > 100.000 rows
and > 5 columns

Small error on pattern constants

Small error on pattern constants :
/** @constant PATTERN_TYPE_DARK_DOWN /
exports.PATTERN_TYPE_DARK_DOWN = "darkDown";
/
* @constant PATTERN_TYPE_DARK_DOWN /
exports.PATTERN_TYPE_DARK_DOWN = "darkUp";
Should be :
/
* @constant PATTERN_TYPE_DARK_UP */
exports.PATTERN_TYPE_DARK_UP = "darkUp";

patternFill => bgColor seems useless to set colors.

Hello,
I was looking for a way to fix the color of fonts (by setting cells of course)
Unfortunately, patternFill => bgColor seems useless to set the color.
Sorry if it is not an issue and Thanks in advance for bug fixing

Excel error if no styles are defined

If you don't define any styles, Excel (2010) throws an "error in styles.xml".

Sample code:

var excel = require("elxml");

var wb = excel.createWorkbook();
// wb.createStyle("Standard");
var sheet = wb.addSheet('test sheet');
var row = sheet.addRow(1);
row.addCell('A').setValue('test value');
wb.save('test-file.xlsx', function() {});

if you include the line wb.createStyle("Standard");, the error goes away.

These are the lines that change in styles.xml:

styles.xml without styles (that throws the error):

<cellStyleXfs count="0"/>
<cellXfs count="0"/>
<cellStyles count="0"/>

styles.xml with styles:

<cellStyleXfs count="1">
    <xf numFmtId="0" fontId="0" fillId="0" borderId="0"/>
</cellStyleXfs>
<cellXfs count="1">
    <xf numFmtId="0" fontId="0" fillId="0" borderId="0" xfId="0"/>
</cellXfs>
<cellStyles count="1">
    <cellStyle name="Standard" xfId="0" builtinId="0"/>
</cellStyles>

Maybe these tags must not be empty or self closing?

btw. awesome module. Thanks for sharing! :-)

License

Hi. This is a great library! Have you decided what the licensing for it is going to be? MIT, hopefully :) ?

Can't resolve 'fs'

I'm currently getting Can't resolve 'fs' errors in several modules:

Module not found: Error: Can't resolve 'fs' in '\node_modules\archiver\lib'
Module not found: Error: Can't resolve 'fs' in \node_modules\elxml'
Module not found: Error: Can't resolve 'fs' in '\node_modules\fs.realpath'
Module not found: Error: Can't resolve 'fs' in '\node_modules\glob'
Module not found: Error: Can't resolve 'fs' in '\node_modules\graceful-fs'

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.