123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 |
- <?php
- /**
- * excel导出数据
- * @param $resHeader array 表头
- * @param $resData array 数据
- * @param $filename string 文件名
- * @param $strType array 需要设置成文本设置的单元格
- */
- function export_excel_data($resHeader, $resData, $filename, $strType = null)
- {
- if ($resHeader && $resData && $filename) {
- header("Content-type:application/vnd.ms-excel");
- header("Content-Disposition: attachment; filename=" . $filename . ".xls");
- echo "<style>
- br {mso-data-placement:same-cell;}
- </style>";
- echo "<table border='1'><tr>";
- $t_field = [];
- foreach ($resHeader as $ha => $h) {
- $t_field[] = $ha;
- echo "<th>" . $h . "</th>";
- }
- echo "</tr>";
- foreach ($resData as $dd => $d) {
- echo "<tr>";
- foreach ($t_field as $td => $t) {
- //判断是否合并单元格
- echo '<td';
- //判断是否设置单元格为文本
- if (is_array($strType) && in_array($t, $strType)) {
- echo ' style="vnd.ms-excel.numberformat:@"';
- }
- echo ">";
- //判断是否拆分单元格
- if (is_array($d[$t])) {
- echo '<table width=\'100%\' border=\'1\' rules=\'all\'>';
- foreach ($d[$t] as $rowValue) {
- echo '<tr><td>' . $rowValue . '</td></tr>';
- }
- echo '</table>';
- } else {
- echo $d[$t];
- }
- echo "</td>";
- }
- echo "</tr>";
- }
- echo "</table>";
- exit;
- }
- }
- /**
- * CSV导出
- * @param $file_name
- * @param $header
- * @param $list
- */
- function exportCsv($header, $list, $file_name)
- {
- static $source = '';
- if (empty($source)) {
- header('Content-type: text/csv; charset=utf-8');
- header('Content-Disposition: attachment;filename="' . $file_name . '.csv"');
- header('Cache-Control: max-age=0');
- // PHP文件句柄,php://output 表示直接输出到浏览器
- $source = fopen('php://output', 'a');
- // 写入列头
- foreach ($header as $k => $v) {
- // CSV的Excel支持GBK编码,一定要转换,否则乱码
- //$header[$k] = iconv('utf-8', 'gbk', $v);
- $header[$k] = $v;
- }
- fputcsv($source, $header);
- }
- foreach ($list as $key => $value) {
- $temp = [];
- foreach ($header as $k => $v) {
- //$temp[$k] = "\t" . iconv('utf-8', 'gbk', $value[$k]);
- $temp[$k] = "\t" . $value[$k];
- }
- fputcsv($source, $temp);
- ob_flush();
- flush();
- }
- }
- /*
- * 生成随机字符串
- *
- * $length 字符串长度
- */
- function randomStr($length)
- {
- // 密码字符集,可任意添加你需要的字符
- $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
- $str = '';
- for ($i = 0; $i < $length; $i++) {
- $str .= $chars[mt_rand(0, strlen($chars) - 1)];
- }
- return $str;
- }
- function get_image_url($file) {
- if (strpos($file, "http") === 0) {
- return $file;
- } else if (strpos($file, "/") === 0) {
- return cmf_get_domain() . cmf_get_root() . $file;
- } else {
- $storage = \cmf\lib\Storage::instance();
- return $storage->getPreviewUrl($file);
- }
- }
- /**
- * 生成唯一的订单号 20110809111259232312
- * 2011-年日期
- * 08-月份
- * 09-日期
- * 11-小时
- * 12-分
- * 59-秒
- * 2323-微秒
- * 12-随机值
- * @return string
- */
- function get_trade_no()
- {
- list($usec, $sec) = explode(" ", microtime());
- $usec = substr(str_replace('0.', '', $usec), 0, 4);
- $str = rand(10, 99);
- return date("YmdHis") . $usec . $str;
- }
|