phpexcel是一個php的插件,他是用來讀取excel文檔的,如果沒有它的話自己要寫個這樣的工具比較麻煩,現在有了現成的excel讀取插件方便了很多.
php對excel文件進行循環讀取,php對字符進行ascii編碼轉化,將字符轉為十進制數,php對excel日期格式讀取,并進行顯示轉化.
php對漢字亂碼進行編碼轉化,代碼如下:
- <?php
- require_once 'PHPExcel.php';
- /**對excel里的日期進行格式轉化*/
- function GetData($val){
- $jd = GregorianToJD(1, 1, 1970);
- $gregorian = JDToGregorian($jd+intval($val)-25569);
- return $gregorian;/**顯示格式為 “月/日/年” */
- }
- $filePath = 'test.xlsx';
- $PHPExcel = new PHPExcel();
- /**默認用excel2007讀取excel,若格式不對,則用之前的版本進行讀取*/
- $PHPReader = new PHPExcel_Reader_Excel2007();
- if(!$PHPReader->canRead($filePath)){
- $PHPReader = new PHPExcel_Reader_Excel5();
- if(!$PHPReader->canRead($filePath)){
- echo 'no Excel';
- return ;
- }
- }
- $PHPExcel = $PHPReader->load($filePath);
- /**讀取excel文件中的第一個工作表*/
- $currentSheet = $PHPExcel->getSheet(0);
- /**取得最大的列號*/
- $allColumn = $currentSheet->getHighestColumn();
- /**取得一共有多少行*/
- $allRow = $currentSheet->getHighestRow();
- /**從第二行開始輸出,因為excel表中第一行為列名*/
- for($currentRow = 2;$currentRow <= $allRow;$currentRow++){
- /**從第A列開始輸出*/
- for($currentColumn= 'A';$currentColumn<= $allColumn; $currentColumn++){
- $val = $currentSheet->getCellByColumnAndRow(ord($currentColumn) - 65,$currentRow)->getValue();/**ord()將字符轉為十進制數*/
- if($currentColumn == 'A')
- {//開源代碼Vevb.com
- echo GetData($val)."t";
- }else{
- //echo $val;
- /**如果輸出漢字有亂碼,則需將輸出內容用iconv函數進行編碼轉換,如下將gb2312編碼轉為utf-8編碼輸出*/
- echo iconv('utf-8','gb2312', $val)."t";
- }
- }
- echo "</br>";
- }
- echo "n";
- ?>
新聞熱點
疑難解答