用一些标题写入数组结果到excel表

时间:2014-05-14 08:40:29

标签: php excel

这里我从mysql db获得了一些数组形式的结果。我想将整个结果写入excel文件。

结果是循环,因此结果应该追加到循环结束。

    for($i = 5; $i > 0 ; $i--)
    {
        $select_words = mysqli_query($con,"SELECT * from review_details where rate = $i and category = 'italian' and isApi = 0");
        while ($row1 = @mysqli_fetch_array($select_words))
        {
            //echo $row1[review]."<br>";
//          echo $row1[rate]."<br>";
            $word.=$row1[adjective].",";        
        }
        //echo "Words are : $word";
        echo "<br/><br/><strong> $i star word count </strong>"; // want to write this in excel
        $word_count = array_count_values(str_word_count($word,1));
        arsort($word_count);
        echo "<br>";
        echo "<br>";
        //var_dump($word_count);
        $word_count2 = array_slice($word_count,0,20);
        print_r($word_count2); //Writing this to excel

        unset($word);
        unset($word_count);
        unset($select_words);
    }

结果如下:

Category : Italian

5 star word count

Array ( [Italian] => 1754 [good] => 1600 [great] => 1514 [delicious] => 923 [amazing] => 740 [fresh] => 596 [nice] => 486 [perfect] => 478 [little] => 467 [favorite] => 463 [other] => 451 [excellent] => 437 [special] => 395 [small] => 388 [wonderful] => 380 [much] => 342 [Great] => 314 [sure] => 301 [last] => 300 [fantastic] => 292 )

4 star word count

Array ( [good] => 2215 [Italian] => 1388 [great] => 1261 [delicious] => 791 [little] => 706 [nice] => 668 [fresh] => 465 [small] => 447 [other] => 427 [much] => 412 [tasty] => 357 [excellent] => 352 [amazing] => 312 [perfect] => 301 [special] => 297 [next] => 290 [few] => 248 [Great] => 243 [sure] => 241 [attentive] => 233 )

3 star word count

Array ( [good] => 1017 [Italian] => 470 [great] => 386 [nice] => 288 [little] => 271 [other] => 264 [much] => 231 [small] => 198 [special] => 161 [delicious] => 145 [tasty] => 130 [bad] => 127 [sure] => 118 [fresh] => 115 [decent] => 114 [next] => 109 [italian] => 109 [few] => 109 [average] => 95 [many] => 94 )

2 star word count

Array ( [good] => 342 [Italian] => 222 [great] => 144 [other] => 122 [much] => 114 [nice] => 112 [little] => 104 [bad] => 101 [small] => 93 [special] => 84 [bland] => 67 [long] => 60 [many] => 53 [fresh] => 53 [next] => 46 [mediocre] => 45 [few] => 45 [tasty] => 45 [high] => 44 [sure] => 43 )

1 star word count
Array ( [good] => 150 [Italian] => 104 [bad] => 87 [other] => 66 [great] => 55 [much] => 50 [few] => 48 [next] => 39 [many] => 36 [special] => 35 [little] => 33 [small] => 32 [last] => 32 [same] => 30 [nice] => 30 [long] => 30 [rude] => 29 [sure] => 29 [bland] => 29 [such] => 26 ) 

这是一种可以在每次测试后给出excel结果的方法:

$filename ="data.xls";
header('Content-type: application/ms-excel');
header('Content-Disposition: attachment; filename='.$filename);

但我不知道它会如何将上述结果写入此文件。

更新

我根据答案更新了以下代码:

        $fp = fopen('file.xlsx', 'w');

        foreach ($word_count2 as $fields)
        {
            fputcsv($fp, $fields);
        }
        fclose($fp);

file.xlsx不包含任何数据。我还使用了file.csv,它还包含0个字节的数据。

2 个答案:

答案 0 :(得分:1)

PHP函数fputcsv (PHP Docs)将数组作为CSV写入打开的文件处理程序。

使用示例:

<?php

$list = array (
    array('aaa', 'bbb', 'ccc', 'dddd'),
    array('123', '456', '789'),
    array('"aaa"', '"bbb"')
);

$fp = fopen('file.csv', 'w');

foreach ($list as $fields) {
    fputcsv($fp, $fields);
}

fclose($fp);
?>

(取自PHP Doc)

所以基本上你迭代你的数组并将每一行作为数组写入文件。

答案 1 :(得分:0)

我可以这样做:

        $file = fopen("words.xls","w+"); //clear the content every time before start writing. To see new content everytime
        fclose($file);
        for($i = 5; $i > 0 ; $i--)
        {
            $select_words = mysqli_query($con,"SELECT * from review_details where rate = $i and category = 'italian' and isApi = 0");
            while ($row1 = @mysqli_fetch_array($select_words))
            {
                //echo $row1[review]."<br>";
    //          echo $row1[rate]."<br>";
                $word.=$row1[adjective].",";        
            }
            //echo "Words are : $word";
            echo "<br/><br/><strong> $i star word count </strong>";
            $word_count = array_count_values(str_word_count($word,1));
            arsort($word_count);
            echo "<br>";
            echo "<br>";
            //var_dump($word_count);
            $word_count2 = array_slice($word_count,0,20);
            print_r($word_count2);

            unset($word);
            unset($word_count);
            unset($select_words);
            file_put_contents('words.xls', print_r($word_count2, true), FILE_APPEND | LOCK_EX);
        }