您好,登錄后才能下訂單哦!
composer安裝phpexecl
composer require phpoffice/phpexcel
<?php
namespace Potting;
use PHPExcel;
use PHPExcel_IOFactory;
class Excel
{
static public function export($headArr,$data){
$path='execl/';
$fileName=time();
if(!file_exists($path)){
mkdir($path,0777,true);
}
$objPHPExcel = new PHPExcel();
$objPHPExcel->getProperties();
$key = ord("A"); // 設置表頭
foreach ($headArr as $v) {
$colum = chr($key);
$objPHPExcel->setActiveSheetIndex(0)->setCellValue($colum . '1', $v);
$objPHPExcel->setActiveSheetIndex(0)->setCellValue($colum . '1', $v);
$key += 1;
}
$column = '2';
$objActSheet = $objPHPExcel->getActiveSheet();
foreach ($data as $key => $rows) { // 行寫入
$span = ord("A");
foreach ($rows as $keyName => $value) { // 列寫入
$objActSheet->setCellValue(chr($span) . $column, $value);
$span++;
}
$column++;
}
$fileName = $fileName.".xls";
$objActSheet = PHPExcel_IOFactory::createWriter($objPHPExcel,'Excel2007');
//保存xls
$objActSheet->save($path.$fileName);
//返回路徑
return $path.$fileName;
}
}
<?php
namespace Potting;
use PHPExcel;
use PHPExcel_IOFactory;
class Excel
{
static public function export($fileName,$headArr,$data){
// $path='execl/';
// $fileName=time();
// if(!file_exists($path)){
// mkdir($path,0777,true);
// }
$objPHPExcel = new PHPExcel();
$objPHPExcel->getProperties();
$key = ord("A"); // 設置表頭
foreach ($headArr as $v) {
$colum = chr($key);
$objPHPExcel->setActiveSheetIndex(0)->setCellValue($colum . '1', $v);
$objPHPExcel->setActiveSheetIndex(0)->setCellValue($colum . '1', $v);
$key += 1;
}
$column = '2';
$objActSheet = $objPHPExcel->getActiveSheet();
foreach ($data as $key => $rows) { // 行寫入
$span = ord("A");
foreach ($rows as $keyName => $value) { // 列寫入
$objActSheet->setCellValue(chr($span) . $column, $value);
$span++;
}
$column++;
}
$fileName = $fileName.".xls";
$fileName = iconv("utf-8", "gb2312", $fileName); // 重命名表
$objPHPExcel->setActiveSheetIndex(0); // 設置活動單指數到第一個表,所以Excel打開這是第一個表
header('Content-Type: application/vnd.ms-excel');
header("Content-Disposition: p_w_upload;filename='$fileName'");
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output'); // 文件通過瀏覽器下載
exit();
//$fileName = $fileName.".xls";
//$objActSheet = PHPExcel_IOFactory::createWriter($objPHPExcel,'Excel2007');
//保存xls
//$objActSheet->save($path.$fileName);
//返回路徑
//return $path.$fileName;
}
}
public function index()
{
$fileName='測試';
$header=['表頭A','表頭B','表頭C'];
$data=[
['嘿嘿','heihei','2321312'],
['嘿嘿','heihei','2321312'],
['哈哈','haha','2321312']
];
$url=Excel::export($fileName,$header,$data);
var_dump($url);die();
}
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。