common.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. // 应用公共文件
  3. function ajax_return($code = 0, $msg = '', $data = [])
  4. {
  5. $res = ['code' => $code, 'msg' => $msg, 'data' => $data];
  6. $response = \think\Response::create($res, 'json');
  7. throw new \think\exception\HttpResponseException($response);
  8. }
  9. function url(string $url = '', array $vars = [], $suffix = true, $domain = true)
  10. {
  11. return \think\facade\Route::buildUrl($url, $vars)->suffix($suffix)->domain($domain);
  12. }
  13. function rand_str($len = 6)
  14. {
  15. $str = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
  16. $res = '';
  17. for ($i = 0; $i < $len; $i++) {
  18. $key = rand(0, strlen($str));
  19. $res .= $str[$key];
  20. }
  21. return $res;
  22. }
  23. function array_get($array, $key, $default = null)
  24. {
  25. if (isset($array[$key])) {
  26. return $array[$key];
  27. }
  28. if (strpos($key, '.') === false) {
  29. return $array[$key] ?? $default;
  30. }
  31. foreach (explode('.', $key) as $segment) {
  32. if (is_array($array) && array_key_exists($segment, $array)) {
  33. $array = $array[$segment];
  34. } else {
  35. return $default;
  36. }
  37. }
  38. return $array;
  39. }
  40. /**
  41. * 公共数据导出实现功能
  42. * @param string $expTitle
  43. * @param array $expCellName [['a','A']]
  44. * @param array $expTableData [['a'=>1]]
  45. * @param array $textValue ['a']
  46. */
  47. function export_exl($expTitle, $expCellName, $expTableData, $textValue=[]) {
  48. $fileName = $expTitle . date('_YmdHis'); //or $xlsTitle 文件名称可根据自己情况设定
  49. $cellNum = count($expCellName);
  50. $dataNum = count($expTableData);
  51. $objPHPExcel = new \PHPExcel();
  52. $cellName = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'AA', 'AB', 'AC', 'AD', 'AE', 'AF', 'AG', 'AH', 'AI', 'AJ', 'AK', 'AL', 'AM', 'AN', 'AO', 'AP', 'AQ', 'AR', 'AS', 'AT', 'AU', 'AV', 'AW', 'AX', 'AY', 'AZ'];
  53. for ($i = 0; $i < $cellNum; $i++) {
  54. $objPHPExcel->setActiveSheetIndex(0)->setCellValue($cellName[$i] . '1', $expCellName[$i][1]);
  55. }
  56. // Miscellaneous glyphs, UTF-8
  57. for ($i = 0; $i < $dataNum; $i++) {
  58. for ($j = 0; $j < $cellNum; $j++) {
  59. if (in_array($expCellName[$j][0],$textValue)) {
  60. $objPHPExcel->getActiveSheet(0)->setCellValueExplicit($cellName[$j] . ($i + 2), $expTableData[$i][$expCellName[$j][0]],\PHPExcel_Cell_DataType::TYPE_STRING);
  61. } else {
  62. $objPHPExcel->getActiveSheet(0)->setCellValue($cellName[$j] . ($i + 2), $expTableData[$i][$expCellName[$j][0]]);
  63. }
  64. }
  65. }
  66. ob_end_clean();
  67. header('pragma:public');
  68. header('Content-type:application/vnd.ms-excel');
  69. header("Content-Disposition:attachment;filename=$fileName.xls"); //attachment新窗口打印inline本窗口打印
  70. $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
  71. $objWriter->save('php://output');
  72. exit;
  73. }
  74. /**
  75. * @param string $file
  76. * @param array $cell
  77. * @param int $crop
  78. * @param int $sheet
  79. * @return array
  80. */
  81. function import_exl($file = '', $cell = [], $crop = 0, $sheet = 0)
  82. {
  83. $file = iconv("utf-8", "gb2312", $file); //转码
  84. if (empty($file) OR !file_exists($file)) {
  85. die('file not exists!');
  86. }
  87. $objRead = new PHPExcel_Reader_Excel2007(); //建立reader对象
  88. if (!$objRead->canRead($file)) {
  89. $objRead = new PHPExcel_Reader_Excel5();
  90. if (!$objRead->canRead($file)) {
  91. die('No Excel!');
  92. }
  93. }
  94. $cellName = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'AA', 'AB', 'AC', 'AD', 'AE', 'AF', 'AG', 'AH', 'AI', 'AJ', 'AK', 'AL', 'AM', 'AN', 'AO', 'AP', 'AQ', 'AR', 'AS', 'AT', 'AU', 'AV', 'AW', 'AX', 'AY', 'AZ'];
  95. $obj = $objRead->load($file); //建立excel对象
  96. $currSheet = $obj->getSheet($sheet); //获取指定的sheet表
  97. $columnH = $currSheet->getHighestColumn(); //取得最大的列号
  98. $columnCnt = array_search($columnH, $cellName);
  99. $rowCnt = $currSheet->getHighestRow(); //获取总行数
  100. $data = [];
  101. for ($_row = 1; $_row <= $rowCnt; $_row++) { //读取内容
  102. if ($_row > $crop) {
  103. for ($_column = 0; $_column <= $columnCnt; $_column++) {
  104. $cellId = $cellName[$_column] . $_row;
  105. $cellValue = $currSheet->getCell($cellId)->getValue();
  106. //$cellValue = $currSheet->getCell($cellId)->getCalculatedValue(); #获取公式计算的值
  107. if ($cellValue instanceof PHPExcel_RichText) { //富文本转换字符串
  108. $cellValue = $cellValue->__toString();
  109. } else {
  110. $cellValue = (string)$cellValue;
  111. }
  112. if (!empty($cell[$_column])) {
  113. $data[$_row][$cell[$_column]] = $cellValue;
  114. } else {
  115. $data[$_row][] = $cellValue;
  116. }
  117. }
  118. }
  119. }
  120. return array_values($data);
  121. }
  122. /**
  123. * 删除空值
  124. */
  125. function del_empty($array)
  126. {
  127. foreach ($array as $k => $v) {
  128. if (empty($v)) {
  129. unset($array[$k]);
  130. }
  131. }
  132. return $array;
  133. }