dongzha3058 2013-07-25 07:17
浏览 49

Zend Framework导出文件和PHP excel编写器

  1. I have my Zend environment and I'm using phpexcelwriter (it is a set of lib/class files to export excel).
  2. I have a MySQL query which fetch information from six individual tables. (so the query will fetch a record of more than 10,000 records)
  3. When a single user try to export the record, my system/code works fine and I don't see any issue.
  4. When more than one user (say two users try to export the record), the CPU usage is >100% and I'm getting an internal error 501 and the system is not responding to the user.

Is there a way to overcome this problem?

Here is my sample code:

Lib File:

<?php

     /*
     ###############################################
     ####                                       ####
     ####    Author : Harish Chauhan            ####
     ####    Updated: 11 Dec,2009               ####
     ####    Updated By: Vijay Joshi            ####
     ####                                       ####
     ###############################################

     */


     /*
     * Class is used for save the data into microsoft excel format.
     * It takes data into array or you can write data column vise.
     */


    Class ExcelWriter
    {

        var $fp=null;
        var $error;
        var $state="CLOSED";
        var $newRow=false;

        /*
        * @Params : $file  : file name of excel file to be created.
        * @Return : On Success Valid File Pointer to file
        *           On Failure return false  
        */

        function ExcelWriter($file="")
        {
            return $this->open($file);
        }

        /*
        * @Params : $file  : file name of excel file to be created.
        *           if you are using file name with directory i.e. test/myFile.xls
        *           then the directory must be existed on the system and have permissioned properly
        *           to write the file.
        * @Return : On Success Valid File Pointer to file
        *           On Failure return false  
        */
        function open($file)
        {
            if($this->state!="CLOSED")
            {
                $this->error="Error : Another file is opend .Close it to save the file";
                return false;
            }   

            if(!empty($file))
            {
                $this->fp=@fopen($file,"w+");
            }
            else
            {
                $this->error="Usage : New ExcelWriter('fileName')";
                return false;
            }   
            if($this->fp==false)
            {
                $this->error="Error: Unable to open/create File.You may not have permmsion to write the file.";
                return false;
            }
            $this->state="OPENED";
            fwrite($this->fp,$this->GetHeader());
            return $this->fp;
        }

        function close()
        {
            if($this->state!="OPENED")
            {
                $this->error="Error : Please open the file.";
                return false;
            }   
            if($this->newRow)
            {
                fwrite($this->fp,"</tr>");
                $this->newRow=false;
            }

            fwrite($this->fp,$this->GetFooter());
            fclose($this->fp);
            $this->state="CLOSED";
            return ;
        }
        /* @Params : Void
        *  @return : Void
        * This function write the header of Excel file.
        */

        function GetHeader()
        {
            $header = <<<EOH
                <html xmlns:o="urn:schemas-microsoft-com:office:office"
                xmlns:x="urn:schemas-microsoft-com:office:excel"
                xmlns="http://www.w3.org/TR/REC-html40">

                <head>
                <meta http-equiv=Content-Type content="text/html; charset=us-ascii">
                <meta name=ProgId content=Excel.Sheet>
                <!--[if gte mso 9]><xml>
                 <o:DocumentProperties>
                  <o:LastAuthor>Sriram</o:LastAuthor>
                  <o:LastSaved>2005-01-02T07:46:23Z</o:LastSaved>
                  <o:Version>10.2625</o:Version>
                 </o:DocumentProperties>
                 <o:OfficeDocumentSettings>
                  <o:DownloadComponents/>
                 </o:OfficeDocumentSettings>
                </xml><![endif]-->
                <style>
                <!--table
                    {mso-displayed-decimal-separator:"\.";
                    mso-displayed-thousand-separator:"\,";}
                @page
                    {margin:1.0in .75in 1.0in .75in;
                    mso-header-margin:.5in;
                    mso-footer-margin:.5in;}
                tr
                    {mso-height-source:auto;}
                col
                    {mso-width-source:auto;}
                br
                    {mso-data-placement:same-cell;}
                .style0
                    {mso-number-format:General;
                    text-align:general;
                    vertical-align:bottom;
                    white-space:nowrap;
                    mso-rotate:0;
                    mso-background-source:auto;
                    mso-pattern:auto;
                    color:windowtext;
                    font-size:10.0pt;
                    font-weight:400;
                    font-style:normal;
                    text-decoration:none;
                    font-family:Arial;
                    mso-generic-font-family:auto;
                    mso-font-charset:0;
                    border:none;
                    mso-protection:locked visible;
                    mso-style-name:Normal;
                    mso-style-id:0;}
                td
                    {mso-style-parent:style0;
                    padding-top:1px;
                    padding-right:1px;
                    padding-left:1px;
                    mso-ignore:padding;
                    color:windowtext;
                    font-size:10.0pt;
                    font-weight:400;
                    font-style:normal;
                    text-decoration:none;
                    font-family:Arial;
                    mso-generic-font-family:auto;
                    mso-font-charset:0;
                    mso-number-format:General;
                    text-align:general;
                    vertical-align:bottom;
                    border:none;
                    mso-background-source:auto;
                    mso-pattern:auto;
                    mso-protection:locked visible;
                    white-space:nowrap;
                    mso-rotate:0;}
                .xl24
                    {mso-style-parent:style0;
                    white-space:normal;}
                -->
                </style>
                <!--[if gte mso 9]><xml>
                 <x:ExcelWorkbook>
                  <x:ExcelWorksheets>
                   <x:ExcelWorksheet>
                    <x:Name>Winery datas</x:Name>
                    <x:WorksheetOptions>
                     <x:Selected/>
                     <x:ProtectContents>False</x:ProtectContents>
                     <x:ProtectObjects>False</x:ProtectObjects>
                     <x:ProtectScenarios>False</x:ProtectScenarios>
                    </x:WorksheetOptions>
                   </x:ExcelWorksheet>
                  </x:ExcelWorksheets>
                  <x:WindowHeight>10005</x:WindowHeight>
                  <x:WindowWidth>10005</x:WindowWidth>
                  <x:WindowTopX>120</x:WindowTopX>
                  <x:WindowTopY>135</x:WindowTopY>
                  <x:ProtectStructure>False</x:ProtectStructure>
                  <x:ProtectWindows>False</x:ProtectWindows>
                 </x:ExcelWorkbook>
                </xml><![endif]-->
                </head>

                <body link=blue vlink=purple>
                <table x:str border=0 cellpadding=0 cellspacing=0 style='border-collapse: collapse;table-layout:fixed;'>
EOH;
            return $header;
        }

        function GetFooter()
        {
            return "</table></body></html>";
        }

        /*
        * @Params : $line_arr: An valid array 
        * @Params : $css: optional : An array of css properties
        * @Return : Void
        */

        function writeLine($line_arr, $css = NULL)
        {
            if($this->state!="OPENED")
            {
                $this->error="Error : Please open the file.";
                return false;
            }   
            if(!is_array($line_arr))
            {
                $this->error="Error : Argument is not valid. Supply an valid Array.";
                return false;
            }

            if($css!=NULL)
            {
                $cssStr = $this->getCssString($css);
            }
            fwrite($this->fp,"<tr>");
            foreach($line_arr as $col)
                fwrite($this->fp,"<td class=xl24 width=64 $cssStr>$col</td>");
            fwrite($this->fp,"</tr>");
        }

        function getCssString($css)
        {
            $returnVal = "style='";
            foreach($css as $key => $val)
            {
                $returnVal.= $key.':'.$val.';';
            }
            $returnVal.= "'";
            return $returnVal;
        }

        /*
        * @Params : Void
        * @Return : Void
        */
        function writeRow()
        {
            if($this->state!="OPENED")
            {
                $this->error="Error : Please open the file.";
                return false;
            }   
            if($this->newRow==false)
                fwrite($this->fp,"<tr>");
            else
                fwrite($this->fp,"</tr><tr>");
            $this->newRow=true; 
        }

        /*
        * @Params : $value : Column Value
        * @Params : $css: optional : An array of css properties
        * @Return : Void
        */
        function writeCol($value,$css = NULL)
        {
            if($this->state!="OPENED")
            {
                $this->error="Error : Please open the file.";
                return false;
            }   
            if($css!=NULL)
            {
                $cssStr = $this->getCssString($css);
            }

            fwrite($this->fp,"<td class=xl24 width=64 $cssStr>$value</td>");
        }
    }
