qileilove

blog已经转移至github,大家请访问 http://qaseven.github.io/

PHP写的从数据库导入到EXCEL

原理: 就是原理很分页原理一样! 选取一定数量的数据然后变成数组,接着直接写入文件。接下来继续选取后面没选定数据在变成数组,接着在写入文件!这个解决了内存溢出。但是多CPU还是有个考验! 由于本人刚刚学PHP(PHP培训 php教程 )不久,功力不深厚!只能写出这样的东西!
  源码!
  Excel类
PHP code
class Excel{
var $header = "<?xml version="1.0" encoding="utf-8"?>
<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">";
var $footer = "</Workbook>";
var $lines = array ();
var $worksheet_title = "Table1";
function addRow ($array) {
$cells = "";
foreach ($array as $k => $v):
if(is_numeric($v)) {
if(substr($v, 0, 1) == 0) {
$cells .= "<Cell><Data ss:Type="String">" . $v . "</Data></Cell>n";
} else {
$cells .= "<Cell><Data ss:Type="Number">" . $v . "</Data></Cell>n";
}
} else {
$cells .= "<Cell><Data ss:Type="String">" . $v . "</Data></Cell>n";
}
endforeach;
$this->lines[] = "<Row>n" . $cells . "</Row>n";
unset($arry);
}
function setWorksheetTitle ($title) {
$title = preg_replace ("/[\|:|/|?|*|[|]]/", "", $title);
$title = substr ($title, 0, 31);
$this->worksheet_title = $title;
}
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"");
// 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" ss:Width="110"/>n";
echo implode ("n", $this->lines);
echo "</Table>n</Worksheet>n";
echo $this->footer;
exit;
}
function write ($filename) // 重点
{
$content= stripslashes ($this->header);
$content.= "n<Worksheet ss:Name="" . $this->worksheet_title . "">n<Table>n";
$content.= "<Column ss:Index="1" ss:AutoFitWidth="0" ss:Width="110"/>n";
$content.= implode ("n", $this->lines);
$content.= "</Table>n</Worksheet>n";
$content.= $this->footer;//EXCEL文件
//error_log($content, 3,$filename);
if (!file_exists($filename))//判断有没有文件
{
fopen($filename,'a');
}
$fp = fopen($filename,'a');
fwrite($fp, $content);//写入文件
fclose($fp);
unset($this->lines);//清空内存中的数据
}
}
 页面
PHPcode
include_once"./include/class.excel.PHP";//调用EXCEL类
require_once'./include/class.zipfile.PHP';//调用大包类
$xls=newExcel;//实例化
$w=explode("limit",$where_str);//把WHERE
$p=6000;//分页原理
$a=$ip_list_count/$p;//分页原理
if($ip_list_count%$p==0)//分页原理
else//分页原理
for($i=0;$i<=$a;$i++)//循环写出
{
$s=6000*$i;
$ip=$_SG['db']->fetch_all("select*frommain_info".$w[0]."limit".$s.",".$p);//调用自己写的数据库(数据库培训数据库认证)方法,写出数组
$xls->addArray($ip);//调用EXCEL类中addArray方法
xml1=$xls->write("./".$i.".xls");//调用EXCEL类中write方法
unset($ip);
unset($xml1);
sleep(1);
}

posted on 2014-11-26 15:08 顺其自然EVO 阅读(466) 评论(0)  编辑  收藏 所属分类: 测试学习专栏数据库


只有注册用户登录后才能发表评论。


网站导航:
 
<2014年11月>
2627282930311
2345678
9101112131415
16171819202122
23242526272829
30123456

导航

统计

常用链接

留言簿(55)

随笔分类

随笔档案

文章分类

文章档案

搜索

最新评论

阅读排行榜

评论排行榜