如何在今天的日期保存php excel文件

时间:2014-04-14 06:51:56

标签: php mysql excel

过去三天,我正在努力使用这个excel文件报告生成。我可以下载该文件。但我想在今天的日期下载该文件。像abcd_14 / 4 / 2014.xls。下面给出了我的代码。请帮我解决这个问题。

excel_report.php

<?php

include("connection.php");
require_once("excel.class.php");

if(isset($_POST['submit']) && $_POST['submit']=="Download")
    {
    $payor_code = $_POST['payor_code'];
        $corp_code= $_POST['corp_code'];
        $pro_type = isset($_POST['product_type']);
        $submit_date = $_POST['sub_date'];


$dat = date("d/m/Y");

   $filename = "Report_$dat";

    $excel=new ExcelWriter("reports\STMB_report.xls");

$header = array(
    "ReceivedDate",
    "SubmissionDate",
    "PatientName",
    "PatientIC",
    "PrincipalName",
    "PrincipalIC",
    "ClaimNo"

       );
//$excel->writeLine($header);

    $excel->writeLine($header);
 $sql= oci_parse($conn, " select * from members ");

oci_execute($sql);
$dat = date("d/m/Y");


if($sql!=false)
{

        while($row=oci_fetch_array($sql))
    {

     $rec_date = $row['RECEIVED_DATE'];
    $sub_date = $row['SUBMISSION_DATE'];
    $pat_name = $row['PATIENT_NAME'];
    $nric_id = $row['NRIC_ID'];
    $pric_name = $row['PRINCIPAL_NAME'];
    $pric_nric = $row['PRINCIPAL_NRIC'];
    $claim_id = $row['CLAIMS_ID'];

        $header=array($rec_date,$sub_date,$pat_name, $nric_id, $pric_name, $pric_nric,$claim_id);
        $excel->writeLine($header);

    }
}echo "File Download succesfully";


}


?>

Excel类文件是,

excel.class.php

<?php

    $dat = date("d/m/Y");
//echo $dat;
   $filename = "Report_$dat";
 /*
     * 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>STMB_reports</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 
        * @Return : Void
        */

        function writeLine($line_arr)
        {
            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;
            }
            fwrite($this->fp,"<tr>");
            foreach($line_arr as $col)
                fwrite($this->fp,"<td class=xl24 width=64 >$col</td>");
            fwrite($this->fp,"</tr>");
        }

        /*
        * @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 : Coloumn Value
        * @Return : Void
        */
        function writeCol($value)
        {
            if($this->state!="OPENED")
            {
                $this->error="Error : Please open the file.";
                return false;
            }   
            fwrite($this->fp,"<td class=xl24 width=64 >$value</td>");
        }
    }


?>

3 个答案:

答案 0 :(得分:0)

我认为这应该有用

$ dat = date(&#34; d / m / Y&#34;);

$文件名=&#34;报告_(&#34; $ DAT&#34;)。XLS&#34 ;;

$ excel = new ExcelWriter($ filename);

答案 1 :(得分:0)

你的第一个问题是:

您没有将$ filename传递给Excel类Open方法,以便使用自定义字符串保存文件,即:Report_14 / 4 / 2014.xls

这样做:

  

$ filename =“Report_ $ dat.xls”;
  $ excel = new ExcelWriter($ filename);

其次:您无法使用包含无效字符的名称(例如“/”)保存文件,因此更改您调用php date()函数的拆分字符。

  

$ dat = date('d-m-y');

此外,关于将内容写入文件:

在尝试使用$ conn连接到数据库之前,您永远不会设置$ conn。如果在connection.php文件中完成此操作,则需要确保可以在调用它的代码范围内访问该变量。

然后我会更改行读数

  

如果($ SQL!=假)

  

oci_execute($ SQL);

并删除之前对oci_execute($ sql);

的调用

确保您有数据循环

最后,如果您没有关闭文件阅读器,则不会在文件中看到任何内容。

这是因为许多文件处理程序创建了文件,但是从不在每次“writeLine”调用中写入文件,因为这将需要许多昂贵的硬件IO调用。

因此它将所有这些行写入缓存,并且一旦关闭文件处理程序,它就会将内容写入文件&gt;

因此,您需要致电

  

$ excel-&GT;关闭();

你回音的地方

  

echo“文件下载成功”;

确保将数据保存到文件中。

答案 2 :(得分:0)

你可以试试这个

  

$ filename =“Report _”。$ dat。“。xls”;

     

$ excel = new ExcelWriter($ filename);