<?php
use App\Services\UploadService;
use Maatwebsite\Excel\Facades\Excel;
class ExcelTest extends \TestCase
{
/**
* phpunit --filter testAdd tests/admin/AlbumTest.php
* author: costalong
* email: longqiuhong@163.com
*/
public function testRead()
{
$file = 'https://bucket-crm-1258058953.cos.ap-guangzhou.myqcloud.com/5b3be774c0eede35e264f6d740e5d8c8.xls';
$arr = explode('/',$file);
$file_name = "/tmp/".end($arr);
file_put_contents($file_name,file_get_contents($file));
Excel::load($file\_name, function($reader) {
$sheetCounts = $reader->getSheetCount();
if($sheetCounts > 1){
throw new \\Exception("只能1个sheet");
}
$reader = $reader->getSheet(0);
$res = $reader->toArray();
var\_dump($res);
$header = $res\[0\];
if($header\[0\] != '导入地区' || $header\[1\] != '姓名' || $header\[2\] != '微信' || $header\[3\] != '手机号码'){
throw new \\Exception("格式不规范");
}
});
unlink($file\_name);
}
/\*\*
\* author: costalong
\* email: longqiuhong@163.com
\*/
public function testWrite()
{
$cellData = \[
\['学号','姓名','成绩'\],
\['1000133333333333333333333333333333','AAAAA','99'\],
\['100023333333333333333333333','BBBBB','92'\],
\['10003','CCCCC','95'\],
\['10004','DDDDD','89'\],
\['10005','EEEEE','96'\],
\];
// Excel::create('学生成绩',function($excel) use ($cellData){
// $excel->sheet('score', function($sheet) use ($cellData){
// $sheet->rows($cellData);
// });
// })->export('xls');
$file\_name = 'test';
$res = Excel::create($file\_name,function($excel) use ($cellData){
$excel->sheet('score', function($sheet) use ($cellData){
$sheet->rows($cellData);
$sheet->setFontSize(15);
// Alignment
$style = array(
'alignment' => array(
'vertical' => \\PHPExcel\_Style\_Alignment::VERTICAL\_CENTER,
'horizontal' => \\PHPExcel\_Style\_Alignment::HORIZONTAL\_CENTER,
)
);
$sheet->getDefaultStyle()->applyFromArray($style);
$sheet->setWidth(array(
'A' => 50,
'B' => 30,
'C' => 15,
'D' => 10
));
$sheet->setHeight(array(
1 => 20,
2 => 20,
3 => 20,
4 => 20,
5 => 20
));
});
});
$res->store('xls');
// $file = base_path()."/storage/exports/".$file_name.".xls";
// $res =(new UploadService())->uploadFile($file,'businessCard/xls/export/'.$file_name.'.xls');
// dd($res);
// unlink($file);
}
}
手机扫一扫
移动阅读更方便
你可能感兴趣的文章