Created
March 6, 2014 04:23
-
-
Save yuktse/9382642 to your computer and use it in GitHub Desktop.
以HTML形式输出EXCEL文件
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 以下样式可以输出到HTML中,一起导出到excel文件 | |
@page{margin: 30px 20px} /*打印页边距*/ | |
td{mso-number-format:"\@"; } // 解决长数字默认为科学记数法形式。在数字前面加单引号(')也可解决,只是在页面显示时不好看。 | |
/* 附上各种格式 | |
mso-number-format:"0" //NO Decimals | |
mso-number-format:"0\.000" //3 Decimals | |
mso-number-format:"\#\,\#\#0\.000" //Comma with 3 dec | |
mso-number-format:"mm\/dd\/yy" //Date7 | |
mso-number-format:"mmmm\ d\,\ yyyy" //Date9 | |
mso-number-format:"m\/d\/yy\ h\:mm\ AM\/PM" //D -T AMPM | |
mso-number-format:"Short Date" //01/03/1998 | |
mso-number-format:"Medium Date" //01-mar-98 | |
mso-number-format:"d\-mmm\-yyyy" //01-mar-1998 | |
mso-number-format:"Short Time" //5:16 | |
mso-number-format:"Medium Time" //5:16 am | |
mso-number-format:"Long Time" //5:16:21:00 | |
mso-number-format:"Percent" //Percent - two decimals | |
mso-number-format:"0%" //Percent - no decimals | |
mso-number-format:"0\.E+00" //Scientific Notation | |
mso-number-format:"\@" //Text | |
*/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @sample | |
* [code] | |
* $excel = new ExcelOutput(); | |
* $excel->start(); | |
* | |
* // ... page content in html format comes here | |
* | |
* $e->save(); | |
* [/code] | |
*/ | |
class ExcelOutput{ | |
public $path; | |
public $data; | |
public $fileName; | |
public function __construct(){ | |
$this->fileName = uniqid(); | |
} | |
public function start(){ | |
ob_start(); | |
print'<html xmlns:o="urn:schemas-microsoft-com:office:office" | |
xmlns:w="urn:schemas-microsoft-com:office:word" | |
xmlns="http://www.w3.org/TR/REC-html40">'; | |
} | |
public function save(){ | |
print "</html>"; | |
$this->data = ob_get_contents(); | |
ob_end_clean(); | |
$this->wirtefile(); | |
} | |
public function setFileName($fileName){ | |
$this->fileName = $fileName; | |
} | |
public function wirtefile (){ | |
//保存打单信息 | |
$fileName = ''; | |
if($this->fileName){ | |
$fileName = $this->fileName; | |
}else{ | |
$fileName = uniqid(); | |
} | |
header( "Pragma: public" ); | |
header( "Expires: 0" ); | |
header( 'Content-Encoding: none' ); | |
header( "Cache-Control: must-revalidate, post-check=0, pre-check=0" ); | |
header( "Cache-Control: public" ); | |
// header( "Content-type: application/octet-stream\n" ); | |
header( "Content-type: application/ms-excel" ); | |
// header( "Content-Description: File Transfer" ); | |
header( 'Content-Disposition: attachment; filename=' . $fileName.'.xls'); | |
header( "Content-Transfer-Encoding: binary" ); | |
// header( 'Content-Length: ' . filesize ( $this->path) ); | |
echo $this->data; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment