Excel::SetRow()   A
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
nc 5
nop 1
dl 0
loc 24
rs 9.2248
c 0
b 0
f 0
1
<?php
2
/**
3
 * @package		fwolflib
4
 * @subpackage	class
5
 * @copyright	Copyright 2003-2009, Fwolf
6
 * @author		Fwolf <[email protected]>
7
 * @since		2009-12-22
8
 */
9
10
11
require_once(dirname(__FILE__) . '/fwolflib.php');
12
13
14
/**
15
 * @package		fwolflib
16
 * @subpackage	class
17
 * @copyright	Copyright 2003-2009, Fwolf
18
 * @author		Fwolf <[email protected]>
19
 * @since		2009-12-22
20
 * @link http://www.phplamp.org/2008/06/php-to-excel-clas/
21
 */
22
class Excel extends Fwolflib {
0 ignored issues
show
Deprecated Code introduced by
The class Fwolflib has been deprecated with message: Use classes in Fwlib namespace, see PSR-0/1/2

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
23
24
	/**
25
	 * Array of row in xml
26
	 * @var	array
27
	 */
28
	protected $aRow = array();
29
30
    /**
31
     * Footer of excel xml
32
     * @var string
33
     */
34
    protected $sFooter = "</Workbook>";
35
36
    /**
37
     * Header of excel xml
38
     * @var string
39
     */
40
    protected $sHeader = '<?xml version="1.0" encoding="utf-8"?>
41
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
42
	xmlns:x="urn:schemas-microsoft-com:office:excel"
43
	xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
44
	xmlns:html="http://www.w3.org/TR/REC-html40">
45
	';
46
47
	/**
48
	 * Title of sheet
49
	 * @var	string
50
	 */
51
	public $sSheetTitle = 'Sheet1';
52
53
54
    /**
55
     * Set a single row(add mode)
56
     *
57
     * @param	array	$ar	1-dimensional array
58
     */
59
    protected function SetRow($ar) {
60
        $s_cell = '';
61
62
		if (!empty($ar)) {
63
			foreach ($ar as $v) {
64
				// Attention: data type
65
				if(is_numeric($v)) {
66
					$v = strval($v);
67
					// First letter is '0' ?
68
					if(0 == $v{0}) {
69
						$s_cells.= '<Cell><Data ss:Type="string">' . $v . '</Data></Cell>
0 ignored issues
show
Bug introduced by
The variable $s_cells does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
70
						';
71
					} else {
72
						$s_cell .= '<Cell><Data ss:Type="number">' . $v . '</Data></Cell>
73
						';
74
					}
75
				} else {
76
					$s_cell .= '<Cell><Data ss:Type="string">' . $v . '</Data></Cell>
77
					';
78
				}
79
			}
80
			$this->aRow[] = "<Row>\n$s_cell</Row>\n";
81
		}
82
    } // end of func SetRow
83
84
85
    /**
86
     * Set data rows, multi row, clean mode
87
     *
88
     * @param	array	$ar	2-dimensional array
89
     */
90
    public function SetRows ($ar) {
91
        if (!empty($ar))
92
			foreach ($ar as $v)
93
				$this->SetRow($v);
94
    } // end of func SetRows
95
96
97
    /**
98
     * Set the worksheet title
99
     *
100
     * Checks the string for not allowed characters (:\/?*),
101
     * cuts it to maximum 31 characters and set the title. Damn
102
     * why are not-allowed chars nowhere to be found? Windows
103
     * help's no help...
104
     *
105
     * @param	string	$title
106
     */
107
    public function SetSheetTitle($title) {
108
        // Strip special chars
109
        $title = str_replace (
110
			array(':', '\\', '/', '?', '*'),
111
			'', $title);
112
113
        // Cut it to the allowed length
114
        $title = substr($title, 0, 31);
115
116
        $this->sSheetTitle = $title;
117
    } // end of func SetSheetTitle
118
119
120
    /**
121
     * Output the excel file
122
     *
123
     * @param	string	$fn	Filename without '.xls'
124
     */
125
    public function Output($fn) {
126
        // Set header
127
        header('Content-Type: application/vnd.ms-excel;  charset=utf-8');
128
        header('Content-Disposition: inline; filename="'
129
			. $fn . '.xls"');
130
131
        // Print
132
        echo ($this->sHeader);
133
        echo '<Worksheet ss:Name="'
134
			. $this->sSheetTitle
135
			. '">
136
			<Table>
137
		';
138
        echo '<Column ss:Index="1" ss:AutoFitWidth="0" ss:Width="110"/>
139
		';
140
        echo implode("\n", $this->aRow);
141
        echo '</Table>
142
			</Worksheet>
143
		';
144
        echo $this->sFooter;
145
    }
146
147
} // end of class Excel
148
149
/*
150
// Usage
151
// Need high version of microsoft office
152
153
$ar = array (
154
	array ('列1', '列2', '列3列3列3', '123456'),
155
);
156
$xls = new Excel;
157
$xls->SetRows($ar);
158
$xls->Output('test');
159
*/
160
161
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...
162