ImportExcel.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. namespace App\Admin\Actions\Post;
  3. use App\Model\TextLabel;
  4. use App\Model\XyZskDisease;
  5. use Encore\Admin\Actions\Action;
  6. use Illuminate\Http\Request;
  7. use Illuminate\Support\Facades\Storage;
  8. use PHPExcel_IOFactory;
  9. class ImportExcel extends Action
  10. {
  11. protected $selector = '.import-excel';
  12. public function handle(Request $request)
  13. {
  14. $oFile = $request->file('file');
  15. if(!$oFile){
  16. return $this->error('请选择上传文件');
  17. }
  18. Storage::disk('local')->put('text.csv',file_get_contents($oFile));
  19. $url = storage_path().'/app/text.csv';
  20. $file = fopen($url, 'r');
  21. if(!$file)
  22. {
  23. return $this->error('文件打开错误');
  24. }
  25. while ($data = fgetcsv($file)) { //每次读取CSV里面的一行内容
  26. array_walk($data, function(&$item){
  27. $item = mb_convert_encoding($item, "UTF-8", "GBK");
  28. });
  29. $list[] = $data;
  30. }
  31. fclose($file);
  32. foreach ($list as $k=>$v) {
  33. if($k == 0) continue;
  34. $data = [
  35. 'department_1' => $v[0],
  36. 'department_2' => $v[1],
  37. 'name' => $v[2],
  38. 'alias' => $v[3],
  39. 'clinicalFeature' => $v[4],
  40. 'diagnosis' => $v[5],
  41. 'treatment' => $v[6],
  42. 'regularMedication' => $v[7],
  43. 'pathogenesis' => $v[8],
  44. 'inspection' => $v[9],
  45. 'relevantOperation' => $v[10],
  46. 'laboratoryInspection' => $v[11],
  47. 'icd' => $v[12],
  48. 'etiology' => $v[13],
  49. 'auxiliaryExamination' => $v[14],
  50. 'antidiastole' => $v[15],
  51. 'prognosis' => $v[16],
  52. 'complicationsOverview' => $v[17],
  53. 'epidemiology' => $v[18],
  54. 'precaution' => $v[19],
  55. 'symptom' => $v[20],
  56. 'examination' => $v[21]
  57. ];
  58. XyZskDisease::query()->insert($data);
  59. }
  60. return $this->response()->success('Success message...')->refresh();
  61. }
  62. public function form()
  63. {
  64. $this->file('file', '请选择文件');
  65. }
  66. public function html()
  67. {
  68. return <<<HTML
  69. <a class="btn btn-sm btn-default import-excel">导入EXCEL</a>
  70. HTML;
  71. }
  72. }