forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
psxlsgen.php
204 lines (187 loc) · 6.42 KB
/
psxlsgen.php
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
<?php // $Id$
/****************************************************************
* Script : PHP Simple Excel File Generator - Base Class
* Project : PHP SimpleXlsGen
* Author : Erol Ozcan <[email protected]>
* Version : 0.3
* Copyright : GNU LGPL
* URL : http://psxlsgen.sourceforge.net
* Last modified : 13 Jun 2001
* Description : This class is used to generate very simple
* MS Excel file (xls) via PHP.
* The generated xls file can be obtained by web as a stream
* file or can be written under $default_dir path. This package
* is also included mysql, pgsql, oci8 database interaction to
* generate xls files.
* Limitations:
* - Max character size of a text(label) cell is 255
* ( due to MS Excel 5.0 Binary File Format definition )
*
* Credits : This class is based on Christian Novak's small
* Excel library functions.
******************************************************************/
if( !defined( "PHP_SIMPLE_XLS_GEN" ) ) {
define( "PHP_SIMPLE_XLS_GEN", 1 );
class PhpSimpleXlsGen {
var $class_ver = "0.3"; // class version
var $xls_data = ""; // where generated xls be stored
var $default_dir = ""; // default directory to be saved file
var $filename = "psxlsgen"; // save filename
var $fname = ""; // filename with full path
var $crow = 0; // current row number
var $ccol = 0; // current column number
var $totalcol = 0; // total number of columns
var $get_type = 0; // 0=stream, 1=file
var $errno = 0; // 0=no error
var $error = ""; // error string
var $dirsep = "/"; // directory separator
var $header = 1; // 0=no header, 1=header line for xls table
// Default constructor
function PhpSimpleXlsGen()
{
$os = getenv( "OS" );
$temp = getenv( "TEMP");
// check OS and set proper values for some vars.
if ( stristr( $os, "Windows" ) ) {
$this->default_dir = $temp;
$this->dirsep = "\\";
} else {
// assume that is Unix/Linux
$this->default_dir = "/tmp";
$this->dirsep = "/";
}
// begin of the excel file header
$this->xls_data = pack( "vvvvvv", 0x809, 0x08, 0x00,0x10, 0x0, 0x0 );
// check header text
if ( $this->header ) {
$this->Header();
}
}
function Header( $text="" ) {
if ( $text == "" ) {
$text = "This file was generated using PSXlsGen at ".date("D, d M Y H:i:s T");
}
if ( $this->totalcol < 1 ) {
$this->totalcol = 1;
}
$this->InsertText( $text );
$this->crow += 2;
$this->ccol = 0;
}
// end of the excel file
function End()
{
$this->xls_data .= pack("vvvvvvvC", 0x7D, 11, 3, 4, 25600,0,0,0);
$this->xls_data .= pack( "vv", 0x0A, 0x00 );
return;
}
// write a Number (double) into row, col
function WriteNumber_pos( $row, $col, $value )
{
$this->xls_data .= pack( "vvvvv", 0x0203, 14, $row, $col, 0x00 );
$this->xls_data .= pack( "d", $value );
return;
}
// write a label (text) into Row, Col
function WriteText_pos( $row, $col, $value )
{
$len = strlen( $value );
$this->xls_data .= pack( "v*", 0x0204, 8 + $len, $row, $col, 0x00, $len );
$this->xls_data .= $value;
return;
}
// insert a number, increment row,col automatically
function InsertNumber( $value )
{
if ( $this->ccol == $this->totalcol ) {
$this->ccol = 0;
$this->crow++;
}
$this->WriteNumber_pos( $this->crow, $this->ccol, $value );
$this->ccol++;
return;
}
// insert text, increment row,col automatically
function InsertText( $value )
{
if ( $this->ccol == $this->totalcol ) {
$this->ccol = 0;
$this->crow++;
}
$this->WriteText_pos( $this->crow, $this->ccol, $value );
$this->ccol++;
return;
}
// Change position of row,col
function ChangePos( $newrow, $newcol )
{
$this->crow = $newrow;
$this->ccol = $newcol;
return;
}
// new line
function NewLine()
{
$this->ccol = 0;
$this->crow++;
return;
}
// send generated xls as stream file
function SendFileName( $filename )
{
$this->filename = $filename;
$this->SendFile();
}
// send generated xls as stream file
function SendFile()
{
global $HTTP_USER_AGENT;
$this->End();
header ( "Expires: Mon, 1 Apr 1974 05:00:00 GMT" );
header ( "Last-Modified: " . gmdate("D,d M YH:i:s") . " GMT" );
header ( "Pragma: no-cache" );
if (!strstr($HTTP_USER_AGENT,"MSIE")) {
$attachment=" attachment;";
}
header("Content-Type: application/x-msexcel\r\n");
header("Content-Disposition:$attachment filename=$this->filename.xls\r\n\r\n");
header("Content-Transfer-Encoding: binary\r\n");
header("Content-Description: Excel Spreadsheet" );
print $this->xls_data;
}
// change the default saving directory
function ChangeDefaultDir( $newdir )
{
$this->default_dir = $newdir;
return;
}
// Save generated xls file
function SaveFileName( $filename )
{
$this->filename = $filename;
$this->SaveFile();
}
// Save generated xls file
function SaveFile()
{
$this->End();
$this->fname = $this->default_dir."$this->dirsep".$this->filename;
if ( !stristr( $this->fname, ".xls" ) ) {
$this->fname .= ".xls";
}
$fp = fopen( $this->fname, "wb" );
fwrite( $fp, $this->xls_data );
fclose( $fp );
return;
}
// Get generated xls as specified type
function GetXls( $type = 0 ) {
if ( !$type && !$this->get_type ) {
$this->SendFile();
} else {
$this->SaveFile();
}
}
} // end of the class PHP_SIMPLE_XLS_GEN
}
// end of ifdef PHP_SIMPLE_XLS_GEN