excel表格生成

 

require(DT_ROOT.'/include/excel_writer.class.php');

case 'excel'; 
  //exit("ddddddd");
  
 
   //$title= $_POST['title'];
   //exit('ddd :'.$title);
     //$datename="contract_".get_date('YmdHis',PHP_TIME);
      $datename="company_".get_date('His',time());   //下载表名中时间
     $outputFileName = DT_ROOT.'/api/excel/'.$datename.'.xls';   //表名全称
    // EXIT($outputFileName);
   
         $archive = array();
         $archive[]="<b>客户编号</b>";
   $archive[]="<b>供应商名称</b>";
   $archive[]="<b>联系人</b>";
   $archive[]="<b>手机</b>";
   $archive[]="<b>业务类型</b>";
   $archive[]="<b>来源</b>";
   $archive[]="<b>业务员</b>";

 


   $archive[]="<b>发布时间</b>";
   $content = array();
   $content[] = $archive;
    
  
     
     //$lists = $do->get_excel();
     //$sql = "SELECT * FROM ts_crm_supplier WHERE itemid>0";
     //print_r($lists);
     //exit("fff");
   
   $result=$db->query("SELECT * FROM ts_crm_supplier WHERE itemid>0"); 
   //print_r($result);
       //exit("dddddd");
     
   while ($row = $db->fetch_array($result)) { 
          $archive = array();
    $archive[]=$row['itemid'];
    $archive[]=$row['title'];
    $archive[]=$row['contact'];
    $archive[]=$row['phone'];
    $archive[]=$row['suppliertype'];
    $archive[]=$row['supplierfrom'];
    $archive[]=$row['userid'];
    $archive[]=str_replace("-",".",$row['addtime']);
    $content[] = $archive;
        /*
    echo "<pre>";
  print_r($archive);
  echo "<pre>";
  */
  
   }
  
  
   /* echo "<pre>";
  print_r($content);
  echo "<pre>";
   exit("ddd");*/
  //pritn_r($content);
  //exit("FFFFFF");
 $excel = new ExcelWriter($outputFileName);
 if($excel==false)
  echo $excel->error;
  
 foreach($content as $v){
  
  /*echo "<pre>";
  print_r($v);
  echo "<pre>";*/
  $excel->writeLine($v);
 }
 //exit('ddddddddd');
 $excel->sendfile($outputFileName);
 //$excel->
 
 
  //require_once 'module/crm/excel.php';  
  break;
  

 

/******************引用文件***********************/

 

<?php

/*
 [Office 515158] (C) 2009-2012 天生创想 Inc.
 $Id: excel_writer.php 1209087 2012-01-08 08:58:28Z baiwei.jiang $
*/

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($file));
   return $this->fp;
  }
  function sendfile($filename){
 header('Content-type: application/vnd.ms-excel');
 header("Content-Disposition: attachment; filename=".$filename);
 readfile($filename);
 exit();
 }
  /* @Params : Void
  *  @return : Void
  * This function write the header of Excel file.
  */
         
  function GetHeader($file)
  {
   $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/html4/loose.dtd">
    <head>
    <meta http-equiv=Content-Type content="text/html; charset=utf-8">
    <meta name=ProgId content=Excel.Sheet>
    <!--[if gte mso 9]><xml>
     <o:DocumentProperties>
      <o:LastAuthor>Rising</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>Sheet1</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 x:num>$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>");
  }
  /*
  * @Params : Void
  * @Return : Void
  */
  function writeSingleRow($value)
  {
   if($this->state!="OPENED")
   {
    $this->error="Error : Please open the file.";
    return false;
   }
   if($this->newRow==false)
    fwrite($this->fp,"<tr><td>".$value."</td></tr>");
  }
}
?>

转载于:https://my.oschina.net/u/2310017/blog/484477

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值