PHPExcel-将JSON数据写入Excel

JSON数据格式:
2016-08-17.json

{
    "flights": [
        {
            "from": "Beijing", 
            "duration": "19h50m", 
            "class": "Economy", 
            "flight": "|DL128|DL129", 
            "departureAirport": "Beijing Capital International Airport", 
            "stops": "2+ stops", 
            "airline": "Delta Air Lines", 
            "to": "Seattle", 
            "arriveTime": "Wed Aug 17 16:10", 
            "departureTime": "Wed Aug 17 11:20", 
            "price": "|7524|7995", 
            "arrivalAirport": "Seattle–Tacoma International Airport", 
            "passengerType": "|NOR|STU", 
            "flightType": "|UNCERTAIN|Boeing 737-800"
        }, 
        {
            "from": "Beijing", 
            "duration": "22h26m", 
            "class": "Economy", 
            "flight": "|DL128|DL2492", 
            "departureAirport": "Beijing Capital International Airport", 
            "stops": "2+ stops", 
            "airline": "Delta Air Lines", 
            "to": "Seattle", 
            "arriveTime": "Wed Aug 17 18:46", 
            "departureTime": "Wed Aug 17 11:20", 
            "price": "|7524|7995", 
            "arrivalAirport": "Seattle–Tacoma International Airport", 
            "passengerType": "|NOR|STU", 
            "flightType": "|UNCERTAIN|Boeing 737-800"
        }, 
        {
            "from": "Beijing", 
            "duration": "20h55m", 
            "class": "Economy", 
            "flight": "|DL128|DL848", 
            "departureAirport": "Beijing Capital International Airport", 
            "stops": "2+ stops", 
            "airline": "Delta Air Lines", 
            "to": "Seattle", 
            "arriveTime": "Wed Aug 17 17:15", 
            "departureTime": "Wed Aug 17 11:20", 
            "price": "|7524|7995", 
            "arrivalAirport": "Seattle–Tacoma International Airport", 
            "passengerType": "|NOR|STU", 
            "flightType": "|UNCERTAIN|Boeing 737-800"
        }, 
        {
            "from": "Beijing", 
            "duration": "15h5m", 
            "class": "Economy", 
            "flight": "|BR715|BR026", 
            "departureAirport": "Beijing Capital International Airport", 
            "stops": "1 stop", 
            "airline": "EVA Air", 
            "to": "Seattle", 
            "arriveTime": "Wed Aug 17 19:50", 
            "departureTime": "Wed Aug 17 19:45", 
            "price": "|7675|7689", 
            "arrivalAirport": "Seattle–Tacoma International Airport", 
            "passengerType": "|NOR|NOR", 
            "flightType": "|Boeing 747-400|Boeing 777-300ER"
        }, 
        {
            "from": "Beijing", 
            "duration": "11h32m", 
            "class": "Economy", 
            "flight": "|DL128", 
            "departureAirport": "Beijing Capital International Airport", 
            "stops": "Nonstop", 
            "airline": "Delta Air Lines", 
            "to": "Seattle", 
            "arriveTime": "Wed Aug 17 07:52", 
            "departureTime": "Wed Aug 17 11:20", 
            "price": "|9332|10304|14104", 
            "arrivalAirport": "Seattle–Tacoma International Airport", 
            "passengerType": "|STU|NOR|NOR", 
            "flightType": "|Boeing767-300"
        }, 
        {
            "from": "Beijing", 
            "duration": "15h58m", 
            "class": "Economy", 
            "flight": "|UA889|UA322", 
            "departureAirport": "Beijing Capital International Airport", 
            "stops": "1 stop", 
            "airline": "United Airlines", 
            "to": "Seattle", 
            "arriveTime": "Wed Aug 17 12:58", 
            "departureTime": "Wed Aug 17 12:00", 
            "price": "|9649", 
            "arrivalAirport": "Seattle–Tacoma International Airport", 
            "passengerType": "|NOR", 
            "flightType": "|Boeing 747-400|Boeing 737-900"
        }, 
        {
            "from": "Beijing", 
            "duration": "30h20m", 
            "class": "Economy", 
            "flight": "|KE854|KE019", 
            "departureAirport": "Beijing Capital International Airport", 
            "stops": "1 stop", 
            "airline": "Korean Air", 
            "to": "Seattle", 
            "arriveTime": "Thu Aug 18 12:35", 
            "departureTime": "Wed Aug 17 21:15", 
            "price": "|9844|11822", 
            "arrivalAirport": "Seattle–Tacoma International Airport", 
            "passengerType": "|NOR|SEA", 
            "flightType": "|Airbus A330-300|Boeing 777-300ER"
        }, 
        {
            "from": "Beijing", 
            "duration": "13h54m", 
            "class": "Economy", 
            "flight": "|AC030|AC8097", 
            "departureAirport": "Beijing Capital International Airport", 
            "stops": "1 stop", 
            "airline": "Air Canada", 
            "to": "Seattle", 
            "arriveTime": "Wed Aug 17 14:59", 
            "departureTime": "Wed Aug 17 16:05", 
            "price": "|11626", 
            "arrivalAirport": "Seattle–Tacoma International Airport", 
            "passengerType": "|NOR", 
            "flightType": "|Boeing 777-300ER|TBD"
        }, 
        {
            "from": "Beijing", 
            "duration": "10h55m", 
            "class": "Economy", 
            "flight": "|HU495", 
            "departureAirport": "Beijing Capital International Airport", 
            "stops": "Nonstop", 
            "airline": "Hainan Airlines", 
            "to": "Seattle", 
            "arriveTime": "Wed Aug 17 11:35", 
            "departureTime": "Wed Aug 17 15:40", 
            "price": "|12996", 
            "arrivalAirport": "Seattle–Tacoma International Airport", 
            "passengerType": "|NOR", 
            "flightType": "|Airbus A330-300"
        }, 
        {
            "from": "Beijing", 
            "duration": "28h15m", 
            "class": "Economy", 
            "flight": "|CA983|VX781", 
            "departureAirport": "Beijing Capital International Airport", 
            "stops": "1 stop", 
            "airline": "", 
            "to": "Seattle", 
            "arriveTime": "Thu Aug 18 10:15", 
            "departureTime": "Wed Aug 17 21:00", 
            "price": "|13500", 
            "arrivalAirport": "Seattle–Tacoma International Airport", 
            "passengerType": "|NOR", 
            "flightType": "|Boeing 777-300ER|Airbus A319"
        }, 
        {
            "from": "Beijing", 
            "duration": "26h39m", 
            "class": "Economy", 
            "flight": "|MU5160|DL588", 
            "departureAirport": "Beijing Capital International Airport", 
            "stops": "1 stop", 
            "airline": "", 
            "to": "Seattle", 
            "arriveTime": "Thu Aug 18 09:19", 
            "departureTime": "Wed Aug 17 21:40", 
            "price": "|13733", 
            "arrivalAirport": "Seattle–Tacoma International Airport", 
            "passengerType": "|NOR", 
            "flightType": "|Airbus A320|Boeing767-300"
        }, 
        {
            "from": "Beijing", 
            "duration": "24h20m", 
            "class": "Economy", 
            "flight": "|CA981|UA1858", 
            "departureAirport": "Beijing Capital International Airport", 
            "stops": "1 stop", 
            "airline": "", 
            "to": "Seattle", 
            "arriveTime": "Wed Aug 17 22:20", 
            "departureTime": "Wed Aug 17 13:00", 
            "price": "|15409", 
            "arrivalAirport": "Seattle–Tacoma International Airport", 
            "passengerType": "|NOR", 
            "flightType": "|Boeing 747|Airbus A320"
        }, 
        {
            "from": "Beijing", 
            "duration": "15h44m", 
            "class": "Economy", 
            "flight": "|CA985|UA424", 
            "departureAirport": "Beijing Capital International Airport", 
            "stops": "1 stop", 
            "airline": "", 
            "to": "Seattle", 
            "arriveTime": "Wed Aug 17 16:24", 
            "departureTime": "Wed Aug 17 15:40", 
            "price": "|15409", 
            "arrivalAirport": "Seattle–Tacoma International Airport", 
            "passengerType": "|NOR", 
            "flightType": "|Boeing 747|Airbus A320"
        }, 
        {
            "from": "Beijing", 
            "duration": "37h5m", 
            "class": "Economy", 
            "flight": "|LH7321|LH490", 
            "departureAirport": "Beijing Capital International Airport", 
            "stops": "1 stop", 
            "airline": "Lufthansa", 
            "to": "Seattle", 
            "arriveTime": "Thu Aug 18 12:05", 
            "departureTime": "Wed Aug 17 14:00", 
            "price": "|17922", 
            "arrivalAirport": "Seattle–Tacoma International Airport", 
            "passengerType": "|NOR", 
            "flightType": "|Boeing 747|Boeing 747-400"
        }, 
        {
            "from": "Beijing", 
            "duration": "11h32m", 
            "class": "Economy", 
            "flight": "|CZ1155", 
            "departureAirport": "Beijing Capital International Airport", 
            "stops": "Nonstop", 
            "airline": "China Southern Airlines", 
            "to": "Seattle", 
            "arriveTime": "Wed Aug 17 07:52", 
            "departureTime": "Wed Aug 17 11:20", 
            "price": "|17927", 
            "arrivalAirport": "Seattle–Tacoma International Airport", 
            "passengerType": "|NOR", 
            "flightType": "|Boeing767-300"
        }, 
        {
            "from": "Beijing", 
            "duration": "14h40m", 
            "class": "Business", 
            "flight": "|NH956|DL166", 
            "departureAirport": "Beijing Capital International Airport", 
            "stops": "1 stop", 
            "airline": "", 
            "to": "Seattle", 
            "arriveTime": "Wed Aug 17 09:30", 
            "departureTime": "Wed Aug 17 09:50", 
            "price": "|21086", 
            "arrivalAirport": "Seattle–Tacoma International Airport", 
            "passengerType": "|NOR", 
            "flightType": "|Boeing-787|Boeing 747-400"
        }, 
        {
            "from": "Beijing", 
            "duration": "19h9m", 
            "class": "Business", 
            "flight": "|AA186|AA2674", 
            "departureAirport": "Beijing Capital International Airport", 
            "stops": "1 stop", 
            "airline": "American Airlines", 
            "to": "Seattle", 
            "arriveTime": "Wed Aug 17 14:19", 
            "departureTime": "Wed Aug 17 10:10", 
            "price": "|21644", 
            "arrivalAirport": "Seattle–Tacoma International Airport", 
            "passengerType": "|NOR", 
            "flightType": "|Boeing787-800|Boeing 737-800"
        }, 
        {
            "from": "Beijing", 
            "duration": "16h35m", 
            "class": "Economy", 
            "flight": "|NH956|NH178", 
            "departureAirport": "Beijing Capital International Airport", 
            "stops": "1 stop", 
            "airline": "ANA", 
            "to": "Seattle", 
            "arriveTime": "Wed Aug 17 11:25", 
            "departureTime": "Wed Aug 17 09:50", 
            "price": "|23922", 
            "arrivalAirport": "Seattle–Tacoma International Airport", 
            "passengerType": "|NOR", 
            "flightType": "|Boeing787-800|Boeing787-800"
        }, 
        {
            "from": "Beijing", 
            "duration": "28h", 
            "class": "Business", 
            "flight": "|EK307|EK229", 
            "departureAirport": "Beijing Capital International Airport", 
            "stops": "1 stop", 
            "airline": "Emirates", 
            "to": "Seattle", 
            "arriveTime": "Thu Aug 18 12:55", 
            "departureTime": "Wed Aug 17 23:55", 
            "price": "|41458", 
            "arrivalAirport": "Seattle–Tacoma International Airport", 
            "passengerType": "|NOR", 
            "flightType": "|Airbus A380-800|Boeing 777-300ER"
        }
    ], 
    "count": 19, 
    "date": "2016-08-17"
}

