read_excel.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. include($_SERVER['DOCUMENT_ROOT']."/Authorization/script/php/permission/check_right.php");
  3. include("./API/equipment.php");
  4. include("./connect_sql.php");
  5. require '../../assets/vendor/autoload.php';
  6. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  7. use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
  8. if (isset($_FILES["excel"])) {
  9. if (0 < $_FILES['excel']['error']) {
  10. echo 'Error: ' . $_FILES['excel']['error'] . '<br>';
  11. } else {
  12. $filepath = 'uploads/' . $_FILES['excel']['name'];
  13. move_uploaded_file($_FILES['excel']['tmp_name'], $filepath);
  14. $projectName = $_POST['projectName'];
  15. $constructionName = $_POST['constructionName'];
  16. }
  17. $excelData = array();
  18. $file = $filepath;
  19. $extension = pathinfo($file, PATHINFO_EXTENSION);
  20. if ('csv' == $extension) {
  21. $reader = new \PhpOffice\PhpSpreadsheet\Reader\Csv();
  22. } else if ('xls' == $extension) {
  23. $reader = new \PhpOffice\PhpSpreadsheet\Reader\Xls();
  24. } else {
  25. $reader = new \PhpOffice\PhpSpreadsheet\Reader\Xlsx();
  26. }
  27. $reader->setReadDataOnly(true);
  28. $reader->setReadEmptyCells(true);
  29. $spreadsheet = $reader->load($file);
  30. $sheetCount = $spreadsheet->getSheetCount();
  31. $header = $spreadsheet->getSheetByName("表頭");
  32. $date_value = $header->getCell('J2')->getValue();
  33. $timestamp = \PhpOffice\PhpSpreadsheet\Shared\Date::excelToTimestamp($date_value);
  34. $date = date('Y/m/d', $timestamp);
  35. for ($s = 0; $s < $sheetCount; $s++) {
  36. $data = array();
  37. $sheet = $spreadsheet->getSheet($s);
  38. $sheetName = $spreadsheet->getSheetNames()[$s];
  39. if (array_search($sheetName, $not_read) === false) {
  40. //echo ("資料表:" . $sheetName . " ");
  41. $cellCollection = $sheet->getCellCollection();
  42. $column = $cellCollection->getHighestRowAndColumn();
  43. $index_key = check_sheet($sheetName, $equipment_data);
  44. $type = $equipment_data[$index_key]["type"]; //儀器類型
  45. $startC = $equipment_data[$index_key]["startC"]; //起始欄位 EX:A
  46. $endC = $equipment_data[$index_key]["endC"]; //結尾欄位 EX:H
  47. $skipC = $equipment_data[$index_key]["skipC"]; //跳過欄位 EX:G
  48. $startR = $equipment_data[$index_key]["startR"]; //起始列數 EX:10
  49. $endR = $equipment_data[$index_key]["endR"] !== 0 ? $equipment_data[$index_key]["endR"] : $column['row']; //結尾列數 若為0則總列數
  50. for ($i = $startR; $i <= $endR; $i++) {
  51. $skip = false;
  52. $row = array();
  53. $col_count = 0;
  54. array_push($row, $date);
  55. if (str_contains($sheetName, "SIS") || str_contains($sheetName, "SID")) { //若為SIS取出儀器名稱
  56. if(str_contains($sheetName,"區")){
  57. $equipment_name = explode("區", $sheetName)[1];
  58. }
  59. if(str_contains($sheetName,"(")){
  60. $temp = explode("(", $sheetName);
  61. $equipment_name = $temp[0].$temp[1];
  62. }
  63. $equipment_name = str_replace(" ", "", $equipment_name);
  64. $col_count++;
  65. array_push($row, $equipment_name);
  66. }
  67. for ($j = $startC; $j <= $endC; $j++) {
  68. if ($skipC != $j) {
  69. $col_count++;
  70. $key = $j . $i;
  71. $value = $sheet->getCell($key)->getCalculatedValue();
  72. if ((str_contains($sheetName, "SIS") || str_contains($sheetName, "SID")) && $j == 'C' && $value == null) { //若為SIS且C行為空,則跳過
  73. $skip = true;
  74. break;
  75. } else if ($j == 'A' && (!str_contains($value, "-") || str_contains($value, "說明"))) { //若儀器資料不含'-'且含'說明'則跳過
  76. $skip = true;
  77. break;
  78. } else if ($j == 'A' && str_contains($sheetName, "TI")) {
  79. $value = str_replace(".", "", $value);
  80. }
  81. array_push($row, $value);
  82. }
  83. }
  84. if (!$skip) {
  85. array_push($data, $row);
  86. $row_merge = str_repeat(", ?", $col_count);
  87. $sql = "INSERT INTO [{$type}_Data] VALUES (? {$row_merge});";
  88. $stmt = sqlsrv_query($conn, $sql, $row);
  89. if ($stmt === false) {
  90. if (($errors = sqlsrv_errors()) != null) {
  91. foreach ($errors as $error) {
  92. echo "SQLSTATE: " . $error['SQLSTATE'] . "<br />";
  93. echo "code: " . $error['code'] . "<br />";
  94. echo "message: " . $error['message'] . "<br />";
  95. echo ($sql . "<br />");
  96. var_dump($row);
  97. }
  98. }
  99. }
  100. }
  101. }
  102. $excelData[$sheetName] = $data;
  103. }
  104. }
  105. if (isset($_SESSION['UserID'])) {
  106. $user_id = $_SESSION['UserID'];
  107. } else {
  108. $user_id = "0000";
  109. }
  110. $sql = "INSERT INTO [Update_Time] ([UserID]) VALUES (?);";
  111. $stmt = sqlsrv_query($conn, $sql, array($_SESSION['UserID']));
  112. if ($stmt === false) {
  113. if (($errors = sqlsrv_errors()) != null) {
  114. foreach ($errors as $error) {
  115. echo "SQLSTATE: " . $error['SQLSTATE'] . "<br />";
  116. echo "code: " . $error['code'] . "<br />";
  117. echo "message: " . $error['message'] . "<br />";
  118. echo ($sql . "<br />");
  119. var_dump($row);
  120. }
  121. }
  122. }
  123. header("Location: ../../Upload.php?status=success");
  124. }else{
  125. header("Location: ../../Upload.php?status=failed");
  126. }
  127. function check_sheet($sheetName, $equipment_data)
  128. {
  129. $equipment_types = ["SIS", "SID", "SB", "SM", "TI", "VG", "RB", "OW", "SP", "EP", "SS", "EXM", "ETi", "BS"];
  130. foreach ($equipment_types as $type) {
  131. if (str_contains($sheetName, $type)) {
  132. $key = array_search($type, array_column($equipment_data, 'type'));
  133. return $key;
  134. }
  135. }
  136. return null;
  137. }