环境
php7.4+hyperf3
composer require phpoffice/phpspreadsheet
代码
class IndexController extends AbstractController
{
private $picture ;
public function index ( ) {
$res_data [ ] = [
"robot" => '哈哈机器人' ,
"order" => 'TES001' ,
"user" => '人01' ,
"start_time" => '2024-02-01 00:12:13' ,
"end_time" => '2024-02-01 20:12:13' ,
"time" => 120 ,
"soc" => '12' ,
"billing" => '1.1' ,
] ;
return $this -> exportData ( $res_data ) ;
}
private function exportData ( $res_data )
{
$rows = $res_data ;
$newExcel = new Spreadsheet ( ) ;
$objSheet = $newExcel -> getActiveSheet ( ) ;
$objSheet -> setTitle ( '自动生成随机充电订单表' ) ;
$newExcel -> getActiveSheet ( ) -> getColumnDimension ( 'A' ) -> setAutoSize ( true ) ;
$newExcel -> getActiveSheet ( ) -> getColumnDimension ( 'B' ) -> setAutoSize ( true ) ;
$newExcel -> getActiveSheet ( ) -> getColumnDimension ( 'C' ) -> setAutoSize ( true ) ;
$newExcel -> getActiveSheet ( ) -> getColumnDimension ( 'D' ) -> setAutoSize ( true ) ;
$newExcel -> getActiveSheet ( ) -> getColumnDimension ( 'E' ) -> setAutoSize ( true ) ;
$newExcel -> getActiveSheet ( ) -> getColumnDimension ( 'F' ) -> setAutoSize ( true ) ;
$newExcel -> getActiveSheet ( ) -> getColumnDimension ( 'G' ) -> setAutoSize ( true ) ;
$newExcel -> getActiveSheet ( ) -> getColumnDimension ( 'H' ) -> setAutoSize ( true ) ;
$objSheet -> setCellValue ( 'A1' , '站点名称' )
-> setCellValue ( 'B1' , '订单号' )
-> setCellValue ( 'C1' , '用户' )
-> setCellValue ( 'D1' , '开始时间' )
-> setCellValue ( 'E1' , '结束时间' )
-> setCellValue ( 'F1' , '时长' )
-> setCellValue ( 'G1' , '充电量(KW)' )
-> setCellValue ( 'H1' , '计费(元)' ) ;
$k = 1 ;
foreach ( $rows as & $val ) {
$k = $k + 1 ;
$objSheet -> setCellValue ( 'A' . $k , $val [ 'robot' ] )
-> setCellValue ( 'B' . $k , $val [ 'order' ] )
-> setCellValue ( 'C' . $k , $val [ 'user' ] )
-> setCellValue ( 'D' . $k , $val [ 'start_time' ] )
-> setCellValue ( 'E' . $k , $val [ 'end_time' ] )
-> setCellValue ( 'F' . $k , $val [ 'time' ] )
-> setCellValue ( 'G' . $k , $val [ 'soc' ] )
-> setCellValue ( 'H' . $k , $val [ 'billing' ] ) ;
}
[ $bool , $path ] = $this -> picture -> save ( $newExcel , '自动生成随机充电订单表_' . date ( "YmdHis" ) , true , false ) ;
if ( $bool ) {
return response_api ( true , '获取成功' , [ 'path' => $path [ 'path' ] ] ) ;
} else {
return response_api ( false , $path , [ ] , StateCode :: error_confirm ) ;
}
}
}
class Picture
<?php
declare ( strict_types= 1 ) ;
namespace App\ utils ;
use App\ Logic\ v1\ CustomerLogic ;
use League\ Flysystem\ Filesystem ;
use PhpOffice\ PhpSpreadsheet\ IOFactory ;
class Picture
{
const CLOUD_SAVE_PATH = 'saas/uploads/' ;
function save ( $newExcel , string $fileName , bool $uploadCloud = false , bool $deleteLocal = true )
{
$fileName = $fileName . '.xlsx' ;
$path = '/storage/excel/' . $fileName ;
$fullPath = BASE_PATH . $path ;
$writer = IOFactory :: createWriter ( $newExcel , 'Xlsx' ) ;
$writer -> save ( $fullPath ) ;
$newExcel -> disconnectWorksheets ( ) ;
if ( ! $uploadCloud ) {
return [ true , [ 'full_path' => $fullPath , 'path' => $path ] ] ;
} else {
$url = env ( 'CDNURL' , 'http://cdn.***.cn' ) . "/" . $this -> uploadImgFile ( $path , null , $deleteLocal ) ;
return ! $path ? [ false , '上传到云端失败' ] : [ true , [ 'full_path' => $fullPath , 'path' => $url ] ] ;
}
}
public function uploadImg ( $file , $filesystem , $type = 0 )
{
if ( $type == 1 ) {
$stream = fopen ( $file , 'r+' ) ;
$filename = ".xlsx" ;
} else {
$stream = fopen ( $file -> getRealPath ( ) , 'r+' ) ;
$filename = $file -> getClientFilename ( ) ;
}
$rename = date ( 'Ym' ) . "/" . uniqid ( ) . '.' . explode ( '.' , $filename ) [ 1 ] ;
$a = $filesystem -> writeStream ( 'saas/uploads/' . $rename , $stream ) ;
return env ( 'CDNURL' , 'http://cdn.***.cn' ) . '/saas/uploads/' . $rename ;
}
public function uploadImgFile ( string $filePath , string $cloudPath = null , bool $deleteLocal = false , string $fileName = '' )
{
try {
$filePath = BASE_PATH . $filePath ;
$ymdPath = date ( 'Y-m' ) ;
if ( ! $cloudPath ) {
$cloudPath = self :: CLOUD_SAVE_PATH ;
}
$startIndex = strrpos ( $filePath , '.' ) ;
$extension = substr ( $filePath , $startIndex ) ;
$stream = fopen ( $filePath , 'r+' ) ;
$fileSystem = make ( Filesystem :: class ) ;
$fileName = $ymdPath . '/' . ( ( ! empty ( $fileName ) ) ? $fileName : $this -> createFileName ( ) ) . $extension ;
$fileSystem -> writeStream ( $cloudPath . $fileName , $stream ) ;
if ( is_resource ( $stream ) ) {
fclose ( $stream ) ;
}
if ( $deleteLocal ) {
@unlink ( $filePath ) ;
}
return config ( 'oss_base_path' ) . $cloudPath . $fileName ;
} catch ( \ Exception $exception ) {
return '' ;
}
}
private function createFileName ( )
{
return md5 ( date ( 'YmdHis' ) . uniqid ( ) . mt_rand ( 1000 , 9999 ) ) ;
}
}
导出效果类似如下: