本文实例为大家分享了PHPExcel导出Excel表的具体代码,供大家参考,具体内容如下
//创建新的PHPExcel对象
$objPHPExcel = new \PHPExcel();
$objProps = $objPHPExcel->getProperties();
//设置表头
$key = ord("A");
$key2 = ord("A");
$colum2 = '';
$objActSheet = $obj<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>Excel->getActiveSheet();
$obj<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>Excel->getActiveSheet()->getColumnDimension('A')->setWidth(25);
foreach($headArr as $v){
$colum = chr($key);
$obj<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>Excel->setActiveSheetIndex(0) ->setCellValue($colum2.$colum.'1',$v);
if($key < 90){
$key += 1;
}else{
$key = ord("A");
$colum2 = chr($key2);
$key2++;
}
}
//exit;
$column = 2;
foreach($data as $key => $rows){ //行写入
$span = ord("A");
$span2 = ord("A");
$j2 = '';
foreach($rows as $keyName=>$value){// 列写入
$j = chr($span);
//$objActSheet->setCellValue($j.$column,$value);
//把每个单元格设置成分文本类型
//dump($j2.$j.$column);
$objActSheet->setCellValueExplicit($j2.$j.$column,$value,\<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>Excel_Cell_DataType::TYPE_STRING);
if($span < 90){
$span += 1;
}else{
$span = ord("A");
$j2 = chr($span2);
$span2++;
}
}
$column++;
}
// exit;
$fileName = iconv("utf-8","gb2312",$fileName);
//重命名表
$objPHPExcel->getActiveSheet()->setTitle('Simple');
//设置活动单指数到第<a href="https://www.jb51.cc/tag/yige/" target="_blank" class="keywords">一个</a>表,所以Excel打开这是第<a href="https://www.jb51.cc/tag/yige/" target="_blank" class="keywords">一个</a>表
$obj<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>Excel->setActiveSheetIndex(0);
//将<a href="https://www.jb51.cc/tag/shuchu/" target="_blank" class="keywords">输出</a><a href="https://www.jb51.cc/tag/zhongdingxiang/" target="_blank" class="keywords">重定向</a>到<a href="https://www.jb51.cc/tag/yige/" target="_blank" class="keywords">一个</a>客户端web浏览器(Excel2007)
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header("Content-<a href="https://www.jb51.cc/tag/dis/" target="_blank" class="keywords">dis</a>position: attachment; filename=\"$fileName\"");
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,'Excel2007');
// if(!empty($_GET['excel'])){
// $objWriter->save('PHP://output'); //文件通过浏览器下载
// }else{
// $objWriter->save($fileName); //脚本方式运行,保存在当前目录
// }
$objWriter->save('PHP://output');
exit;
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程之家。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。