PhpExcel create sheet for each result row PHP MySQ

2019-08-05 08:29发布

I have a question regarding creating one excel file only with PhpExcel, but many worksheets (or whatever you call the extra sheets all inside 1 excel file).

I am trying to export my php mysql data using phpexcel into excel file xlsx. There is a fair amount of data in my database. I would like to export data in each row in a separate worksheet inside the same excel file.

For example, in my database

1  John   Doe       aaa@bbb.com
2  James  Citizen   zzz@xxx.com
3 ...
4 ...
5...

... and so on

I would like to export to one excel file, but with each row in a separate sheet

Sample.xlsx
Sheet1: 1 John Doe  aaa@bbb.com
Sheet2: 2 James Citizen zzz@xxx.com
Sheet 3: 3.....
Sheet4: 4....
Sheet5: 5.....

And So on

Below is my code, It's not working as of yet sadly. All ideas appreciated!

Cheers,

/** Query */
    $query = "SELECT * FROM membership";

    if ($result = mysql_query($query) or die(mysql_error())) {
/** Create a new PHPExcel object 1.0 */
   $objPHPExcel = new PHPExcel();
   $objPHPExcel->getActiveSheet()->setTitle('Data');
   }  

/** Loop through the result set*/
    $rowNumber = 1; //start in cell 1
    $i=0;
    while ($row = mysql_fetch_row($result)) {
    while ($i < 10) {
       $col = 'A'; // start at column A
       foreach($row as $cell) {
       $objWorkSheet = $objPHPExcel->createSheet($i); //Setting index when creating

          $objPHPExcel->getActiveSheet()->setCellValue($col.$rowNumber,$cell,$i);
          $col++;
       }
       $rowNumber++;
}}

0条回答
登录 后发表回答