?>

My PHP File:

<?php
    include("excelwriter.inc.php");
    $select_query=getenv("SELECTQUERY");
    $strFileName = getenv("FILENAME");
    $fp = fopen('/home/myprojectfilepath/public_html/dev/Export/Tests/output/'.basename($strFileName).'.txt', 'a+');
    $fp = fopen('/home/myprojectfilepath/public_html/dev/Export/Tests/output/'.basename($strFileName).'.txt',"w");
    fwrite($fp, '0');
    fclose($fp);
    $fp = fopen($strFileName,'a+');

    $conn = mysql_connect('localhost','mysqlusername','mysqlpassword');
    mysql_select_db('mydatabase',$conn);
    $result = mysql_query($select_query, $conn );
    $row = mysql_fetch_assoc( $result ); // Get the column names
    $excel = new ExcelWriter($strFileName);


    if($excel==false)   
    {
        echo $excel->error;
        die;
    }
    if ( $row )
    {
        $row_coulumn = array_keys($row);
        $row_coulumn[0] = "Company Name";

        ............Assing Field Name in Excel Sheet........................

        $row_coulumn[28] = "Title Group";

        $excel->writeLine($row_coulumn, array('text-align'=>'center', 'color'=> 'black','border'=> '1px solid black','font-weight'=>'bold'));
    }

    while($row)
    {
    .............Insert value for each record.......................
    }
    $excel->close();
    $fp = fopen('/home/myprojectpath/public_html/dev/Export/Tests/output/'.basename($strFileName).'.txt', 'a+');
    $fp = fopen('/home/myprojectpath/public_html/dev/Export/Tests/output/'.basename($strFileName).'.txt',"w");
    fwrite($fp, '1');
    fclose($fp);
?>
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 winform的chart曲线生成时有凸起
    • ¥15 msix packaging tool打包问题
    • ¥15 finalshell节点的搭建代码和那个端口代码教程
    • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
    • ¥15 Centos / PETSc / PETGEM
    • ¥15 centos7.9 IPv6端口telnet和端口监控问题
    • ¥120 计算机网络的新校区组网设计
    • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
    • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
    • ¥20 海浪数据 南海地区海况数据,波浪数据