Skip to content

Instantly share code, notes, and snippets.

@yuktse
Created March 6, 2014 04:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yuktse/9382642 to your computer and use it in GitHub Desktop.
Save yuktse/9382642 to your computer and use it in GitHub Desktop.
以HTML形式输出EXCEL文件
// 以下样式可以输出到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
*/
/**
* @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