<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                # 目錄 [TOC=2,3] * * * * * * * * * * * * * * * * * * * * - ## PHPExcel函數集合 * * * * * * * * * * * * * * * - ### 讀取excel里的數據到數組中 * * * * * >[info]#### 1. 讀取excel里的數據到數組中【read】 ~~~ 方法一: /** * 讀取excel里的數據到數組中 * @param: $filename 路徑文件名 * @param:$encode 返回數據的編碼 默認為utf8 * @author: ityangs<ityangs@163.com> */ public function read($filename,$encode='utf-8'){ $type = 'Excel5'; if(strtolower(array_pop(explode('.',$filename))) == 'xlsx'){ $type = 'Excel2007'; } $objPHPExcel = new PHPExcel(); $objReader = PHPExcel_IOFactory::createReader($type); $objReader->setReadDataOnly(true); $objPHPExcel = $objReader->load($filename); // $objWorksheet = $objPHPExcel->getActiveSheet(); $objSheet = $objPHPExcel->getAllSheets(); $excelData = array(); foreach($objSheet as $k=>$sheet){ $highestRow = $sheet->getHighestRow(); $highestColumn = $sheet->getHighestColumn(); $highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn); for ($row = 1; $row <= $highestRow; $row++) { for ($col = 0; $col < $highestColumnIndex; $col++) { $excelData[$k]['list'][$row-1][] =(string)$sheet->getCellByColumnAndRow($col, $row)->getValue(); } } $excelData[$k]['title'] = $sheet->getTitle(); } return $excelData; } 方法二: /** * 讀取excel里的數據到數組中 * @param: $filename 路徑文件名 * @param:$encode 返回數據的編碼 默認為utf8 * @author: ityangs<ityangs@163.com> */ public function read($filename,$encode='utf-8'){ $excel=PHPExcel_IOFactory::load($filename); $excel->setActiveSheetIndex(0); $sheetData = $excel->getActiveSheet()->toArray(null,true,true,true); unlink($filename); unset($sheetData[1]); return $excelData; } ~~~ >[info]#### 2. 測試 - #### 函數使用 ~~~ read($path); ~~~ - #### 函數結果 ~~~ <pre>Array ( [0] => Array ( [list] => Array ( [0] => Array ( [0] => 發貨單號 [1] => 更改后渠道代碼 [2] => 更改后計費重 [3] => 更改后總費用 [4] => 更改原因 ) [1] => Array ( [0] => FH171227000003 [1] => QD00323 [2] => 12 [3] => 12 [4] => 清關原因 ) ) [title] => Sheet1 ) ) </pre> ~~~ * * * * * * * * * * * * * * * - ### 讀取excel文件的日期轉換成php時間 * * * * * >[info]#### 1.讀取excel文件的日期轉換成php時間【excelTime】 ~~~ /** * 讀取excel文件的日期轉換成php時間 * @param $date * @param bool|false $time * @return array|int|string */ protected function excelTime($date, $time = false) { if(function_exists('GregorianToJD')){ if (is_numeric( $date )) { $jd = GregorianToJD( 1, 1, 1970 ); $gregorian = JDToGregorian( $jd + intval ( $date ) - 25569 ); $date = explode( '/', $gregorian ); $date_str = str_pad( $date [2], 4, '0', STR_PAD_LEFT ) ."-". str_pad( $date [0], 2, '0', STR_PAD_LEFT ) ."-". str_pad( $date [1], 2, '0', STR_PAD_LEFT ) . ($time ? " 00:00:00" : ''); return $date_str; } }else{ $date=$date>25568?$date+1:25569; /*There was a bug if Converting date before 1-1-1970 (tstamp 0)*/ $ofs=(70 * 365 + 17+2) * 86400; $date = date("Y-m-d",($date * 86400) - $ofs).($time ? " 00:00:00" : ''); } return $date; } ~~~ >[info]#### 2. 測試 - #### 函數使用 ~~~ excelTime($data); ~~~ - #### 函數結果 ~~~ 2018-01-01 00:00:00 ~~~ * * * * * * * * * * * * * * * - ## PHP操作導入導出Excel和CSV函數 * * * * * >[info]#### 2. 導出Excel文件【export_excel】 ~~~ /** *導出Excel表格 *@param $data 一個二維數組,結構如同從數據庫查出來的數組 *@param $title excel的第一行標題,一個數組,如果為空則沒有標題 *@param $filename 下載的文件名 為空是當前時間格式命名 *@param $filetype 下載的文件類型(支持:xls/xlsx) *@param int $limit 每隔$limit行 刷新一下輸出buffer,不要太大,也不要太小 *@examlpe *export_excel($arr,array('id','賬戶','密碼','昵稱'),'文件名','xls',10000); */ function export_excel($data=array(),$title=array(),$filename='',$filetype='xls',$limit=10000) { ignore_user_abort(true); set_time_limit(0);// 不限制腳本執行時間以確保導出完成 ini_set("memory_limit","1024M"); $filename=!empty($filename)?$filename:date('YmdHis',time()); header("Content-type:application/octet-stream"); header("Accept-Ranges:bytes"); header("Content-type:application/vnd.ms-excel"); header("Content-Disposition:attachment;filename={$filename}.{$filetype}"); header("Pragma: no-cache"); header("Expires: 0"); //導出開始 if (count($title)>0) { foreach ($title as $k => $v) { $title[$k]=iconv("utf-8", "gbk",$v); } $title= implode("\t ", $title); echo "$title\n"; } $num = 0; if (count($data)>0) { foreach($data as $key=>$val) { $num++; //刷新一下輸出buffer,防止由于數據過多造成問題 if ($limit == $num) { ob_flush(); flush(); $num = 0; } foreach ($val as $ck => $cv) { $data[$key][$ck]=iconv("utf-8", "gbk", $cv); } $data[$key]=implode("\t ", $data[$key]); } echo implode("\n",$data); } } ~~~ >[info]#### 2. 測試 - #### 函數使用 ~~~ $data=[ ['卡1','b',date("Y-m-d"),'d'], ['卡2','b','c','d'], ['卡3','b','c','d'], ['卡4','b','c','d'], ]; $title=array('卡','生成碼','開始','有效'); export_excel($data, $title, 'test','xls',10000); ~~~ - #### 函數結果 ~~~ 導出Excel文件 ~~~ * * * * * * * * * * * * * * * >[info]#### 3. 導入CSV文件【import_csv】 ~~~ /** * 導入CSV文件 * @param string $csv_file csv文件路徑 * @param int $offset 起始行數:1為標題行,跟EXcel行標對應 * @param int $lines 讀取行數:-1(負數)為讀完,正數為讀取的行數 * @return array|bool */ function import_csv($csv_file = '', $offset = 1, $lines = -1) { ignore_user_abort(true); set_time_limit(0);// 不限制腳本執行時間以確保導出完成 ini_set("memory_limit","1024M"); if (!$fp = fopen($csv_file, 'r')) { return false; } $i = $j = 1; $data = array(); //過濾起始行以前的數據 while (false !== ($line = fgets($fp))) { if ($i++ < $offset) { continue; } $data[0]=explode(',', $line); break; } if ($lines<0){//讀完 while (!feof($fp)) { $data[] = fgetcsv($fp); } }else { while ($j++ <= $lines && !feof($fp)) { $data[] = fgetcsv($fp); } } fclose($fp); array_pop($data);//刪除數組最后一個空數組 return $data; } ~~~ >[info]#### 2. 測試 - #### 函數使用 ~~~ echo "<pre>"; print_r(import_csv('test.csv',3,2)); echo "</pre>"; ~~~ - #### 函數結果 ~~~ Array ( [0] => Array ( [0] => 卡2 [1] => b [2] => c [3] => d ) [1] => Array ( [0] => 卡3 [1] => b [2] => c [3] => d ) ) ~~~ * * * * * >[info]#### 4. 導出CSV文件【export_csv】 ~~~ /** * 導出CSV文件 * @param array $data 數據 * @param array $header_data 首行標題數據 * @param string $file_name 文件名稱 為空是當前時間格式命名 * @param int $limit 每隔$limit行 刷新一下輸出buffer,不要太大,也不要太小 * @return string */ function export_csv($data = [], $header_data = [], $file_name = '',$limit=10000) { ignore_user_abort(true); set_time_limit(0);// 不限制腳本執行時間以確保導出完成 ini_set("memory_limit","1024M"); $filename=!empty($filename)?$filename:date('YmdHis',time()); header("Content-type:application/octet-stream"); header("Accept-Ranges:bytes"); header('Content-Type: application/vnd.ms-excel'); header('Content-Disposition: attachment;filename='.$file_name.'.csv'); header('Cache-Control: max-age=0'); $fp = fopen('php://output', 'a'); //這個是后臺生成文件(并去除header) //$file_path=$dir_path.'/'.$file_name.'.csv'; //$fp = fopen($file_path, 'a'); if (!empty($header_data)) { foreach ($header_data as $key => $value) { $header_data[$key] = iconv('utf-8', 'gbk', $value); } fputcsv($fp, $header_data); } $num = 0; //逐行取出數據,不浪費內存 $count = count($data); if ($count > 0) { for ($i = 0; $i < $count; $i++) { $num++; //刷新一下輸出buffer,防止由于數據過多造成問題 if ($limit == $num) { ob_flush(); flush(); $num = 0; } $row = $data[$i]; foreach ($row as $key => $value) { $row[$key] = iconv('utf-8', 'gbk', $value); } fputcsv($fp, $row); } } fclose($fp); } ~~~ >[info]#### 2. 測試 - #### 函數使用 ~~~ $data=[ ['卡1','b','c','d'], ['卡2','b','c','d'], ['卡3','b','c','d'], ['卡4','b','c','d'], ]; $title=array('卡','生成碼','開始','有效'); export_csv($data,$title,'test',1000); ~~~ - #### 函數結果 ~~~ 生成csv文件 ~~~ * * * * * * * * * * * * * * * >[info]#### 5. table導出CSV文件【exportExcel】 ~~~ /** * 導出數據 * @date: 2018年7月3日 下午5:21:52 * @author: ityangs<ityangs@163.com> */ function exportExcel($data=array(),$title=array(),$filename='',$filetype='xls') { set_time_limit(0);// 不限制腳本執行時間以確保導出完成 ini_set("memory_limit","1024M"); $filename=!empty($filename)?$filename:date('YmdHis',time()); header("Content-type:application/octet-stream"); header("Accept-Ranges:bytes"); header("Content-type:application/vnd.ms-excel"); header("Content-Disposition:attachment;filename={$filename}.{$filetype}"); header("Pragma: no-cache"); header("Expires: 0"); $content=""; $content.="<table border='1' align='center' style='font-size:15px;'><tr>"; foreach($title as $pre){ $content.="<td>$pre</td>"; } $content.="</tr>"; foreach($data as $list){ $content.= "<tr>"; foreach($list as $val){ $content.= "<td style='vnd.ms-excel.numberformat:@;width:150px;'>".$val."</td>"; //style樣式將導出的內容都設置為文本格式 輸出對應鍵名的鍵值 即內容 } $content.="</tr>"; } $content.="</table>"; echo $content; exit(); } ~~~ >[info]#### 2. 測試 - #### 函數使用 ~~~ exportExcel($data,$title,'用戶信息導出',$filetype='xls'); ~~~ - #### 函數結果 ~~~ 生成Excel ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看