设为首页 收藏本站
查看: 509|回复: 0

[经验分享] php生成excel

[复制链接]

尚未签到

发表于 2015-8-24 09:00:07 | 显示全部楼层 |阅读模式
  1.用类生成
  



<?php
/**
* Simple excel generating from PHP5
*
* This is one of my utility-classes.
*
* The MIT License
*
* Copyright (c) 2007 Oliver Schwarz
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
* @package Utilities
* @author Oliver Schwarz <oliver.schwarz@gmail.com>
* @version 1.0
*/
/**
* Generating excel documents on-the-fly from PHP5
*
* Uses the excel XML-specification to generate a native
* XML document, readable/processable by excel.
*
* @package Utilities
* @subpackage Excel
* @author Oliver Schwarz <oliver.schwarz@vaicon.de>
* @version 1.0
*
* @todo Add error handling (array corruption etc.)
* @todo Write a wrapper method to do everything on-the-fly
*/
class ExcelNew
{
/**
* Header of excel document (prepended to the rows)
*
* Copied from the excel xml-specs.
*
* @access private
* @var string
*/
private $header = "<?xml version=\"1.0\" encoding=\"GBK\"?\>
<Workbook xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\"
xmlns:x=\"urn:schemas-microsoft-com:office:excel\"
xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\"
xmlns:html=\"http://www.w3.org/TR/REC-html40\">";
/**
* Footer of excel document (appended to the rows)
*
* Copied from the excel xml-specs.
*
* @access private
* @var string
*/
private $footer = "</Workbook>";
/**
* Document lines (rows in an array)
*
* @access private
* @var array
*/
private $lines = array ();
private $Index = 1;
/**
* Worksheet title
*
* Contains the title of a single worksheet
*
* @access private
* @var string
*/
private $worksheet_title = "Table1";
/**
* Add a single row to the $document string
*
* @access private
* @param array 1-dimensional array
* @todo Row-creation should be done by $this->addArray
*/
private function addRow ($array)
{
// initialize all cells for this row
$cells = "";
// foreach key -> write value into cells
foreach ($array as $k => $v):
$cells .= "<Cell><Data ss:Type=\"String\">" . $v . "</Data></Cell>\n";
endforeach;
// transform $cells content into one row
$this->lines[] = "<Row>\n" . $cells . "</Row>\n";
}
/**
* Add an array to the document
*
* This should be the only method needed to generate an excel
* document.
*
* @access public
* @param array 2-dimensional array
* @todo Can be transfered to __construct() later on
*/
public function addArray ($array)
{
// run through the array and add them into rows
foreach ($array as $k => $v):
$this->addRow ($v);
endforeach;
}
/**
* Set the worksheet title
*
* Checks the string for not allowed characters (:\/?*),
* cuts it to maximum 31 characters and set the title. Damn
* why are not-allowed chars nowhere to be found? Windows
* help's no help...
*
* @access public
* @param string $title Designed title
*/
public function setWorksheetTitle ($title)
{
// strip out special chars first
$title = preg_replace ("/[\\\|:|\/|\?|\*|\[|\]]/", "", $title);
// now cut it to the allowed length
$title = substr ($title, 0, 31);
// set title
$this->worksheet_title = $title;
}
/**
* Generate the excel file
*
* Finally generates the excel file and uses the header() function
* to deliver it to the browser.
*
* @access public
* @param string $filename Name of excel file to generate (...xls)
*/
function generateXML ($filename)
{
// deliver header (as recommended in php manual)
header("Content-Type: application/vnd.ms-excel; charset=UTF-8");
header("Content-Disposition: inline; filename=\"" . $filename . ".xls\"");
//add by sun
header("Content-Transfer-Encoding: binary");
header("Pragma: public");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
// end
// print out document to the browser
// need to use stripslashes for the damn ">"
echo stripslashes ($this->header);
echo "\n<Worksheet ss:Name=\"" . $this->worksheet_title . "\">\n<Table>\n";
echo "<Column ss:Index=\"1\" ss:AutoFitWidth=\"0\" />\n";
echo implode ("\n", $this->lines);
echo "</Table>\n</Worksheet>\n";
echo $this->footer;
}
/**
* Add an array to the document
*
* This should be the only method needed to generate an excel
* document.
*
* @access public
* @param array 2-dimensional array
* @todo Can be transfered to __construct() later on
*/
public function addallArray ($array)
{
// run through the array and add them into rows
foreach ($array as $k => $v):
$this->addallRow ($v);
endforeach;
}
/*
$arr = array(
'row'=>array(
'cell'=>array(
'MergeAcross'=>'1',
'MergeDown'=>'3',
'Type'=>'Number',
'data'=>'网络房源总量',
),
),
'row'=>array(
'cell'=>array(
'data'=>'网络房源总量',
),
),
);
*/
public function addUMERows($arr)
{
if(is_array($arr))
{
$cells = '';
foreach($arr as $Row)
{
foreach($Row as $Cell)
{
$type = $Cell['Type']?$Cell['Type']:'String';
$MergeAcross = $Cell['MergeAcross']?" ss:MergeAcross=\"".$Cell['MergeAcross']."\"":'';
$MergeDown = $Cell['MergeDown']?" ss:MergeDown=\"".$Cell['MergeDown']."\"":'';
$cells .= "<Cell".$MergeAcross.$MergeDown."><Data ss:Type=\"".$type."\">" . $Cell['data'] . "</Data></Cell>\n";
}
$this->lines[] = "<Row>\n" . $cells . "</Row>\n";
$cells = '';
}
}
}
/**
* Add a single row to the $document string
*
* @access private
* @param array 1-dimensional array
* @todo Row-creation should be done by $this->addArray
*/
private function addallRow ($array)
{
// initialize all cells for this row
$cells = "";
$flag = false;
// foreach key -> write value into cells
foreach ($array as $k => $v):
if (gettype($v) == 'array')
{
foreach ($v as $k1 => $v1):
if ($flag)
{
$cells .= "<Cell ss:Index=\"".$this->Index."\"><Data ss:Type=\"String\">" . $v1 . "</Data></Cell>\n";
$flag = false;
}
else
{
$cells .= "<Cell><Data ss:Type=\"String\">" . $v1 . "</Data></Cell>\n";
}
endforeach;
}
else {
if ($v != "")
{
if ($k =='mergedown' || $k == 'mergedown1' || $k == 'mergedown2' || $k == 'mergedown3')
{
$cells .= "<Cell ss:MergeDown=\"2\" ><Data ss:Type=\"String\">" . $v . "</Data></Cell>\n";
}
else
{
$cells .= "<Cell><Data ss:Type=\"String\">" . $v . "</Data></Cell>\n";
}
if ($k == 'mergedown')  $this->Index = 2;
if ($k == 'mergedown1') $this->Index = 3;
if ($k == 'mergedown2') $this->Index = 4;
if ($k == 'mergedown3') $this->Index = 5;
}
else {
$flag = true;
}
}
endforeach;
// transform $cells content into one row
$this->lines[] = "<Row>\n" . $cells . "</Row>\n";
}
}
?>
  
  
  2.简单的输出excel文件



header("Content-type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=test.xml");
header("Pragma:no-cache");
header('Expires:0');
echo "world"."\t";
echo "\t\n";
/*start of second line*/  
echo "this is second line"."\t";  
echo "Hi,pretty girl"."\t";  
echo "\t\n";  
  3.
  
  

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-103242-1-1.html 上篇帖子: xampp环境下,配置Zend Studio调试php(XDebug) 下篇帖子: 编译PHP并与Ngnix整合
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表