PHPExcel把JSON写入到Excel

<?php
require_once dirname(__FILE__) . '/Classes/PHPExcel.php';

$filename = "./jsonfile/2016-08-17.json";
$json_string = file_get_contents($filename);
//echo print_r($json_string,true);            //打印文件的内容
//echo "<br>";

$array = json_decode($json_string,true);
if (!is_array($array)){
    die('no successful');
}

// echo "<pre>";
// print_r($array);
// echo "</pre>";

$flightsArray = $array['flights'];
// echo "<pre>";
// print_r($flightsArray);
// echo "</pre>";

$sortFlightsArray = array();
for ($i=0; $i < count($flightsArray); $i++) { 
    # code...
    $tempArray = array();
    $tempArray['from'] = $flightsArray[$i]['from'];
    $tempArray['to'] = $flightsArray[$i]['to'];
    $tempArray['departureAirport'] = $flightsArray[$i]['departureAirport'];
    $tempArray['departureTime'] = $flightsArray[$i]['departureTime'];
    $tempArray['arrivalAirport'] = $flightsArray[$i]['arrivalAirport'];
    $tempArray['arriveTime'] = $flightsArray[$i]['arriveTime'];
    $tempArray['duration'] = $flightsArray[$i]['duration'];
    $tempArray['airline'] = $flightsArray[$i]['airline'];
    $tempArray['flightType'] = $flightsArray[$i]['flightType'];
    $tempArray['flight'] = $flightsArray[$i]['flight'];
    $tempArray['stops'] = $flightsArray[$i]['stops'];
    $tempArray['class'] = $flightsArray[$i]['class'];
    $tempArray['price'] = $flightsArray[$i]['price'];
    $tempArray['passengerType'] = $flightsArray[$i]['passengerType'];
    $sortFlightsArray[$i] = $tempArray;
}

