ImportExcel.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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->response()->error('请选择上传文件');
  17. }
  18. try {
  19. Storage::disk('local')->put('text.csv', file_get_contents($oFile));
  20. $url = storage_path() . '/app/text.csv';
  21. $file = fopen($url, 'r');
  22. if (!$file) {
  23. return $this->response()->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()->updateOrInsert(['name' => $v[2]], $data);
  59. }
  60. }catch (\Exception $e){
  61. $this->response()->error('上传失败,确认文件格式或类型!!');
  62. }
  63. return $this->response()->success('Success message...')->refresh();
  64. }
  65. public function form()
  66. {
  67. $this->file('file', '请选择文件');
  68. }
  69. public function html()
  70. {
  71. return <<<HTML
  72. <a class="btn btn-sm btn-default import-excel">导入EXCEL</a>
  73. HTML;
  74. }
  75. }