The script is using user-friendly input format, which is actually XML template. This template can be generated dynamically (like dynamic php web-pages) and then passed to the XLS Generator (or stored for future use) - and XSL Generator will convert this XML template into binary XLS file, which could be opened by any version of MS Excel (from Excel 95). This means that you will be able to convert data from database tables, user input or from any other sources into Excel spreadsheets, creating price-lists, reports, product listings etc.
XLS Generator has many features which will help you to make your generated spreadsheets look individual: font styles, border and color settings, background, foreground and border colors and styles, text orientation and in-cell alignment settings.
Besides, you will be able to tune the printed look of the generated spreadsheet: paper size, page orientation, printed margins, header and footer which can include page numbers and other kind of technical information.
XLS Generator also provides many other features including the possibility to lock certain spreadsheets with a password.
This is very simple example of creation of an .xls file:
<?
// UNCLUDING THE THE MAIN CLASS FILE
require_once("xls_generator/cl_xls_generator.php");
// CREATING XML TEMPLATE
$xml_template = "<?xml version=\"1.0\"?>
<Workbook>
<Styles>
<style name=\"heading\" bold=\"1\" valign=\"middle\" align=\"center\" />
</Styles>
<Worksheet name=\"Simple Demo\">
<Table>
<Row height=\"25\">
<cell width=\"40\" style=\"heading\" >Hello</cell>
<cell width=\"50\" style=\"heading\" >World!</cell>
</Row>
<Row height=\"25\">
<cell width=\"40\" style=\"heading\" >Current date is - </cell>
<cell width=\"50\" num_format=\" d mmmm yyy hh:mm:ss\" >".Unix2Excel(time())."</cell>
</Row>
</Table>
</Worksheet>
</Workbook>
";
// TEMP DIRECTORY PATH - !!! YOU SHOULD HAVE THE WRITING PERMISSIONS TO IT
$temp_dir = "";
// RESULT FILE NAME
$file_name = "xml_test.xls";
// MAIN CLASS CALL
$xls = new xls($xml_template,$file_name,"xls_config.inc",$temp_dir);
// PASSING THE GENERATED FILE TO THE USER
header("Content-Type: application/X-MS-Excel; name=\"$file_name\"");
header("Content-Disposition: attachment; filename=\"$file_name\"");
$fh=fopen($file_name, "rb");
fpassthru($fh);
unlink($file_name);
?>
NOTE!
$temp_dir variable is rather important - you can leave it empty and in this case the file will be created in the system default temp directory - this is not very good, as on Windows systems the temp file may not be deleted, and also - it is always better to be able to track the used space. So, I advise to create some special directory, ensure that your web server (PHP) has permissions to write into it - and assign the full path to this directory to the $temp_dir variable.
<?xml version="1.0"?> at the very beginning.
<Workbook> … </Workbook> element
<Workbook> … </Workbook><worksheet name="First Sheet"> … </worksheet><orientation landscape="1" />
<row><cell> </row></cell><row><cell> … </cell></row>
<!-- some comment -->
#FF0000"red"<', '>' etc. - you can use their HTML codes : '<', '>' etc.
<Styles> section of the Workbook or can be used for within <Cell> elements to define the format of this very cell. Note! If format parameters will be included into <Cell> element - they will discard any of the predefined styles assigned for this cell.
Font_charset - defines the character set for the font
"0" = ANSI Latin
"2" = Symbol
"77" = Apple Roman
"128" = ANSI Japanese Shift-JIS
"129" = ANSI Korean (Hangul)
"130" = ANSI Korean (Johab)
"134" = ANSI Chinese Simplified GBK
"136" = ANSI Chinese Traditional BIG5
"161" = ANSI Greek
"162" = ANSI Turkish
"163" = ANSI Vietnamese
"177" = ANSI Hebrew
"178" = ANSI Arabic
"186" = ANSI Baltic
"204" = ANSI Cyrillic
"222" = ANSI Thai
"238" = ANSI Latin II (Central European)
"255" = OEM Latin I
Size - font size in pt
Bold - "1" = bold; "0" = regular
Italic - "1" = italic; "0" = not italic
Color - should be equal to one of the predefined color names ("red", "navy" etc). The full list of available color names can be found in the 'available_colors.html' file
Underline - "1" = underlined; "0" = not underlined
Strikeout - "1" = stroked out; "0" = not stroked out
Outline - "1" = outlined; "0" = not outlined
Script - "0" = none; "1" = superscript; "2" = subscript
Font_style - to set all the font styles within one parameter:
"b" - boldl;
"i" - italic;
"u" - underlined
"r" - strikeout
"o" - outline"biu" = bold + italic + underline
Align - horizontal alignment of the text
"left" - left laigned
"right" - right aligned
"center" - center aligned
"fill"
"justify"
"merge" - centered across the selection
Valign - vertical alignment of the text
"top"
"bottom"
"middle"
"justify"
text_wrap - "1" - text is wrapped by the cell width
rotation - text is rotated
"0" - Not rotated
"1" - Letters are stacked top-to-bottom, but not rotated
"2" - Text is rotated 90 degrees counterclockwise
"3" - Text is rotated 90 degrees clockwise
fg_color - foreground color name
bg_color - background color name
pattern - background fill pattern index - from "0" to "18"
border - sets all borders for the cell to the given style
"no"
"thin"
"medium"
"dashed"
"dotted"
"thick"
"double"
"hair"
Bottom - bottom border style
Top - top border style
Left - left border style
Right - right border style
border_color - sets colors for all borders
bottom_color - bottom border color
top_color - top border color
left_color - left border color
right_color - right border color
num_format - format template for number inserted into a cell. For more details see "Number formats list" section of this doc. To use time and date formats you should put a certain number into a cell and assign some of the num_formats to the cell. The function which is converting UNIX time stamps into Excel date format is included into the distribution [Unix2Excel($time_stamp)]. Or you can use the predefined rule &NOW which will be substituted by the date and time when the .xls file was generated, but you should not put any additional text to the cell - only the timestamp or the &NOW rule.
<Workbook>
<Styles>
. . .
</Styles>
. . .
</Workbook>
Section <Styles> … </Styles> is a part of <Workbook> element and serves for defining style constructions to be used in the worksheets. This section may contain <Style /> records which should always have name="" attribute to define the style name, besides, this record can contain the number of format parameters to describe the style.
The created styled can then be used to define styles for Worksheets elements: <Table>, <Row> and <Cell>.
<Styles>
<style name="heading" font="Arial" bold="1" size="12"
color="royalblue" bottom="double" border_color="royalblue"
valign="bottom" />
</Styles>
. . .
. . .
<Row>
<cell style="heading">Main Heading Text</cell>
</Row>
<Worksheet name="some name"> . . . </worksheet>
This element is a part of <Workbook> and creates new worksheet in the given workbook with the specified name. Within the workbook you can define whatever number of worksheets that you need.
<Worksheet name="some name">
<Print>
. . .
</Print>
</worksheet>
Print settings are responsible for the printed look of the given worksheet. They can be defined for each sheet separately in the <Print> element of the each sheet. Or can be set for all the sheets within the Workbook - this can be done by modifying the configuration file.
<PaperSize index="0" /> - defines the paper size for printing the given worksheet. Compete list of available paper sizes are given in the "Paper Size Table" section
<Orientation /> - can contain either portrait="1" or landscape="1" - to define page orientation
<Margin /> element - to define the printed margins. Can contain the following attributes:
top="0.50" - top margin (inches)
right="0.50" - right margin (inches)
bottom="0.50" - bottom margin (inches)
left="0.50" - left margin (inches)
<Header>Header text</Header> - defines header text to display on every printed page above the table data. Header text may contain special rules to auto insert additional information like page numbers, or format the header itself. Complete list of the rules can be found in the "Header and Footer special commands" section.
This element can also have margin="0.40" attribute (in inches) to define the distance from the top of the page to the header text.
<Footer>Footer text</ Footer > - defines footer text to display on every printed page below the table data. Footer text may contain special rules to auto insert additional information like page numbers, or format the header itself. Complete list of the rules can be found in the "Header and Footer special commands" section.
This element can also have margin="0.40" attribute (in inches) to define the distance from the bottom of the page to the footer text.
<RowTitle start="1" end="1" /> - defines the Printed Title Row, which will be repeated on every printed page in case the row set will not fit into the printed page (vertically). You can define more than one row. Start - defines the first row, end - the final. The following construction will make the first 3 rows to be repeated on every printed page: <RowTitle start="1" end="3" />
<ColTitle start="1" end="1" /> - defines the Printed Title Column, which will be repeated on every printed page in case the row set will not fit into the printed page (horizontally). You can define more than one column. Start - defines the first column, end - the final. The following construction will make the first 3 columns to be repeated on every printed page: <ColTitle start="1" end="3" />
<Print>
<papersize index="0" />
<orientation landscape="1" />
<margin top="0.50" right="0.50" bottom="0.50" left="0.50" />
<Header margin="0.1">Upper Header [Page &P of &N]</Header>
<Footer>lower footer</Footer>
</Print>
<Worksheet name="some name">
<Password>secret</Password>
. . .
</Worksheet>
<Worksheet name="some name">
. . .
<Table>
. . .
</Table>
</Worksheet>
This element contains the main worksheet table data. Custom style can be assigned to this element and this style will be inherited by all the children elements to which no other styles was assigned.
<Worksheet name="some name">
. . .
<Table>
<Row>
<Cell> . . . </Cell>
<Row>
<Cell> . . . </Cell>
</Table>
</Worksheet>
Index="" - row index - if omitted - the data will be placed to the next row after the previous </Row> (ending tag)
"25" - exact row number
"+1" - plus number of rows. If the previous row was 10 - then this row will be 12
"-2" - minus number of rows. If the previous row number was 10 - then this row will be 9
Height="20" - row height in millimeters
Style="style_name" - the name of the style to be used for the whole row. The appropriate style should be predefined in the <Styles> section.
<Row> element. If not part of any <Row> element - the next row will be used to place cell data. If <Cell> elements are following each other - the column number is incremented automatically for each cell. This element can have the following attributes:
Index="" - cell index, if omitted - the next cell in the row will be used to place data.
"B3" - exact index of the table cell - in this case all the previous row/cell information will be ignored and the data will be put into cell B3
"+1" - plus number of columns. If the previous cell has column #2 (B) - this cell will be placed in the column #4 (D)
"-1" - minus number of columns. If the previous cell has column #3 (C) - this cell will be placed in the column #2 (B)
Width="30" - column width in millimeters, this parameter defines the width for the whole column - so, it is useless to redefine it several times
Style="style_name" - the name of the style to be used for the given cell. The appropriate style should be predefined in the <Styles> section.
<Cell> element may contain any numbers of Format parameters to define the format of the cell without predefining style in the <Styles> section. But this new format will discard any of the predefined styles assigned to this cell.
<Cell> element may contain a hyperlink or a mailto: link. To put one of this links into a cell you should always have either http:// or ftp:// or mailto: in the beginning of the cell text - then the text will be treated as an URL.
<Worksheet name="some name">
. . .
<Table>
<Row height="25">
<Cell>First cell</Cell>
<Cell width="40" style="heading">Second cell</Cell>
<Cell index="+1">First cell</Cell>
<Row>
<Cell index="C4" border="double" font="Arial">direct index</Cell>
</Table>
</Worksheet>
"=" symbol: <cell>= 3 + 7</cell>.
Rules:
"=" symbol
'+', '-', '*', '/' etc into white space:Below is the list of currently supported functions. Please, refer to the MS Excel help documentation concerning the description for functions and usage examples. Only pay attention that to be used in XLS Generator the format of the functions should comply the abovementioned rules.
| ABS | CONCATENATE | DSUM | HYPGEOMDIST | LOOKUP | PEARSON | SEARCH | TIME |
| ACOS | CONFIDENCE | DVAR | IF | LOWER | PERCENTILE | SECOND | TIMEVALUE |
| ACOSH | CORREL | DVARP | INDEX | MATCH | PERCENTRANK | SIGN | TINV |
| ADDRESS | COS | ERROR.TYPE | INDIRECT | MAX | PERMUT | SIN | TODAY |
| AND | COSH | EVEN | INFO | MDETERM | PI | SINH | TRANSPOSE |
| AREAS | COUNT | EXACT | INT | MEDIAN | PMT | SKEW | TREND |
| ASIN | COUNTA | EXP | INTERCEPT | MID | POISSON | SLN | TRIM |
| ASINH | COUNTBLANK | EXPONDIST | IPMT | MIN | POWER | SLOPE | TRIMMEAN |
| ATAN | COUNTIF | FACT | IRR | MINUTE | PPMT | SMALL | TRUE |
| ATAN2 | COVAR | FALSE | ISBLANK | MINVERSE | PROB | SQRT | TRUNC |
| ATANH | CRITBINOM | FDIST | ISERR | MIRR | PRODUCT | STANDARDIZE | TTEST |
| AVEDEV | DATE | FIND | ISERROR | MMULT | PROPER | STDEV | TYPE |
| AVERAGE | DATEVALUE | FINV | ISLOGICAL | MOD | PV | STDEVP | UPPER |
| BETADIST | DAVERAGE | FISHER | ISNA | MODE | QUARTILE | STEYX | VALUE |
| BETAINV | DAY | FISHERINV | ISNONTEXT | MONTH | RADIANS | SUBSTITUTE | VAR |
| BINOMDIST | DAYS360 | FIXED | ISNUMBER | N | RAND | SUBTOTAL | VARP |
| CALL | DB | FLOOR | ISREF | NA | RANK | SUM | VDB |
| CEILING | DCOUNT | FORECAST | ISTEXT | NEGBINOMDIST | RATE | SUMIF | VLOOKUP |
| CELL | DCOUNTA | FREQUENCY | KURT | NORMDIST | REGISTER.ID | SUMPRODUCT | WEEKDAY |
| CHAR | DDB | FTEST | LARGE | NORMINV | REPLACE | SUMSQ | WEIBULL |
| CHIDIST | DEGREES | FV | LEFT | NORMSDIST | REPT | SUMX2MY2 | YEAR |
| CHIINV | DEVSQ | GAMMADIST | LEN | NORMSINV | RIGHT | SUMX2PY2 | ZTEST |
| CHITEST | DGET | GAMMAINV | LINEST | NOT | ROMAN | SUMXMY2 | |
| CHOOSE | DMAX | GAMMALN | LN | NOW | ROUND | SYD | |
| CLEAN | DMIN | GEOMEAN | LOG | NPER | ROUNDDOWN | T | |
| CODE | DOLLAR | GROWTH | LOG10 | NPV | ROUNDUP | TAN | |
| COLUMN | DPRODUCT | HARMEAN | LOGEST | ODD | ROW | TANH | |
| COLUMNS | DSTDEV | HLOOKUP | LOGINV | OFFSET | ROWS | TDIST | |
| COMBIN | DSTDEVP | HOUR | LOGNORMDIST | OR | RSQ | TEXT |
<?xml version="1.0"?>
<Workbook>
<Styles>
<style name="heading" font="Arial" bold="1" size="12"
color="royalblue" bottom="double" border_color="royalblue"
valign="bottom" />
<style name="date" num_format=" d mmmm yyy hh:mm:ss" />
</Styles>
<Worksheet name="Main Sheet">
<Print>
<papersize index="0" />
<orientation landscape="1" />
<margin top="0.50" right="0.50" bottom="0.50" left="0.50" />
<Header margin="0.1">Upper Header [Page &P of &N]</Header>
<Footer>lower footer</Footer>
</Print>
<Password>secret</Password>
<Table>
<Row height="25">
<cell style="heading">XLS Generator</cell>
<cell style="heading"> PHP 2 EXCEL TOOL</cell>
</Row>
<Row>
<cell width="35">Introduction</cell>
<cell width="60">XLS Generator is a PHP script, and so on...</cell>
</Row>
<row>
<cell>Date (in different formats) When this file was generated</cell>
<cell style="date">&NOW</cell>
</row>
<row>
<cell>Mailto links</cell>
<cell color="dodgerblue" underline="1">mailto:paggard@paggard.com</cell>
</row>
<row>
<cell>This page is protected by password:</cell>
<cell border="dashed" align="center">"secret"</cell>
</row>
<Row index="+1">
<cell style="heading">Formulas</cell>
<cell style="heading"> </cell>
</Row>
<Row>
<cell width="35">Basic Operations [3+8*4] = </cell>
<cell width="60">= 3 + 7 * 4</cell>
</Row>
<Row>
<cell>Cell references [B8 + 5] = </cell>
<cell>= B8 + 5</cell>
</Row>
<Row>
<cell>Arythmetic functions [PI()] = </cell>
<cell>= PI()</cell>
</Row>
<Row>
<cell>And other 351 functions [ROUND(SUM(B8:B10),2)] = </cell>
<cell>=ROUND(SUM(B8:B10),2)</cell>
</Row>
</Table>
</Worksheet>
<Worksheet name="Second Sheet">
<Table>
<Row height="30">
<cell>1</cell>
<cell>Here is another sheet here</cell>
<cell>!</cell>
</Row>
</Table>
</Worksheet>
</Workbook>
|
|
|
|
|
|
| Command | Description |
| &L | Start of the left section |
| &C | Start of the centred section |
| &R | Start of the right section |
| &P | Current page number |
| &N | Page count |
| &D | Current date |
| &T | Current time |
| &A | Sheet name (BIFF5-BIFF8) |
| &F | File name without path |
| &Z | File path without file name (BIFF8X) |
| &G | Picture (BIFF8X) |
| &B | Bold on/off (BIFF2-BIFF4) |
| &I | Italic on/off (BIFF2-BIFF4) |
| &U | Underlining on/off |
| &E | Double underlining on/off (BIFF5-BIFF8) |
| &S | Strikeout on/off |
| &X | Superscript on/off (BIFF5-BIFF8) |
| &Y | Subscript on/off (BIFF5-BIFF8) |
| MAY NOT WORK: | |
| &"<fontname>" | Set new font <fontname> |
| &"<fontname>,<fontstyle>" | Set new font with specified style<font style>. The style <fontstyle> is in most cases one of “Regular”, “Bold”, “Italic”, or “Bold Italic”. But this setting is dependent on the used font, it may differ (localised style names, or “Standard”, “Oblique”, ...). |
| &<fontheight> | Set font height in points (<fontheight> is a decimal value). If this command is followed by a plain number to be printed in the header, it will be separated from the font height with a space character. |
If you buy XLS Generator you can use it as you like it and anywhere you will need it. You can do what ever you want with it except selling the script itself. If you're going to include my script into some commercial project for selling, you should include the script price into the budget and every of your customer should together with your software buy the license for my script. It is also possible to buy a distribution license for the script. If you have other plans - we can discuss them.
Vadim Kiparenko paggard@paggard.com© 2004. All Rights Reserved. This class is a commercial software, if you are interested in purchasing it - contact me.