common.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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) - 1);
  19. $res .= $str[$key];
  20. }
  21. return $res;
  22. }
  23. function array_get($array, $key, $default = null)
  24. {
  25. return isset($array[$key]) ? $array[$key] : $default;
  26. }
  27. /**
  28. * 公共数据导出实现功能
  29. * @param string $expTitle
  30. * @param array $expCellName [['a','A']]
  31. * @param array $expTableData [['a'=>1]]
  32. * @param array $textValue ['a']
  33. */
  34. function export_exl($expTitle, $expCellName, $expTableData, $textValue = [])
  35. {
  36. $fileName = $expTitle . date('_YmdHis'); //or $xlsTitle 文件名称可根据自己情况设定
  37. $cellNum = count($expCellName);
  38. $dataNum = count($expTableData);
  39. $objPHPExcel = new \PHPExcel();
  40. $cellName = get_exl_column_letter(count($expCellName));
  41. for ($i = 0; $i < $cellNum; $i++) {
  42. $objPHPExcel->setActiveSheetIndex(0)->setCellValue($cellName[$i] . '1', $expCellName[$i][1]);
  43. }
  44. // Miscellaneous glyphs, UTF-8
  45. for ($i = 0; $i < $dataNum; $i++) {
  46. for ($j = 0; $j < $cellNum; $j++) {
  47. if (in_array($expCellName[$j][0], $textValue)) {
  48. $objPHPExcel->getActiveSheet(0)->setCellValueExplicit($cellName[$j] . ($i + 2), $expTableData[$i][$expCellName[$j][0]], \PHPExcel_Cell_DataType::TYPE_STRING);
  49. } else {
  50. $objPHPExcel->getActiveSheet(0)->setCellValue($cellName[$j] . ($i + 2), $expTableData[$i][$expCellName[$j][0]]);
  51. }
  52. }
  53. }
  54. ob_end_clean();
  55. header('pragma:public');
  56. header('Content-type:application/vnd.ms-excel');
  57. header("Content-Disposition:attachment;filename=$fileName.xls"); //attachment新窗口打印inline本窗口打印
  58. $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
  59. $objWriter->save('php://output');
  60. exit;
  61. }
  62. function get_exl_column_letter($num = 1)
  63. {
  64. $res = [];
  65. for ($i = 0; $i < $num; $i++) {
  66. $y = ($i / 26);
  67. if ($y >= 1) {
  68. $y = intval($y);
  69. $res[] = chr($y + 64) . chr($i - $y * 26 + 65);
  70. } else {
  71. $res[] = chr($i + 65);
  72. }
  73. }
  74. return $res;
  75. }
  76. /**
  77. * @param string $file
  78. * @param array $cell
  79. * @param int $crop
  80. * @param int $sheet
  81. * @return array
  82. */
  83. function import_exl($file = '', $cell = [], $crop = 0, $sheet = 0)
  84. {
  85. $file = iconv("utf-8", "gb2312", $file); //转码
  86. if (empty($file) or !file_exists($file)) {
  87. die('file not exists!');
  88. }
  89. $objRead = new PHPExcel_Reader_Excel2007(); //建立reader对象
  90. if (!$objRead->canRead($file)) {
  91. $objRead = new PHPExcel_Reader_Excel5();
  92. if (!$objRead->canRead($file)) {
  93. die('No Excel!');
  94. }
  95. }
  96. $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'];
  97. $obj = $objRead->load($file); //建立excel对象
  98. $currSheet = $obj->getSheet($sheet); //获取指定的sheet表
  99. $columnH = $currSheet->getHighestColumn(); //取得最大的列号
  100. $columnCnt = array_search($columnH, $cellName);
  101. $rowCnt = $currSheet->getHighestRow(); //获取总行数
  102. $data = [];
  103. for ($_row = 1; $_row <= $rowCnt; $_row++) { //读取内容
  104. if ($_row > $crop) {
  105. for ($_column = 0; $_column <= $columnCnt; $_column++) {
  106. $cellId = $cellName[$_column] . $_row;
  107. $cellValue = $currSheet->getCell($cellId)->getValue();
  108. //$cellValue = $currSheet->getCell($cellId)->getCalculatedValue(); #获取公式计算的值
  109. if ($cellValue instanceof PHPExcel_RichText) { //富文本转换字符串
  110. $cellValue = $cellValue->__toString();
  111. } else {
  112. $cellValue = (string)$cellValue;
  113. }
  114. if (!empty($cell[$_column])) {
  115. $data[$_row][$cell[$_column]] = $cellValue;
  116. }
  117. }
  118. }
  119. }
  120. return array_values($data);
  121. }