// echo "<pre>";
// print_r($sortFlightsArray);
// echo "</pre>";

//********************************************************************************
    
// Create new PHPExcel object
$objPHPExcel = new PHPExcel();

// Fill worksheet from values in array
$objPHPExcel->getActiveSheet()->fromArray($sortFlightsArray, null, 'A1');

// Rename worksheet
$objPHPExcel->getActiveSheet()->setTitle('Members');

// Set AutoSize for name and email fields
$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setAutoSize(true);
$objPHPExcel->getActiveSheet()->getColumnDimension('B')->setAutoSize(true);


// Save Excel 2007 file
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save("./2016-08-17.xlsx");

?>

生成的Excel文件

PHPExcel生成的excel文件.png

参考资料:

https://github.com/PHPOffice/PHPExcel/issues/716

Fatal error: 'break' not in the 'loop' or 'switch' context in 
<mypath>\PHPExcel\PHPExcel\Calculation\Functions.php on line 581

需要把PHPExcel官方库的Functions.php代码的581行break删掉
需要把PHPExcel官方库的Functions.php代码的581行 break; 删掉.png

http://stackoverflow.com/questions/21436949/phpexcel-writer-exception-with-message-could-not-close-zip-file-php-output

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 204,793评论 6 478
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 87,567评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 151,342评论 0 338
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,825评论 1 277
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,814评论 5 368
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,680评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,033评论 3 399
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,687评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 42,175评论 1 300
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,668评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,775评论 1 332
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,419评论 4 321
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,020评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,978评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,206评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,092评论 2 351
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,510评论 2 343

推荐阅读更多精彩内容