EXCEL表格转换成json数据工具
生活随笔
收集整理的這篇文章主要介紹了
EXCEL表格转换成json数据工具
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
請先下載phpexcel插件后在使用如下代碼運行
phpexcel插件下載地址:http://www.chtml.cn/topic/show/40
實例代碼:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><html> <head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>EXCEL表格轉換成json數據工具</title></head><body><?php /* * 將excel轉換為數組 by aibhsc * */ini_set('max_execution_time', '0'); require('PHPExcel.php');//引入PHP EXCEL類function dump($data){echo '<pre>';print_r($data);echo '</pre>'; }function format_excel2array($filePath='',$sheet=0){if(empty($filePath) or !file_exists($filePath)){die('file not exists');}$PHPReader = new PHPExcel_Reader_Excel2007(); //建立reader對象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($sheet); //**讀取excel文件中的指定工作表*/$allColumn = $currentSheet->getHighestColumn(); //**取得最大的列號*/$allRow = $currentSheet->getHighestRow(); //**取得一共有多少行*/$data = array();for($rowIndex=1;$rowIndex<=$allRow;$rowIndex++){ //循環讀取每個單元格的內容。注意行從1開始,列從A開始for($colIndex='A';$colIndex<=$allColumn;$colIndex++){$addr = $colIndex.$rowIndex;$cell = $currentSheet->getCell($addr)->getValue();if($cell instanceof PHPExcel_RichText){ //富文本轉換字符串$cell = $cell->__toString();}$data[$rowIndex][$colIndex] = $cell;}}return $data; } function getUrl($str) { preg_match("/href=\"(.*)\" /", $str, $a);return $a[1]; } function mvtags($str){$str = implode('|rencongcong,',$str);$str = strip_tags($str,'<p><br/><br>');$str = explode('|rencongcong,',$str);return $str; } $arr = format_excel2array('./asp.xls'); $title = implode(',',$arr[1]); $db = array(); foreach($arr as $k=>$v){$str = $v;$v = mvtags($v);$db[$k]['title'] = $v['0'];$db[$k]['time'] = $v['1'];$db[$k]['node_id'] = $v['2'];$db[$k]['content'] = $v['3'];$db[$k]['uid'] = $v['4'];$db[$k]['homepage'] = getUrl($str['F']);$db[$k]['demo'] = getUrl($str['G']);$db[$k]['download'] = $v['7']; } $db = json_encode($db,true);file_put_contents('./asp.json',$db,FILE_APPEND);?></body> </html> 創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的EXCEL表格转换成json数据工具的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【java基础】idea配置jdk环境,
- 下一篇: 1.13编程基础之综合应用_14求满足条