1. 程式人生 > >php 輸出表格

php 輸出表格

mat 讀取 AC name ica 保險 sheet char param

/* 
*處理Excel導出 
*@param $datas array 設置表格數據 
*@param $titlename string 設置head 
*@param $title string 設置表頭 
*/ 
public function excelData($datas,$titlename,$title,$filename){ 
    $str = "<html xmlns:o=\"urn:schemas-microsoft-com:office:office\"\r\nxmlns:x=\"urn:schemas-microsoft-com:office:excel\"\r\nxmlns=\"http://www.w3.org/TR/REC-html40\">\r\n<head>\r\n<meta http-equiv=Content-Type content=\"text/html; charset=utf-8\">\r\n</head>\r\n<body>"; 
    $str .="<table border=1><head>".$titlename."</head>"; 
    $str .= $title; 
    foreach ($datas  as $key=> $rt ) 
    { 
        $str .= "<tr>"; 
        foreach ( $rt as $k => $v ) 
        { 
            $str .= "<td>{$v}</td>"; 
        } 
        $str .= "</tr>\n"; 
    } 
    $str .= "</table></body></html>"; 
    header( "Content-Type: application/vnd.ms-excel; name=‘excel‘" ); 
    header( "Content-type: application/octet-stream" ); 
    header( "Content-Disposition: attachment; filename=".$filename ); 
    header( "Cache-Control: must-revalidate, post-check=0, pre-check=0" ); 
    header( "Pragma: no-cache" ); 
    header( "Expires: 0" ); 
    exit( $str ); 
} 

==========

$dataResult = array();      //todo:導出數據(自行設置) 
$headTitle = "XX保險公司 優惠券贈送記錄"; 
$title = "優惠券記錄"; 
$headtitle= "<tr style=‘height:50px;border-style:none;><th border=\"0\" style=‘height:60px;width:270px;font-size:22px;‘ colspan=‘11‘ >{$headTitle}</th></tr>"; 
$titlename = "<tr> 
               <th style=‘width:70px;‘ >合作商戶</th> 
               <th style=‘width:70px;‘ >會員卡號</th> 
               <th style=‘width:70px;‘>車主姓名</th> 
               <th style=‘width:150px;‘>手機號</th> 
               <th style=‘width:70px;‘>車牌號</th> 
               <th style=‘width:100px;‘>優惠券類型</th> 
               <th style=‘width:70px;‘>優惠券名稱</th> 
               <th style=‘width:70px;‘>優惠券面值</th> 
               <th style=‘width:70px;‘>優惠券數量</th> 
               <th style=‘width:70px;‘>贈送時間</th> 
               <th style=‘width:90px;‘>截至有效期</th> 
           </tr>"; 
           $filename = $title.".xls"; 
       $this->excelData($dataResult,$titlename,$headtitle,$filename); 

====================

require_once ‘phpexcel/Classes/PHPExcel.php‘;
require_once ‘phpexcel/Classes/PHPExcel/IOFactory.php‘;
require_once ‘phpexcel/Classes/PHPExcel/Reader/Excel5.php‘;
$objReader = PHPExcel_IOFactory::createReader(‘Excel5‘);//use excel2007 for 2007 format 
$objPHPExcel = $objReader->load($filename); //$filename可以是上傳的文件,或者是指定的文件
$sheet = $objPHPExcel->getSheet(0); 
$highestRow = $sheet->getHighestRow(); // 取得總行數 
$highestColumn = $sheet->getHighestColumn(); // 取得總列數
$k = 0; 
 
//循環讀取excel文件,讀取一條,插入一條
for($j=2;$j<=$highestRow;$j++)
{
 
$a = $objPHPExcel->getActiveSheet()->getCell("A".$j)->getValue();//獲取A列的值
$b = $objPHPExcel->getActiveSheet()->getCell("B".$j)->getValue();//獲取B列的值
$sql = "INSERT INTO table VALUES(".$a.",".$b.")";
mysql_query($sql);
 

  

php 輸出表格