1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
| <?php
class MakeCsv{
var $fileName;
var $csv = array();
var $maxY = 0;
var $maxX = 0;
function MakeCsv($fileName){
$this->fileName = $fileName;
}
function addCol($array,$l){
$this->maxX++;
$this->csv[$l-1] = $array;
}
function addLine($array,$c){
$this->maxY++;
$this->maxX = count($array);
for($i=0;$i<count($array);$i++){
$this->csv[$i][$c-1] = $array[$i];
}
}
function setValueAt($x,$y,$v){
$this->csv[$x-1][$y-1] = $v;
}
function createCsv(){
$fp = @fopen($this->fileName,"w") or
die('<br /><b>Fatal error</b>: Can\'t open <b>'.$this->fileName.'</b> in <b>'.__FILE__.'</b> on line <b>'.(__LINE__-1).'</b><br />');
for($y=0;$y<$this->maxY();$y++){
for($x=0;$x<$this->maxX();$x++){
if(!empty($this->csv[$x][$y]))
fwrite($fp,$this->csv[$x][$y],strlen($this->csv[$x][$y]));
fwrite($fp,';',1);
}
fwrite($fp,"\r\n",2);
}
fclose($fp);
}
function downloadCsv(){
header("Content-disposition: attachment; filename=\"$this->fileName\"");
header("Content-Type: application/force-download");
header("Content-Transfer-Encoding: binary");
for($y=0;$y<$this->maxY();$y++){
for($x=0;$x<$this->maxX();$x++){
if(!empty($this->csv[$x][$y]))
echo '"'.$this->csv[$x][$y].'"';
echo ';';
}
echo "\r\n";
}
exit();
}
function maxX(){
$max = 0;
while(list($k,) = each($this->csv)){
if($k>$max) $max = $k;
}
reset($this->csv);
return $max+1;
}
function maxY(){
$max = 0;
while(list($k,) = each($this->csv)){
while(list($k2,) = each($this->csv[$k])){
if($k2>$max) $max = $k2;
}
reset($this->csv[$k]);
}
reset($this->csv);
return $max+1;
}
}
$mCsv = new MakeCsv('fileName');
$mCsv->downloadCsv();
?> |
Partager