<?php
class
Excel_html{
private
$_common_style
= '';
private
$_head
= '';
private
$_body
= '';
private
$_head_bgcolor
= '#f3f3f3';
private
$_body_bgcolor
= '';
private
$_default_width
= 100;
Private
$_default_align
= 'center';
private
$_default_charset
= 'utf-8';
private
$_tables
= [];
private
$_table_brs
= [];
public
function
set_head_bgcolor(
$bgcolor
= '#f3f3f3'){
$this
->_head_bgcolor =
$bgcolor
;
}
public
function
set_body_bgcolor(
$bgcolor
= ''){
$this
->_body_bgcolor =
$bgcolor
;
}
public
function
set_charset(
$charset
= 'utf-8'){
$this
->_default_charset =
$charset
;
}
public
function
set_align(
$align
= 'center'){
$this
->_default_align =
$align
;
}
public
function
set_default_width(
$width
= 100){
$this
->_default_width =
$width
;
}
public
function
set_common_style(
$style
= ''){
$this
->_common_style =
$style
;
}
public
function
add_talbe(
$add_br
= 0){
$this
->_tables[] =
array
(
'head' =>
$this
->_head,
'body' =>
$this
->_body,
);
$this
->_table_brs[] =
$add_br
? 1 : 0;
$this
->_head = '';
$this
->_body = '';
}
public
function
add_head(
$head_arr
=
array
()){
$head_html
= '<tr>';
if
(
is_array
(
$head_arr
) && !
empty
(
$head_arr
)){
foreach
(
$head_arr
as
$head
){
if
(!
is_array
(
$head
)){
$head_html
.=
"<th width='"
.
$this
->_default_width .
"' bgcolor='"
.
$this
->_head_bgcolor .
"'>"
.
$this
->get_value(
$head
) .
"</th>\n"
;
}
else
{
$width
= !
empty
(
$head
[1]) ?
$head
[1] :
$this
->_default_width;
$other
= isset(
$head
[2]) ?
$head
[2] : [];
$head_html
.=
"<th width='"
.
$width
.
"' bgcolor='"
.
$this
->_head_bgcolor .
"'"
;
if
(!
empty
(
$other
) &&
is_array
(
$other
)){
foreach
(
$other
as
$k
=>
$v
){
$head_html
.=
" {$k}='{$v}' "
;
}
}
$head_html
.=
" >"
.
$this
->get_value(
$head
[0]) .
"</th>\n"
;
}
}
}
$head_html
.=
"</tr>\n"
;
$this
->_head .=
$head_html
;
}
public
function
add_body(
$body_arr
=
array
(),
$deal_long_num
= false){
$body_html
= '<tr>';
if
(
is_array
(
$body_arr
) && !
empty
(
$body_arr
)){
foreach
(
$body_arr
as
$body
){
if
(!
is_array
(
$body
)){
$style
=
$deal_long_num
? 'style=
"mso-number-format:\'\@\';"
' : '';
$body_html
.=
"<td bgcolor='"
.
$this
->_body_bgcolor .
"' align='"
.
$this
->_default_align .
"' {$style}>"
.
$this
->get_value(
$body
) .
"</td>\n"
;
}
else
{
$align
= isset(
$body
[1]) ?
$body
[1] :
$this
->_default_align;
$style
= isset(
$body
[2]) ?
$body
[2] : '';
$other
= isset(
$body
[3]) ?
$body
[3] : [];
if
(!
empty
(
$style
)){
$style
=
$deal_long_num
? (rtrim(
$style
, ';
"') . "
;mso-number-format:'\@';
" . '"
') :
$style
;
}
else
{
$style
=
$deal_long_num
? 'style=
"mso-number-format:\'\@\';"
' : '';
}
$body_html
.=
"<td bgcolor='"
.
$this
->_body_bgcolor .
"' align='"
.
$align
.
"' "
.
$style
.
""
;
if
(!
empty
(
$other
) &&
is_array
(
$other
)){
foreach
(
$other
as
$k
=>
$v
){
$body_html
.=
" {$k}='{$v}' "
;
}
}
$body_html
.=
">"
.
$this
->get_value(
$body
[0]) .
"</td>\n"
;
}
}
}
$body_html
.=
"</tr>\n"
;
$this
->_body .=
$body_html
;
}
public
function
downLoad(
$filename
= ''){
$this
->add_talbe();
$chare_set
=
$this
->_default_charset;
$down_content
= '<meta http-equiv=
"Content-Type"
content=
"text/html; charset=' . $chare_set . '"
/>' .
"\n"
;
$down_content
.=
$this
->_common_style;
foreach
(
$this
->_tables
as
$t_key
=>
$table
){
if
(
empty
(
$table
['head']) &&
empty
(
$table
['body'])){
continue
;
}
$down_content
.= '<table border=
"1"
>' .
"\n"
;
$down_content
.=
$table
['head'] .
"\n"
;
$down_content
.=
$table
['body'] .
"\n"
;
$down_content
.= '</table>';
if
(
$this
->_table_brs[
$t_key
]){
$down_content
.=
"<br/>"
;
}
$down_content
.=
"\n"
;
}
if
(!
$filename
) {
$filename
=
date
('YmdHis',time()).'.xls';
}
$ci
= &get_instance();
$ci
->load->helper('download');
force_download(
$filename
,
$down_content
);
}
private
function
get_value(
$value
){
if
(
strtolower
(
$this
->_default_charset) != 'utf-8'){
return
iconv('utf-8',
$this
->_default_charset,
$value
);
}
else
{
return
$value
;
}
}
}