| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162 |
- <?php
- // +----------------------------------------------------------------------
- // | ThinkPHP [ WE CAN DO IT JUST THINK ]
- // +----------------------------------------------------------------------
- // | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
- // +----------------------------------------------------------------------
- // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
- // +----------------------------------------------------------------------
- // | Author: 流年 <liu21st@gmail.com>
- // +----------------------------------------------------------------------
- // 应用公共文件
- /**
- * 删除目录以及其下的文件
- * @param $directory
- * @return bool
- */
- function removeDir($directory)
- {
- if (false == is_dir($directory)) {
- return false;
- }
- $handle = opendir($directory);
- while (false !== ($file = readdir($handle))) {
- if ('.' != $file && '..' != $file) {
- is_dir("$directory/$file") ? removeDir("$directory/$file") : @unlink("$directory/$file");
- }
- }
- if (readdir($handle) == false) {
- closedir($handle);
- rmdir($directory);
- }
- return true;
- }
- function Kfid($uid)
- {
- return trim($uid, 'KF');
- }
- function kfUid($id)
- {
- return 'KF' . $id;
- }
- //数组转为健的数组
- function kftoKey($uidArray, $type = 0)
- {
- $return = [];
- foreach ($uidArray as $val) {
- if ($type == 0) {
- $return[$val] = 0;
- } elseif ($type == 1) {
- $return[$val] = [];
- } else {
- $return[$val] = $val;
- }
- }
- return $return;
- }
- //保留小数位数
- function floatPointDigit($data, $long = 2)
- {
- $long = intval($long);
- $for1 = "%." . $long . "f";
- $for2 = "%." . ($long + 1) . "f";
- return sprintf($for1, substr(sprintf($for2, $data), 0, -1));
- }
- //秒格式化为 小时分秒
- function secendToHourMinit($sec)
- {
- $array = ['h' => 0, 'm' => 0, 's' => 0];
- $sec = intval($sec);
- $array['h'] = floor($sec / 3600);
- $array['m'] = floor(($sec - 3600 * $array['h']) / 60);
- $array['s'] = $sec - 3600 * $array['h'] - 60 * $array['m'];
- $return = '';
- if ($array['h']) {
- $return .= $array['h'] . '小时';
- }
- if ($array['m']) {
- $return .= $array['m'] . '分';
- }
- if ($array['s']) {
- $return .= $array['s'] . '秒';
- }
- return $return;
- }
- //小数转百分比显示
- function perDisplay($dit, $xiaos = 2)
- {
- return floatPointDigit(floor(100 * $dit), $xiaos) . '%';
- }
- /**
- * 导出excel表格
- *
- * @param array $columName 第一行的列名称
- * @param array $list 二维数组
- * @param string $setTitle sheet名称
- * @return
- * @author Tggui <tggui@vip.qq.com>
- */
- function exportExcel($columName, $list, $setTitle = 'Sheet1', $fileName = 'demo')
- {
- if (empty($columName) || empty($list)) {
- return '列名或者内容不能为空';
- }
- if (count($list[0]) != count($columName)) {
- return '列名跟数据的列不一致';
- }
- //实例化PHPExcel类
- $PHPExcel = new PHPExcel();
- //获得当前sheet对象
- $PHPSheet = $PHPExcel->getActiveSheet();
- //定义sheet名称
- $PHPSheet->setTitle($setTitle);
- //excel的列 这么多够用了吧?不够自个加 AA AB AC ……
- $letter = [
- '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'
- ];
- //把列名写入第1行 A1 B1 C1 ...
- for ($i = 0; $i < count($list[0]); $i++) {
- $PHPSheet->setCellValue("$letter[$i]1", "$columName[$i]");
- }
- //内容第2行开始
- foreach ($list as $key => $val) {
- foreach (array_values($val) as $key2 => $val2) {
- $PHPSheet->setCellValue($letter[$key2] . ($key + 2), $val2);
- }
- }
- //生成2007版本的xlsx
- $PHPWriter = PHPExcel_IOFactory::createWriter($PHPExcel, 'Excel2007');
- header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
- header('Content-Disposition: attachment;filename=' . $fileName . '.xlsx');
- header('Cache-Control: max-age=0');
- $PHPWriter->save("php://output");
- }
- /*
- * 使用方法
- $titlname = ['订单号','消费用户','订单金额','订单数量','支付状态','订单时间'];
- exportExcel($titlname,'你的二维数组数据','Sheet1','文件名称');
- */
|