12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879 |
- <?php
- namespace App\Admin\Actions\Post;
- use App\Model\TextLabel;
- use Encore\Admin\Actions\Action;
- use Illuminate\Http\Request;
- use Illuminate\Support\Facades\Storage;
- use PHPExcel_IOFactory;
- class ImportExcel extends Action
- {
- protected $selector = '.import-excel';
- public function handle(Request $request)
- {
- $oFile = $request->file('file');
- Storage::disk('local')->put('text.xlsx',file_get_contents($oFile));
- $url = storage_path().'/app/text.xlsx';
- $excel = PHPExcel_IOFactory::load($url);
- $sheet = $excel->getSheet(0);
- $row_num = $sheet->getHighestRow();
- $data = [];
- for ($row = 2;$row <= $row_num;$row++){
- $text = $excel->getActiveSheet()->getCell("A".$row)->getValue();
- $length = mb_strlen($text);
- if ($length <= 510){
- $data[]['text'] = $text;
- }elseif ($length > 510 && $length <= 1020){
- $textArray = preg_split('/。/',$text);
- $count = count($textArray);
- $pos = ceil($count / 2);
- $text1 = '';
- $text2 = '';
- foreach ($textArray as $key => $item){
- if ($key <= $pos){
- $text1 .= $item.'。';
- }else{
- $text2 .= $item.'。';
- }
- }
- $data[]['text'] = $text1;
- $data[]['text'] = $text2;
- }elseif ($length > 1020){
- $textArray = preg_split('/。/',$text);
- $count = count($textArray);
- $pos = ceil($count / 3);
- $text1 = '';
- $text2 = '';
- $text3 = '';
- foreach ($textArray as $key => $item){
- if ($key <= $pos){
- $text1 .= $item;
- }elseif($key <= $pos*2){
- $text2 .= $item.'。';
- }else{
- $text3 .= $item.'。';
- }
- }
- $data[]['text'] = $text1;
- $data[]['text'] = $text2;
- $data[]['text'] = $text3;
- }
- }
- TextLabel::query()->insert($data);
- return $this->response()->success('Success message...')->refresh();
- }
- public function form()
- {
- $this->file('file', '请选择文件');
- }
- public function html()
- {
- return <<<HTML
- <a class="btn btn-sm btn-default import-excel">导入EXCEL</a>
- HTML;
- }
- }
|