common.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. 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. $xlsTitle = iconv('utf-8', 'gb2312', $expTitle); //文件名称
  36. $fileName = $expTitle . date('_YmdHis'); //or $xlsTitle 文件名称可根据自己情况设定
  37. $cellNum = count($expCellName);
  38. $dataNum = count($expTableData);
  39. $objPHPExcel = new \PHPExcel();
  40. $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'];
  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. header('pragma:public');
  55. header('Content-type:application/vnd.ms-excel;charset=utf-8;name="' . $xlsTitle . '.xls"');
  56. header("Content-Disposition:attachment;filename=$fileName.xls"); //attachment新窗口打印inline本窗口打印
  57. $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
  58. $objWriter->save('php://output');
  59. exit;
  60. }
  61. /**
  62. * @param string $file
  63. * @param array $cell
  64. * @param int $crop
  65. * @param int $sheet
  66. * @return array
  67. */
  68. function import_exl($file = '', $cell = [], $crop = 0, $sheet = 0)
  69. {
  70. $file = iconv("utf-8", "gb2312", $file); //转码
  71. if (empty($file) OR !file_exists($file)) {
  72. die('file not exists!');
  73. }
  74. $objRead = new PHPExcel_Reader_Excel2007(); //建立reader对象
  75. if (!$objRead->canRead($file)) {
  76. $objRead = new PHPExcel_Reader_Excel5();
  77. if (!$objRead->canRead($file)) {
  78. die('No Excel!');
  79. }
  80. }
  81. $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'];
  82. $obj = $objRead->load($file); //建立excel对象
  83. $currSheet = $obj->getSheet($sheet); //获取指定的sheet表
  84. $columnH = $currSheet->getHighestColumn(); //取得最大的列号
  85. $columnCnt = array_search($columnH, $cellName);
  86. $rowCnt = $currSheet->getHighestRow(); //获取总行数
  87. $data = [];
  88. for ($_row = 1; $_row <= $rowCnt; $_row++) { //读取内容
  89. if ($_row > $crop) {
  90. for ($_column = 0; $_column <= $columnCnt; $_column++) {
  91. $cellId = $cellName[$_column] . $_row;
  92. $cellValue = $currSheet->getCell($cellId)->getValue();
  93. //$cellValue = $currSheet->getCell($cellId)->getCalculatedValue(); #获取公式计算的值
  94. if ($cellValue instanceof PHPExcel_RichText) { //富文本转换字符串
  95. $cellValue = $cellValue->__toString();
  96. } else {
  97. $cellValue = (string)$cellValue;
  98. }
  99. if (!empty($cell[$_column])) {
  100. $data[$_row][$cell[$_column]] = $cellValue;
  101. } else {
  102. $data[$_row][] = $cellValue;
  103. }
  104. }
  105. }
  106. }
  107. return array_values($data);
  108. }