PHP寫的從資料庫匯入到EXCEL

來源:互聯網
上載者:User

標籤:http   io   ar   os   for   sp   檔案   資料   on   

原理: 就是原理很分頁原理一樣! 選取一定數量的資料然後變成數組,接著直接寫入檔案。接下來繼續選取後面沒選定資料在變成數組,接著在寫入檔案!這個解決了記憶體溢出。但是多CPU還是有個考驗! 由於本人剛剛學PHP(PHP培訓 php教程 )不久,功力不深厚!只能寫出這樣的東西! 

源碼! 
Excel(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);//清空記憶體中的資料
    }

}

 

頁面 
PHP code
include_once"./include/class.excel.PHP";//調用EXCEL類
require_once ‘./include/class.zipfile.PHP‘;//調用大包類 
$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);
}

PHP寫的從資料庫匯入到EXCEL

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.