getExcel.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. require '../../../vendor/autoload.php';
  3. include("../sql.php");
  4. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  5. use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
  6. $ajax = array();
  7. $connectionInfo = array("Database" => "$dbname", "UID" => "$username", "PWD" => "$password", "CharacterSet" => "UTF-8");
  8. $conn = sqlsrv_connect($hostname, $connectionInfo);
  9. if ($conn === false) {
  10. die(print_r(sqlsrv_errors(), true));
  11. }
  12. $fileinfo = array(array("計畫編號", "工程類別", "此計畫是否匯入過"));
  13. $file = '00000.xlsx';
  14. if (isset($_GET['file'])) {
  15. $file = "uploads/{$_GET['file']}";
  16. }
  17. $isImport = '是';
  18. $isImportArray = array();
  19. $project_id = $_GET['projectId'];
  20. $category_id = $_GET['categoryId'];
  21. $sql = "SELECT TOP 1 [project_id] FROM [21000X].[dbo].[File_Table] WHERE [project_id] = '{$project_id}'";
  22. $fetchResult = sqlsrv_query($conn, $sql);
  23. $row = sqlsrv_fetch_array($fetchResult);
  24. if (empty($row)) {
  25. $isImport = '否';
  26. }
  27. $sql = "SELECT [category_name] FROM [Construction_Category] WHERE [category_id] = '{$category_id}'";
  28. $fetchResult = sqlsrv_query($conn, $sql);
  29. $category = sqlsrv_fetch_array($fetchResult)[0];
  30. array_push($fileinfo, array($project_id, $category, $isImport));
  31. $importinfo = array(array("類別", "選擇檔案類型", "是否為新的類型"));
  32. $ajax["0_匯入資訊"] = $fileinfo;
  33. $reader = new \PhpOffice\PhpSpreadsheet\Reader\Xlsx();
  34. $reader->setReadDataOnly(true);
  35. $reader->setReadEmptyCells(false);
  36. $spreadsheet = $reader->load($file);
  37. $sheetCount = $spreadsheet->getSheetCount();
  38. $all = $spreadsheet->getSheetNames();
  39. for ($s = 0; $s < $sheetCount; $s++) {
  40. if (str_contains($all[$s], "_")) {
  41. $Category = explode("_", $all[$s])[1];
  42. } else if (str_contains($all[$s], ".")) {
  43. $Category = explode(".", $all[$s])[1];
  44. } else {
  45. $Category = $all[$s];
  46. }
  47. $sql = "IF NOT EXISTS (SELECT [type_id] FROM [File_Category] WHERE [type_name] = '{$Category}')
  48. BEGIN
  49. SELECT '是'
  50. END
  51. ELSE
  52. BEGIN
  53. SELECT '否'
  54. END";
  55. $fetchResult = sqlsrv_query($conn, $sql);
  56. while ($row = sqlsrv_fetch_array($fetchResult)) {
  57. array_push($isImportArray, $row[0]);
  58. }
  59. }
  60. for ($s = 0; $s < $sheetCount; $s++) {
  61. $normalCheck = "";
  62. $blueprintCheck = "";
  63. $cancelCheck = "";
  64. $sheet = $spreadsheet->getSheet($s);
  65. $sheetName = $spreadsheet->getSheetNames()[$s];
  66. if (str_contains($sheetName, '成果圖') || str_contains($sheetName, '設計圖')) {
  67. $blueprintCheck = "checked='checked'";
  68. } else {
  69. $normalCheck = "checked='checked'";
  70. }
  71. if (str_contains($sheetName, '目錄') || str_contains($sheetName, '注意')) {
  72. $cancelCheck = "checked='checked'";
  73. }
  74. $cellCollection = $sheet->getCellCollection();
  75. $column = $cellCollection->getHighestRowAndColumn();
  76. $data = array();
  77. for ($i = 1; $i <= $column['row']; $i++) { //行
  78. $row = array();
  79. for ($j = 'A'; $j <= $column['column']; $j++) { //列
  80. $key = $j . $i;
  81. $value = $sheet->getCell($key)->getValue();
  82. array_push($row, $value);
  83. }
  84. array_push($data, $row);
  85. }
  86. $ajax[$sheetName] = $data;
  87. array_push($importinfo, array($sheetName, "<input type='radio' id='normal{$s}' name='type{$s}' value='[File_Table]' {$normalCheck}>
  88. <label for='normal{$s}'>一般檔案</label><br>
  89. <input type='radio' id='blueprint{$s}' name='type{$s}' value='[Blueprint]' {$blueprintCheck}>
  90. <label for='blueprint{$s}'>設計圖或成果圖</label><br>
  91. <input type='radio' id='cancel{$s}' name='type{$s}' value='cancel' {$cancelCheck}>
  92. <label for='cancel{$s}'>不進行匯入</label> ", $isImportArray[$s]));
  93. }
  94. $data["table"] = $ajax;
  95. $data["info"] = $importinfo;
  96. echo json_encode($data, JSON_UNESCAPED_UNICODE);
  97. sqlsrv_close($conn);