PHP写的从数据库导入到EXCEL
PHP 写的从数据库导入到EXCEL终于写完了! 这个问题纠结了我一个星期! 谢谢上面帮助我的人和给我提的意见!
以前不能导出的问题是:
内存溢出然后页报错调用不了内存(呵呵,这个问题我还专门到国外论坛上发帖看看有上面方法解决,一个老外开跟我开一个玩笑 叫我设置一下这个ini_set("memory_limit","2000M");然后直接导致我机器死机)和CPU100% .
现在我自己通过用分批写入到文件中来实现!
原理我说一下: 就是原理很分页原理一样! 选取一定数量的数据然后变成数组,接着直接写入文件。接下来继续选取后面没选定数据在变成数组,接着在写入文件!这个解决了内存溢出。但是多CPU还是有个考验! 由于本人刚刚学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=""";
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 .= "CellData ss:Type="String"" . $v . "/Data/Cell";
} else {
$cells .= "CellData ss:Type="Number"" . $v . "/Data/Cell";
}
} else {
$cells .= "CellData ss:Type="String"" . $v . "/Data/Cell";
}
endforeach;
$this-lines[] = "Row" . $cells . "/Row";
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/-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 "Worksheet ss:Name="" . $this-worksheet_title . ""Table";
echo "Column ss:Index="1" ss:AutoFitWidth="0" ss:Width="110"/";
echo implode ("", $this-lines);
echo "/Table/Worksheet";
echo $this-footer;
exit;
}
function write ($filename) // 重点
{
$content= stripslashes ($this-header);
$content.= "Worksheet ss:Name="" . $this-worksheet_title . ""Table";
$content.= "Column ss:Index="1" ss:AutoFitWidth="0" ss:Width="110"/";
$content.= implode ("", $this-lines);
$content.= "/Table/Worksheet";
$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);//清空内存中的数据
}
}
页面
PHP code
include_once"./include/p";//调用EXCEL类
require_once "./include/p";//调用大包类
$xls = new Excel;//实例化
$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 * from main_info".$w[0]." limit ".$s.",".$p);//调用自己写的数据库方法,写出数组
$xls-addArray ( $ip );//调用EXCEL 类中addArray 方法
xml1=$xls-write ("./".$i.".xls");//调用EXCEL 类中write 方法
unset($ip);
unset($xml1);
sleep(1);
}
小便黄赤是什么原因上一篇:除夕夜上鼓山有公交专线每人10元