php中使用PHPExcel读写excel(xls)文件的方法

前端之家收集整理的这篇文章主要介绍了php中使用PHPExcel读写excel(xls)文件的方法前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

本文实例讲述了PHP中使用PHPExcel读写excel(xls)文件方法,非常实用。分享给大家供大家参考之用。具体方法如下:

很多PHP类库在读取中文的xls、csv文件时会有问题,网上找了下资料,发现PHPExcel类库好用,官网地址为:http://PHPexcel.codeplex.com/。现将PHPExcel读写Excel的方法分别叙述如下:

1、读取xls文件内容

PHP;"> getProperties()->setCreator("云舒") // ->setLastModifiedBy("云舒") // ->setTitle("产品URL导出") // ->setSubject("产品URL导出") // ->setDescription("产品URL导出") // ->setKeywords("产品URL导出"); $objPHPExcel->setActiveSheetIndex(0);
$cols = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
//设置www.jb51.cc<a href="https://www.jb51.cc/tag/biaoti/" target="_blank" class="keywords">标题</a>
for($i=0,$length=count($title); $i<$length; $i++) {
  //echo $cols{$i}.'1';
  $objPHPExcel->getActiveSheet()->setCellValue($cols{$i}.'1',$title[$i]);
}
//设置<a href="https://www.jb51.cc/tag/biaoti/" target="_blank" class="keywords">标题</a>样式
$titleCount = count($title);
$r = $cols{0}.'1';
$c = $cols{$titleCount}.'1';
$obj<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>Excel->getActiveSheet()->getStyle("$r:$c")->applyFromArray(
  array(
    'font'  => array(
      'bold'   => true
    ),'alignment' => array(
      'horizontal' => <a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>Excel_Style_Alignment::HORIZONTAL_RIGHT,),'borders' => array(
      'top'   => array(
        'style' => <a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>Excel_Style_Border::BORDER_THIN
      )
    ),'fill' => array(
      'type'    => <a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>Excel_Style_Fill::FILL_GRADIENT_LINEAR,'rotation'  => 90,'startcolor' => array(
        'argb' => 'FFA0A0A0'
      ),'endcolor'  => array(
        'argb' => 'FFFFFFFF'
      )
    )
  )
);

$i = 0;
foreach($data as $d) { //这里用foreach,<a href="https://www.jb51.cc/tag/zhichi/" target="_blank" class="keywords">支持</a>关联数组和数字索引数组
  $j = 0;
  foreach($d as $v) {  //这里用foreach,<a href="https://www.jb51.cc/tag/zhichi/" target="_blank" class="keywords">支持</a>关联数组和数字索引数组
    $obj<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>Excel->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 = <a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>Excel_IOFactory::createWriter($obj<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>Excel,'Excel5');
$objWriter->save('<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>://output');

}
$array = array(
array(1111,'名称','品牌','商品名','//www.jb51.cc'),array(1111,);
write_xls($array,array('商品id','供应商名称','URL'),'report');

?>

2、向xls文件内容

PHP;"> 'MysqL','DB_HOST'=>'localhost','DB_NAME'=>'test','DB_USER'=>'root','DB_PWD'=>'root','DB_PORT'=>'3306',); function getProductIdByName($name) { global $config; $id = false;
$<a href="https://www.jb51.cc/tag/MysqL/" target="_blank" class="keywords">MysqL</a>i = new <a href="https://www.jb51.cc/tag/MysqL/" target="_blank" class="keywords">MysqL</a>i($config['DB_HOST'],$config['DB_USER'],$config['DB_PWD'],$config['DB_NAME']);
if(<a href="https://www.jb51.cc/tag/MysqL/" target="_blank" class="keywords">MysqL</a>i_connect_error()) {  //兼容 < php5.2.9 OO way:$mysqli->connect_error
  die("连接失败,<a href="https://www.jb51.cc/tag/cuowu/" target="_blank" class="keywords">错误</a>码:".<a href="https://www.jb51.cc/tag/MysqL/" target="_blank" class="keywords">MysqL</a>i_connect_errno()."<a href="https://www.jb51.cc/tag/cuowu/" target="_blank" class="keywords">错误</a>信息:".<a href="https://www.jb51.cc/tag/MysqL/" target="_blank" class="keywords">MysqL</a>i_connect_error());
}
//设置连接<a href="https://www.jb51.cc/tag/shujuku/" target="_blank" class="keywords">数据库</a>的编码,不要忘了设置
$<a href="https://www.jb51.cc/tag/MysqL/" target="_blank" class="keywords">MysqL</a>i->set_charset("gbk");
//<a href="https://www.jb51.cc/tag/zhongwen/" target="_blank" class="keywords">中文</a>字符的编码要与<a href="https://www.jb51.cc/tag/shujuku/" target="_blank" class="keywords">数据库</a>一致,若没设置,结果为null
$name = iconv("utf-8","gbk//IGNORE",$name);
if($<a href="https://www.jb51.cc/tag/MysqL/" target="_blank" class="keywords">MysqL</a>i_stmt = $<a href="https://www.jb51.cc/tag/MysqL/" target="_blank" class="keywords">MysqL</a>i->prepare("select id from 137_product where name like ?")) {
  $<a href="https://www.jb51.cc/tag/MysqL/" target="_blank" class="keywords">MysqL</a>i_stmt->bind_param("s",$name);
  $<a href="https://www.jb51.cc/tag/MysqL/" target="_blank" class="keywords">MysqL</a>i_stmt->execute();
  $<a href="https://www.jb51.cc/tag/MysqL/" target="_blank" class="keywords">MysqL</a>i_stmt->bind_result($id);
  $<a href="https://www.jb51.cc/tag/MysqL/" target="_blank" class="keywords">MysqL</a>i_stmt->fetch();
  $<a href="https://www.jb51.cc/tag/MysqL/" target="_blank" class="keywords">MysqL</a>i_stmt->close();
}
$<a href="https://www.jb51.cc/tag/MysqL/" target="_blank" class="keywords">MysqL</a>i->close(); 
return $id;  //得到的是gbk码(同<a href="https://www.jb51.cc/tag/shujuku/" target="_blank" class="keywords">数据库</a>编码)

}
$id = getProductIdByName('%伊奈卫浴伊奈分体座便器%');
var_dump($id);
?>

希望本文所述对大家的PHP程序设计有所帮助

原文链接:https://www.f2er.com/php/23817.html

猜你在找的PHP相关文章