read_excel.php 7.1 KB

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