欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

php中使用PHPExcel讀寫excel(xls)文件的方法

 更新時間:2014年09月15日 09:06:52   投稿:shichen2014  
這篇文章主要介紹了php中使用PHPExcel讀寫excel(xls)文件的方法,phpExcel是常用的用于操作Excel的PHP類庫,應(yīng)用非常廣泛。需要的朋友可以參考下

本文實例講述了PHP中使用PHPExcel讀寫excel(xls)文件的方法,非常實用。分享給大家供大家參考之用。具體方法如下:

很多PHP類庫在讀取中文的xls、csv文件時會有問題,網(wǎng)上找了下資料,發(fā)現(xiàn)PHPExcel類庫好用,官網(wǎng)地址為:http://phpexcel.codeplex.com/?,F(xiàn)將PHPExcel讀寫Excel的方法分別敘述如下:

1、讀取xls文件內(nèi)容

<?php
  //向xls文件寫入內(nèi)容
  error_reporting(E_ALL);
  ini_set('display_errors', TRUE);  
  include 'Classes/PHPExcel.php';      
  include 'Classes/PHPExcel/IOFactory.php'; 
  //$data:xls文件內(nèi)容正文
  //$title:xls文件內(nèi)容標(biāo)題
  //$filename:導(dǎo)出的文件名
  //$data和$title必須為utf-8碼,否則會寫入FALSE值
  function write_xls($data=array(), $title=array(), $filename='report'){
    $objPHPExcel = new PHPExcel();
    //設(shè)置文檔屬性,設(shè)置中文會產(chǎn)生亂碼,需要轉(zhuǎn)換成utf-8格式?。?
    // $objPHPExcel->getProperties()->setCreator("云舒")
               // ->setLastModifiedBy("云舒")
               // ->setTitle("產(chǎn)品URL導(dǎo)出")
               // ->setSubject("產(chǎn)品URL導(dǎo)出")
               // ->setDescription("產(chǎn)品URL導(dǎo)出")
               // ->setKeywords("產(chǎn)品URL導(dǎo)出");
    $objPHPExcel->setActiveSheetIndex(0);
    
    $cols = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
    //設(shè)置www.dbjr.com.cn標(biāo)題
    for($i=0,$length=count($title); $i<$length; $i++) {
      //echo $cols{$i}.'1';
      $objPHPExcel->getActiveSheet()->setCellValue($cols{$i}.'1', $title[$i]);
    }
    //設(shè)置標(biāo)題樣式
    $titleCount = count($title);
    $r = $cols{0}.'1';
    $c = $cols{$titleCount}.'1';
    $objPHPExcel->getActiveSheet()->getStyle("$r:$c")->applyFromArray(
      array(
        'font'  => array(
          'bold'   => true
        ),
        'alignment' => array(
          'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_RIGHT,
        ),
        'borders' => array(
          'top'   => array(
            'style' => PHPExcel_Style_Border::BORDER_THIN
          )
        ),
        'fill' => array(
          'type'    => PHPExcel_Style_Fill::FILL_GRADIENT_LINEAR,
          'rotation'  => 90,
          'startcolor' => array(
            'argb' => 'FFA0A0A0'
          ),
          'endcolor'  => array(
            'argb' => 'FFFFFFFF'
          )
        )
      )
    );
    
    $i = 0;
    foreach($data as $d) { //這里用foreach,支持關(guān)聯(lián)數(shù)組和數(shù)字索引數(shù)組
      $j = 0;
      foreach($d as $v) {  //這里用foreach,支持關(guān)聯(lián)數(shù)組和數(shù)字索引數(shù)組
        $objPHPExcel->getActiveSheet()->setCellValue($cols{$j}.($i+2), $v);
        $j++;
      }
       $i++;
    }
    // 生成2003excel格式的xls文件
    header('Content-Type: application/vnd.ms-excel');
    header('Content-Disposition: attachment;filename="'.$filename.'.xls"');
    header('Cache-Control: max-age=0');

    $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
    $objWriter->save('php://output');
  }
  $array = array(
    array(1111,'名稱','品牌','商品名','http://www.dbjr.com.cn'),
    array(1111,'名稱','品牌','商品名','http://www.dbjr.com.cn'),
    array(1111,'名稱','品牌','商品名','http://www.dbjr.com.cn'),
    array(1111,'名稱','品牌','商品名','http://www.dbjr.com.cn'),
    array(1111,'名稱','品牌','商品名','http://www.dbjr.com.cn'),
  );
  write_xls($array,array('商品id','供應(yīng)商名稱','品牌','商品名','URL'),'report');
  
?>

2、向xls文件寫內(nèi)容

<?php
  //獲取數(shù)據(jù)庫數(shù)據(jù)(mysqli預(yù)處理學(xué)習(xí))
  $config = array(
    'DB_TYPE'=>'mysql',
    'DB_HOST'=>'localhost',
    'DB_NAME'=>'test',
    'DB_USER'=>'root',
    'DB_PWD'=>'root',
    'DB_PORT'=>'3306',
  );
  function getProductIdByName($name) {
    global $config;
    $id = false;
    
    $mysqli = new mysqli($config['DB_HOST'], $config['DB_USER'], $config['DB_PWD'], $config['DB_NAME']);
    if(mysqli_connect_error()) {  //兼容 < php5.2.9 OO way:$mysqli->connect_error
      die("連接失敗,錯誤碼:".mysqli_connect_errno()."錯誤信息:".mysqli_connect_error());
    }
    //設(shè)置連接數(shù)據(jù)庫的編碼,不要忘了設(shè)置
    $mysqli->set_charset("gbk");
    //中文字符的編碼要與數(shù)據(jù)庫一致,若沒設(shè)置,結(jié)果為null
    $name = iconv("utf-8", "gbk//IGNORE", $name);
    if($mysqli_stmt = $mysqli->prepare("select id from 137_product where name like ?")) {
      $mysqli_stmt->bind_param("s", $name);
      $mysqli_stmt->execute();
      $mysqli_stmt->bind_result($id);
      $mysqli_stmt->fetch();
      $mysqli_stmt->close();
    }
    $mysqli->close(); 
    return $id;  //得到的是gbk碼(同數(shù)據(jù)庫編碼)
  }  
  $id = getProductIdByName('%伊奈衛(wèi)浴伊奈分體座便器%');
  var_dump($id);
?>

希望本文所述對大家的PHP程序設(shè)計有所幫助

相關(guān)文章

最新評論