Ajout de phpexcelreader afin de lire des fichiers excel

This commit is contained in:
Regis Houssin 2007-09-08 15:57:58 +00:00
parent aed5f15f51
commit 54664b9e8a
9 changed files with 1644 additions and 8 deletions

View File

@ -445,7 +445,7 @@ if ($step == 3 && $datatoexport)
print '<table class="noborder" width="100%">';
print '<tr class="liste_titre">';
print '<td>'.$langs->trans("Entities").'</td>';
print '<td>'.$langs->trans("Entities").'</td>';
print '<td>'.$langs->trans("ExportedFields").'</td>';
print '<td align="right" colspan="2">'.$langs->trans("Position").'</td>';
print '<td>&nbsp;</td>';
@ -618,6 +618,13 @@ if ($step == 4 && $datatoexport)
print '</td><td width="50%">&nbsp;</td></tr>';
print '</table>';
// test d'affichage du tableau excel
/*
print '<table width="100%"><tr><td>';
viewExcelFileContent($conf->export->dir_temp.'/1/export_commande_1.xls');
print '</td></tr></table>';
*/
}

View File

@ -148,19 +148,19 @@ class ModeleExports
// Execute requete export
$sql=$this->array_export_sql[0];
$resql = $this->db->query($sql);
if ($resql)
{
$resql = $this->db->query($sql);
if ($resql)
{
// Genere en-tete
$obj->write_header();
// Genere ligne de titre
$obj->write_title();
while ($objp = $this->db->fetch_object($resql))
{
$var=!$var;
$obj->write_record($objp,$array_selected);
while ($objp = $this->db->fetch_object($resql))
{
$var=!$var;
$obj->write_record($objp,$array_selected);
}
// Genere en-tete

View File

@ -0,0 +1,25 @@
INTRODUCTION
Read data from Excel spread sheets without Microsoft!
Provides an API to allow any application to read Excel
documents. Written in PHP. Based on the the Java version by Andy Khan.
LINKS
OpenOffice.org's Documentation
http://sc.openoffice.org/excelfileformat.pdf
OLE2 Storage Documentation
http://jakarta.apache.org/poi/poifs/fileformat.html
Java API for reading, writing and modifying the contents of Excel spreadsheets
http://www.andykhan.com/
CONTACT
Vadim Tkachenko
vt@apachephp.com
INFO
For use encoding you must have installed iconv extension, otherwise data output in unicode
HOW TO USE
see example.php

View File

@ -0,0 +1,12 @@
--------2k--------
BugFix :
added patch by Rberto Innocenti - robyinno to fix infinite loop on 64 bit processors.
fixed order of operations error on date fields. - bizon153
--------2j--------
Features:
added example2.php which demonstrates a more feature rich use
BugFix:
dates were being rolled back by one day

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,271 @@
<?php
define('NUM_BIG_BLOCK_DEPOT_BLOCKS_POS', 0x2c);
define('SMALL_BLOCK_DEPOT_BLOCK_POS', 0x3c);
define('ROOT_START_BLOCK_POS', 0x30);
define('BIG_BLOCK_SIZE', 0x200);
define('SMALL_BLOCK_SIZE', 0x40);
define('EXTENSION_BLOCK_POS', 0x44);
define('NUM_EXTENSION_BLOCK_POS', 0x48);
define('PROPERTY_STORAGE_BLOCK_SIZE', 0x80);
define('BIG_BLOCK_DEPOT_BLOCKS_POS', 0x4c);
define('SMALL_BLOCK_THRESHOLD', 0x1000);
// property storage offsets
define('SIZE_OF_NAME_POS', 0x40);
define('TYPE_POS', 0x42);
define('START_BLOCK_POS', 0x74);
define('SIZE_POS', 0x78);
define('IDENTIFIER_OLE', pack("CCCCCCCC",0xd0,0xcf,0x11,0xe0,0xa1,0xb1,0x1a,0xe1));
//echo 'ROOT_START_BLOCK_POS = '.ROOT_START_BLOCK_POS."\n";
//echo bin2hex($data[ROOT_START_BLOCK_POS])."\n";
//echo "a=";
//echo $data[ROOT_START_BLOCK_POS];
//function log
function GetInt4d($data, $pos)
{
$value = ord($data[$pos]) | (ord($data[$pos+1]) << 8) | (ord($data[$pos+2]) << 16) | (ord($data[$pos+3]) << 24);
if ($value>=4294967294)
{
$value=-2;
}
return $value;
}
class OLERead {
var $data = '';
function OLERead(){
}
function read($sFileName){
// check if file exist and is readable (Darko Miljanovic)
if(!is_readable($sFileName)) {
$this->error = 1;
return false;
}
$this->data = @file_get_contents($sFileName);
if (!$this->data) {
$this->error = 1;
return false;
}
//echo IDENTIFIER_OLE;
//echo 'start';
if (substr($this->data, 0, 8) != IDENTIFIER_OLE) {
$this->error = 1;
return false;
}
$this->numBigBlockDepotBlocks = GetInt4d($this->data, NUM_BIG_BLOCK_DEPOT_BLOCKS_POS);
$this->sbdStartBlock = GetInt4d($this->data, SMALL_BLOCK_DEPOT_BLOCK_POS);
$this->rootStartBlock = GetInt4d($this->data, ROOT_START_BLOCK_POS);
$this->extensionBlock = GetInt4d($this->data, EXTENSION_BLOCK_POS);
$this->numExtensionBlocks = GetInt4d($this->data, NUM_EXTENSION_BLOCK_POS);
/*
echo $this->numBigBlockDepotBlocks." ";
echo $this->sbdStartBlock." ";
echo $this->rootStartBlock." ";
echo $this->extensionBlock." ";
echo $this->numExtensionBlocks." ";
*/
//echo "sbdStartBlock = $this->sbdStartBlock\n";
$bigBlockDepotBlocks = array();
$pos = BIG_BLOCK_DEPOT_BLOCKS_POS;
// echo "pos = $pos";
$bbdBlocks = $this->numBigBlockDepotBlocks;
if ($this->numExtensionBlocks != 0) {
$bbdBlocks = (BIG_BLOCK_SIZE - BIG_BLOCK_DEPOT_BLOCKS_POS)/4;
}
for ($i = 0; $i < $bbdBlocks; $i++) {
$bigBlockDepotBlocks[$i] = GetInt4d($this->data, $pos);
$pos += 4;
}
for ($j = 0; $j < $this->numExtensionBlocks; $j++) {
$pos = ($this->extensionBlock + 1) * BIG_BLOCK_SIZE;
$blocksToRead = min($this->numBigBlockDepotBlocks - $bbdBlocks, BIG_BLOCK_SIZE / 4 - 1);
for ($i = $bbdBlocks; $i < $bbdBlocks + $blocksToRead; $i++) {
$bigBlockDepotBlocks[$i] = GetInt4d($this->data, $pos);
$pos += 4;
}
$bbdBlocks += $blocksToRead;
if ($bbdBlocks < $this->numBigBlockDepotBlocks) {
$this->extensionBlock = GetInt4d($this->data, $pos);
}
}
// var_dump($bigBlockDepotBlocks);
// readBigBlockDepot
$pos = 0;
$index = 0;
$this->bigBlockChain = array();
for ($i = 0; $i < $this->numBigBlockDepotBlocks; $i++) {
$pos = ($bigBlockDepotBlocks[$i] + 1) * BIG_BLOCK_SIZE;
//echo "pos = $pos";
for ($j = 0 ; $j < BIG_BLOCK_SIZE / 4; $j++) {
$this->bigBlockChain[$index] = GetInt4d($this->data, $pos);
$pos += 4 ;
$index++;
}
}
//var_dump($this->bigBlockChain);
//echo '=====2';
// readSmallBlockDepot();
$pos = 0;
$index = 0;
$sbdBlock = $this->sbdStartBlock;
$this->smallBlockChain = array();
while ($sbdBlock != -2) {
$pos = ($sbdBlock + 1) * BIG_BLOCK_SIZE;
for ($j = 0; $j < BIG_BLOCK_SIZE / 4; $j++) {
$this->smallBlockChain[$index] = GetInt4d($this->data, $pos);
$pos += 4;
$index++;
}
$sbdBlock = $this->bigBlockChain[$sbdBlock];
}
// readData(rootStartBlock)
$block = $this->rootStartBlock;
$pos = 0;
$this->entry = $this->__readData($block);
/*
while ($block != -2) {
$pos = ($block + 1) * BIG_BLOCK_SIZE;
$this->entry = $this->entry.substr($this->data, $pos, BIG_BLOCK_SIZE);
$block = $this->bigBlockChain[$block];
}
*/
//echo '==='.$this->entry."===";
$this->__readPropertySets();
}
function __readData($bl) {
$block = $bl;
$pos = 0;
$data = '';
while ($block != -2) {
$pos = ($block + 1) * BIG_BLOCK_SIZE;
$data = $data.substr($this->data, $pos, BIG_BLOCK_SIZE);
//echo "pos = $pos data=$data\n";
$block = $this->bigBlockChain[$block];
}
return $data;
}
function __readPropertySets(){
$offset = 0;
//var_dump($this->entry);
while ($offset < strlen($this->entry)) {
$d = substr($this->entry, $offset, PROPERTY_STORAGE_BLOCK_SIZE);
$nameSize = ord($d[SIZE_OF_NAME_POS]) | (ord($d[SIZE_OF_NAME_POS+1]) << 8);
$type = ord($d[TYPE_POS]);
//$maxBlock = strlen($d) / BIG_BLOCK_SIZE - 1;
$startBlock = GetInt4d($d, START_BLOCK_POS);
$size = GetInt4d($d, SIZE_POS);
$name = '';
for ($i = 0; $i < $nameSize ; $i++) {
$name .= $d[$i];
}
$name = str_replace("\x00", "", $name);
$this->props[] = array (
'name' => $name,
'type' => $type,
'startBlock' => $startBlock,
'size' => $size);
if (($name == "Workbook") || ($name == "Book")) {
$this->wrkbook = count($this->props) - 1;
}
if ($name == "Root Entry") {
$this->rootentry = count($this->props) - 1;
}
//echo "name ==$name=\n";
$offset += PROPERTY_STORAGE_BLOCK_SIZE;
}
}
function getWorkBook(){
if ($this->props[$this->wrkbook]['size'] < SMALL_BLOCK_THRESHOLD){
// getSmallBlockStream(PropertyStorage ps)
$rootdata = $this->__readData($this->props[$this->rootentry]['startBlock']);
$streamData = '';
$block = $this->props[$this->wrkbook]['startBlock'];
//$count = 0;
$pos = 0;
while ($block != -2) {
$pos = $block * SMALL_BLOCK_SIZE;
$streamData .= substr($rootdata, $pos, SMALL_BLOCK_SIZE);
$block = $this->smallBlockChain[$block];
}
return $streamData;
}else{
$numBlocks = $this->props[$this->wrkbook]['size'] / BIG_BLOCK_SIZE;
if ($this->props[$this->wrkbook]['size'] % BIG_BLOCK_SIZE != 0) {
$numBlocks++;
}
if ($numBlocks == 0) return '';
//echo "numBlocks = $numBlocks\n";
//byte[] streamData = new byte[numBlocks * BIG_BLOCK_SIZE];
//print_r($this->wrkbook);
$streamData = '';
$block = $this->props[$this->wrkbook]['startBlock'];
//$count = 0;
$pos = 0;
//echo "block = $block";
while ($block != -2) {
$pos = ($block + 1) * BIG_BLOCK_SIZE;
$streamData .= substr($this->data, $pos, BIG_BLOCK_SIZE);
$block = $this->bigBlockChain[$block];
}
//echo 'stream'.$streamData;
return $streamData;
}
}
}
?>

View File

@ -3232,4 +3232,154 @@ function ConvertSecondToTime($iSecond,$format='all'){
return $sTime;
}
/*
*
*/
function make_alpha_from_numbers($number)
{
$numeric = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
if($number<strlen($numeric))
{
return $numeric[$number];
}
else
{
$dev_by = floor($number/strlen($numeric));
return "" . make_alpha_from_numbers($dev_by-1) . make_alpha_from_numbers($number-($dev_by*strlen($numeric)));
}
}
/*
* \brief Affiche le contenu d'un fichier Excel (avec les feuilles de calcul) sous forme de tableau
* \param file_to_include Fichier Excel à afficher
* \param max_rows Nombre max de lignes à afficher (0 = illimité)
* \param max_cols Nombre max de colonnes à afficher (0 = illimité)
*/
function viewExcelFileContent($file_to_include='',$max_rows=0,$max_cols=0)
{
$debug = 0; //1 for on 0 for off
$force_nobr = 0; //Force the info in cells not to wrap unless stated explicitly (newline)
require_once(DOL_DOCUMENT_ROOT.'/includes/phpexcelreader/excelreader.php');
$data = new Spreadsheet_Excel_Reader();
$data->setOutputEncoding('CPa25a');
$data->read($file_to_include);
error_reporting(E_ALL ^ E_NOTICE);
echo "<script language='Javascript'>
var sheet_HTML = Array();\n";
for($sheet=0;$sheet<count($data->sheets);$sheet++)
{
$table_output[$sheet] .= "<TABLE CLASS='table_body'>
<TR>
<TD>&nbsp;</TD>";
for($i=0;$i<$data->sheets[$sheet]['numCols']&&($i<=$max_cols||$max_cols==0);$i++)
{
$table_output[$sheet] .= "<TD CLASS='table_sub_heading' ALIGN=CENTER>" . make_alpha_from_numbers($i) . "</TD>";
}
for($row=1;$row<=$data->sheets[$sheet]['numRows']&&($row<=$max_rows||$max_rows==0);$row++)
{
$table_output[$sheet] .= "<TR><TD CLASS='table_sub_heading'>" . $row . "</TD>";
for($col=1;$col<=$data->sheets[$sheet]['numCols']&&($col<=$max_cols||$max_cols==0);$col++)
{
if($data->sheets[$sheet]['cellsInfo'][$row][$col]['colspan'] >=1 && $data->sheets[$sheet]['cellsInfo'][$row][$col]['rowspan'] >=1)
{
$this_cell_colspan = " COLSPAN=" . $data->sheets[$sheet]['cellsInfo'][$row][$col]['colspan'];
$this_cell_rowspan = " ROWSPAN=" . $data->sheets[$sheet]['cellsInfo'][$row][$col]['rowspan'];
for($i=1;$i<$data->sheets[$sheet]['cellsInfo'][$row][$col]['colspan'];$i++)
{
$data->sheets[$sheet]['cellsInfo'][$row][$col+$i]['dontprint']=1;
}
for($i=1;$i<$data->sheets[$sheet]['cellsInfo'][$row][$col]['rowspan'];$i++)
{
for($j=0;$j<$data->sheets[$sheet]['cellsInfo'][$row][$col]['colspan'];$j++)
{
$data->sheets[$sheet]['cellsInfo'][$row+$i][$col+$j]['dontprint']=1;
}
}
}
else if($data->sheets[$sheet]['cellsInfo'][$row][$col]['colspan'] >=1)
{
$this_cell_colspan = " COLSPAN=" . $data->sheets[$sheet]['cellsInfo'][$row][$col]['colspan'];
$this_cell_rowspan = "";
for($i=1;$i<$data->sheets[$sheet]['cellsInfo'][$row][$col]['colspan'];$i++)
{
$data->sheets[$sheet]['cellsInfo'][$row][$col+$i]['dontprint']=1;
}
}
else if($data->sheets[$sheet]['cellsInfo'][$row][$col]['rowspan'] >=1)
{
$this_cell_colspan = "";
$this_cell_rowspan = " ROWSPAN=" . $data->sheets[$sheet]['cellsInfo'][$row][$col]['rowspan'];
for($i=1;$i<$data->sheets[$sheet]['cellsInfo'][$row][$col]['rowspan'];$i++)
{
$data->sheets[$sheet]['cellsInfo'][$row+$i][$col]['dontprint']=1;
}
}
else
{
$this_cell_colspan = "";
$this_cell_rowspan = "";
}
if(!($data->sheets[$sheet]['cellsInfo'][$row][$col]['dontprint']))
{
$table_output[$sheet] .= "<TD CLASS='table_data' $this_cell_colspan $this_cell_rowspan>&nbsp;";
if($force_nobr)
{
$table_output[$sheet] .= "<NOBR>";
}
$table_output[$sheet] .= nl2br(htmlentities($data->sheets[$sheet]['cells'][$row][$col]));
if($force_nobr)
{
$table_output[$sheet] .= "</NOBR>";
}
$table_output[$sheet] .= "</TD>";
}
}
$table_output[$sheet] .= "</TR>";
}
$table_output[$sheet] .= "</TABLE>";
$table_output[$sheet] = str_replace("\n","",$table_output[$sheet]);
$table_output[$sheet] = str_replace("\r","",$table_output[$sheet]);
$table_output[$sheet] = str_replace("\t"," ",$table_output[$sheet]);
if($debug)
{
$debug_output = print_r($data->sheets[$sheet],true);
$debug_output = str_replace("\n","\\n",$debug_output);
$debug_output = str_replace("\r","\\r",$debug_output);
$table_output[$sheet] .= "<PRE>$debug_output</PRE>";
}
echo "sheet_HTML[$sheet] = \"$table_output[$sheet]\";\n";
}
echo "
function change_tabs(sheet)
{
//alert('sheet_tab_' + sheet);
for(i=0;i<", count($data->sheets) , ";i++)
{
document.getElementById('sheet_tab_' + i).className = 'tab_base';
}
document.getElementById('table_loader_div').innerHTML=sheet_HTML[sheet];
document.getElementById('sheet_tab_' + sheet).className = 'tab_loaded';
}
</SCRIPT>";
echo "
<TABLE CLASS='table_body' NAME='tab_table'>
<TR>";
for($sheet=0;$sheet<count($data->sheets);$sheet++)
{
echo "<TD CLASS='tab_base' ID='sheet_tab_$sheet' ALIGN=CENTER
ONMOUSEDOWN=\"change_tabs($sheet);\">", $data->boundsheets[$sheet]['name'] , "</TD>";
}
echo
"<TR>";
echo "</TABLE>
<DIV ID=table_loader_div></DIV>
<SCRIPT LANGUAGE='JavaScript'>
change_tabs(0);
</SCRIPT>";
}
?>

View File

@ -1256,3 +1256,48 @@ form.inplaceeditor-form a { /* The cancel link */
background-position : bottom;
cursor:pointer;
}
/* ============================================================================== */
/* PHP_ExcelReader */
/* ============================================================================== */
.table_data
{
border-style:ridge;
border-width:1;
}
.tab_base
{
background:#C5D0DD;
font-weight:bold;
border-style:ridge;
border-width:1;
cursor:pointer;
}
.table_sub_heading
{
background:#CCCCCC;
font-weight:bold;
border-style:ridge;
border-width:1;
}
.table_body
{
background:#F0F0F0;
font-wieght:normal;
font-size:12;
font-family:sans-serif;
border-style:ridge;
border-width:1;
border-spacing: 0px;
border-collapse: collapse;
}
.tab_loaded
{
background:#222222;
color:white;
font-weight:bold;
border-style:groove;
border-width:1;
cursor:pointer;
}

View File

@ -1390,3 +1390,48 @@ div.menuFleche
position:relative;
}
/* ============================================================================== */
/* PHP_ExcelReader */
/* ============================================================================== */
.table_data
{
border-style:ridge;
border-width:1;
}
.tab_base
{
background:#C5D0DD;
font-weight:bold;
border-style:ridge;
border-width:1;
cursor:pointer;
}
.table_sub_heading
{
background:#CCCCCC;
font-weight:bold;
border-style:ridge;
border-width:1;
}
.table_body
{
background:#F0F0F0;
font-wieght:normal;
font-size:12;
font-family:sans-serif;
border-style:ridge;
border-width:1;
border-spacing: 0px;
border-collapse: collapse;
}
.tab_loaded
{
background:#222222;
color:white;
font-weight:bold;
border-style:groove;
border-width:1;
cursor:pointer;
}