read_excel.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. set_time_limit(300);
  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 ($j == 'A' && str_contains($sheetName, "TI")) {
  85. $value = str_replace(".", "", $value);
  86. }
  87. array_push($row, $value);
  88. }
  89. }
  90. if (!$skip) {
  91. array_push($data, $row);
  92. $row_merge = str_repeat(", ?", $col_count);
  93. $sql = "BEGIN
  94. IF NOT EXISTS (SELECT [date] FROM [{$type}_Data] WHERE [date] = '{$date}' AND [EquipmentID] = '')
  95. BEGIN
  96. INSERT INTO [{$type}_Data] VALUES (? {$row_merge})
  97. END
  98. END";
  99. $stmt = sqlsrv_query($conn, $sql, $row);
  100. if ($stmt === false) {
  101. if (($errors = sqlsrv_errors()) != null) {
  102. foreach ($errors as $error) {
  103. echo "SQLSTATE: " . $error['SQLSTATE'] . "<br />";
  104. echo "code: " . $error['code'] . "<br />";
  105. echo "message: " . $error['message'] . "<br />";
  106. echo ($sql . "<br />");
  107. var_dump($row);
  108. }
  109. }
  110. }
  111. }
  112. }
  113. $excelData[$sheetName] = $data;
  114. }
  115. }
  116. if (isset($_SESSION['UserID'])) {
  117. $user_id = $_SESSION['UserID'];
  118. } else {
  119. $user_id = "0000";
  120. }
  121. $sql = "INSERT INTO [Update_Time] ([UserID]) VALUES (?);";
  122. $stmt = sqlsrv_query($conn, $sql, array($_SESSION['UserID']));
  123. if ($stmt === false) {
  124. if (($errors = sqlsrv_errors()) != null) {
  125. foreach ($errors as $error) {
  126. echo "SQLSTATE: " . $error['SQLSTATE'] . "<br />";
  127. echo "code: " . $error['code'] . "<br />";
  128. echo "message: " . $error['message'] . "<br />";
  129. echo ($sql . "<br />");
  130. var_dump($row);
  131. }
  132. }
  133. }
  134. header("Location: ../../Upload.php?status=success");
  135. } else {
  136. header("Location: ../../Upload.php?status=failed");
  137. }
  138. function check_sheet($sheetName, $equipment_data)
  139. {
  140. $equipment_types = ["SIS", "SID", "SB", "SM", "TI", "VG", "RB", "OW", "SP", "EP", "SS", "EXM", "ETi", "BS"];
  141. foreach ($equipment_types as $type) {
  142. if (str_contains($sheetName, $type)) {
  143. $key = array_search($type, array_column($equipment_data, 'type'));
  144. return $key;
  145. }
  146. }
  147. return null;
  148. }