read_excel.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. $equipment_name = "";
  36. for ($s = 0; $s < $sheetCount; $s++) {
  37. $data = array();
  38. $sheet = $spreadsheet->getSheet($s);
  39. $sheetName = $spreadsheet->getSheetNames()[$s];
  40. if (array_search($sheetName, $not_read) === false) {
  41. //echo ("資料表:" . $sheetName . " ");
  42. $cellCollection = $sheet->getCellCollection();
  43. $column = $cellCollection->getHighestRowAndColumn();
  44. $index_key = check_sheet($sheetName, $equipment_data);
  45. $type = $equipment_data[$index_key]["type"]; //儀器類型
  46. $startC = $equipment_data[$index_key]["startC"]; //起始欄位 EX:A
  47. $endC = $equipment_data[$index_key]["endC"]; //結尾欄位 EX:H
  48. $skipC = $equipment_data[$index_key]["skipC"]; //跳過欄位 EX:G
  49. $startR = $equipment_data[$index_key]["startR"]; //起始列數 EX:10
  50. $endR = $equipment_data[$index_key]["endR"] !== 0 ? $equipment_data[$index_key]["endR"] : $column['row']; //結尾列數 若為0則總列數
  51. for ($i = $startR; $i <= $endR; $i++) {
  52. $skip = false;
  53. $first = true;
  54. $row = array();
  55. $col_count = 0;
  56. array_push($row, $date);
  57. if (str_contains($sheetName, "SIS") || str_contains($sheetName, "SID")) { //若為SIS取出儀器名稱
  58. if (str_contains($sheetName, "區")) {
  59. $equipment_name = explode("區", $sheetName)[1];
  60. }
  61. if (str_contains($sheetName, "(")) {
  62. $temp = explode("(", $sheetName);
  63. $equipment_name = $temp[0] . $temp[1];
  64. }
  65. $equipment_name = str_replace(" ", "", $equipment_name);
  66. $col_count++;
  67. array_push($row, $equipment_name);
  68. }
  69. for ($j = $startC; $j <= $endC; $j++) {
  70. if ($skipC != $j) {
  71. if($first){
  72. $first = false;
  73. }
  74. $col_count++;
  75. $key = $j . $i;
  76. $value = $sheet->getCell($key)->getCalculatedValue();
  77. if ((str_contains($sheetName, "SIS") || str_contains($sheetName, "SID")) && $j == 'C' && $value == null) { //若為SIS且C行為空,則跳過
  78. $skip = true;
  79. break;
  80. } else if ($j == 'A' && (!str_contains($value, "-") || str_contains($value, "說明"))) { //若儀器資料不含'-'且含'說明'則跳過
  81. $skip = true;
  82. break;
  83. } else if ($j == 'A' && str_contains($sheetName, "TI")) {
  84. $value = str_replace(".", "", $value);
  85. }
  86. array_push($row, $value);
  87. }
  88. }
  89. if (!$skip) {
  90. array_push($data, $row);
  91. $row_merge = str_repeat(", ?", $col_count);
  92. $sql = "BEGIN
  93. IF NOT EXISTS (SELECT [date] FROM [{$type}_Data] WHERE [date] = '{$date}' AND [EquipmentID] = '')
  94. BEGIN
  95. INSERT INTO [{$type}_Data] VALUES (? {$row_merge})
  96. END
  97. END";
  98. $stmt = sqlsrv_query($conn, $sql, $row);
  99. if ($stmt === false) {
  100. if (($errors = sqlsrv_errors()) != null) {
  101. foreach ($errors as $error) {
  102. echo "SQLSTATE: " . $error['SQLSTATE'] . "<br />";
  103. echo "code: " . $error['code'] . "<br />";
  104. echo "message: " . $error['message'] . "<br />";
  105. echo ($sql . "<br />");
  106. var_dump($row);
  107. }
  108. }
  109. }
  110. }
  111. }
  112. $excelData[$sheetName] = $data;
  113. }
  114. }
  115. if (isset($_SESSION['UserID'])) {
  116. $user_id = $_SESSION['UserID'];
  117. } else {
  118. $user_id = "0000";
  119. }
  120. $sql = "INSERT INTO [Update_Time] ([UserID]) VALUES (?);";
  121. $stmt = sqlsrv_query($conn, $sql, array($_SESSION['UserID']));
  122. if ($stmt === false) {
  123. if (($errors = sqlsrv_errors()) != null) {
  124. foreach ($errors as $error) {
  125. echo "SQLSTATE: " . $error['SQLSTATE'] . "<br />";
  126. echo "code: " . $error['code'] . "<br />";
  127. echo "message: " . $error['message'] . "<br />";
  128. echo ($sql . "<br />");
  129. var_dump($row);
  130. }
  131. }
  132. }
  133. header("Location: ../../Upload.php?status=success");
  134. } else {
  135. header("Location: ../../Upload.php?status=failed");
  136. }
  137. function check_sheet($sheetName, $equipment_data)
  138. {
  139. $equipment_types = ["SIS", "SID", "SB", "SM", "TI", "VG", "RB", "OW", "SP", "EP", "SS", "EXM", "ETi", "BS"];
  140. foreach ($equipment_types as $type) {
  141. if (str_contains($sheetName, $type)) {
  142. $key = array_search($type, array_column($equipment_data, 'type'));
  143. return $key;
  144. }
  145. }
  146. return null;
  147. }