excelsheet.cls.php

Gesponsorde koppelingen

PHP script bestanden

  1. excelsheet.cls.php

« Lees de omschrijving en reacties

Code (php)
PHP script in nieuw venster Selecteer het PHP script
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
<?php

/*
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program. If not, see <http://www.gnu.org/licenses/>.
*/


    class Excelsheet
    {
        private $aData = array();
        private $iRowNumber = 0;

        public function __construct()
        {
        }


        // Insert cell value
        public function setCell($iRow, $iCol, $sValue = '')
        {

            // Add rows when required
            for($i = 0; $i <= $iRow; $i++)
            {

                if(isset($this->aData[$i]) == false)
                {

                    $this->aData[$i] = array();
                }
            }


            // Add columns when required
            for($i = 0; $i <= $iCol; $i++)
            {

                if(isset($this->aData[$iRow][$i]) == false)
                {

                    $this->aData[$iRow][$i] = '';
                }
            }


            // Assign value
            $this->aData[$iRow][$iCol] = $sValue;
        }


        // Insert row
        public function setRow($iRow, $aRow = array(''))
        {

            // Add rows when required
            for($i = 0; $i <= $iRow; $i++)
            {

                if(isset($this->aData[$i]) == false)
                {

                    $this->aData[$i] = array();
                }
            }


            // Assign row
            $this->aData[$iRow] = $aRow;
        }


        // Append row (1d array)
        public function addRow($aRow)
        {

            if(is_array($aRow))
            {

                $this->aData[] = $aRow;
            }

            else
            {
                $this->aData[][0] = $aRow;
            }
        }


        // Append multiple rows (2d array)
        public function addData($aData)
        {

            if(is_array($aData) && sizeof($aData))
            {

                foreach($aData as $k => $v)
                {

                    $this->addRow($v);
                }
            }
        }

        
        public function saveFile($sFilePath)
        {

            $sData = $this->buildSheet();

            @
touch($sFilePath);
            @
chmod($sFilePath, 0777);

            if(@file_put_contents($sFilePath, $sData))
            {

                return true;
            }


            return false;
        }


        public function sendFile($sFileName)
        {

            $sData = $this->buildSheet();

            header('Pragma: public');
            header('Expires: 0');
            header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
            header('Content-Type: application/force-download');
            header('Content-Type: application/octet-stream');
            header('Content-Type: application/download');
            header('Content-Disposition: attachment; filename=' . $sFileName);
            header('Content-Transfer-Encoding: binary');
            echo $sData;
            exit;
        }


        private function buildSheet()
        {

            $sData = pack('ssssss', 0x809, 0x8, 0x0, 0x10, 0x0, 0x0);

            if(is_array($this->aData) && sizeof($this->aData))
            {

                $sData .= $this->buildData($this->aData);
            }


            $sData .= pack('ss', 0x0A, 0x00);

            return $sData;
        }


        private function buildData($aData)
        {

            $sData = '';
            $this->iRowNumber = 0;

            foreach($aData as $aCells)
            {

                $iColumnNumber = 0;

                foreach($aCells as $mCell)
                {

                    if(is_array($mCell))
                    {

                        $sData .= $this->textFormat($this->iRowNumber, $iColumnNumber, json_encode($mCell));
                    }

                    elseif(is_numeric($mCell))
                    {

                        $sData .= $this->numberFormat($this->iRowNumber, $iColumnNumber, $mCell);
                    }

                    else
                    {
                        $sData .= $this->textFormat($this->iRowNumber, $iColumnNumber, $mCell);
                    }


                    $iColumnNumber++;
                }


                $this->iRowNumber++;
            }


            return $sData;
        }


        private function textFormat($row, $col, $data)
        {

            $data = utf8_decode($data);
            $length = strlen($data);
            $cell = pack('ssssss', 0x204, 8 + $length, $row, $col, 0x0, $length);
            $cell .= $data;

            return $cell;
        }


        private function numberFormat($row, $col, $data)
        {

            $cell = pack('sssss', 0x203, 14, $row, $col, 0x0);
            $cell .= pack('d', $data);

            return $cell;
        }
    }

    function
arrayToExcel($aData, $sFileName)
    {

        $oExcel = new Excelsheet();

        foreach($aData as $aLine)
        {

            $oExcel->addRow($aLine);
        }


        if((strpos($sFileName, '/') === false) && (strpos($sFileName, '\\') === false))
        {

            $oExcel->sendFile($sFileName);
        }

        else
        {
            return $oExcel->saveFile($sFileName);
        }
    }


?>

 
 

Om de gebruiksvriendelijkheid van onze website en diensten te optimaliseren maken wij gebruik van cookies. Deze cookies gebruiken wij voor functionaliteiten, analytische gegevens en marketing doeleinden. U vindt meer informatie in onze privacy statement.