SimpleExcel.php
[Pman.Core] / JsonToExcel.php
1 <?php
2
3 /**
4  * 
5  * MOVE To Core ---
6  * 
7  * Usage:
8  * 
9  * POST Values: 
10  *  xml = data
11  *  format = [empty=xls] , 'gnumeric'  (download format)
12  *  debug = true => download xml.
13  * 
14  * 
15  * 
16  */
17
18 require_once 'Pman.php';
19 class Pman_Core_JsonToExcel extends Pman
20 {
21     function getAuth()
22     {
23         $au = $this->getAuthUser();
24         if (!$au) {
25             die("NOT authenticated");
26         }
27         $this->authUser = $au;
28         return true;
29     }
30
31     function get($v, $opts=array())
32     {
33         $this->jerr("invalid get");
34     }
35     function post($fname) {
36         
37         $ml = (int) ini_get('suhosin.post.max_value_length');
38         if (empty($_POST['_json'])) {
39             header("HTTP/1.0 400 Internal Server Error");
40             die(  $ml ? "Suhosin Patch enabled - try and disable it!!!" : 'no JSON sent');
41         }
42         
43         if (empty($_POST['_json'])) {
44             header("HTTP/1.0 400 Internal Server Error");
45             die("Missing json attribute");
46         }
47         $json = json_decode($_POST['_json']);
48         
49         
50         require_once 'Spreadsheet/Excel/Writer.php';
51         // Creating a workbook
52         $outfile2 = $this->tempName('xls');
53        // var_dump($outfile2);
54         $workbook = new Spreadsheet_Excel_Writer($outfile2);
55         //$workbook = new Spreadsheet_Excel_Writer();
56         $workbook->setVersion(8);
57         // sending HTTP headers
58         $worksheet =  $workbook->addWorksheet("Sheet 1");
59         if (is_a($worksheet, 'PEAR_Error')) {
60             die($worksheet->toString());
61         }
62         //print_R($worksheet);
63         $worksheet->setInputEncoding('UTF-8');
64         
65         for ($r = 0; $r < count($json); $r++) {
66             $row = $json[$r];
67             for ($c = 0; $c < count($row); $c++) {
68                 $worksheet->write($r, $c, $row[$c]);
69             }
70             
71         }
72          $workbook->close();
73         
74         require_once 'File/Convert.php';
75         $fc=  new File_Convert($outfile2, "application/vnd.ms-excel");
76         $fn = $fc->convert("application/vnd.ms-excel"); 
77         $fc->serve('attachment','excel-'.date('Y-m-d-H-i-s').'.xls'); // can fix IE Mess
78         unlink($outfile2); 
79     }
80      
81 }