开发者

PHPExcel write Excel and save to the server?

The code I currently have:

<?php

  /** PHPExcel */
  require_once '../Classes/PHPExcel.php';

  /** PHPExcel_IOFactory */
  require_once '../Classes/PHPExcel/IOFactory.php';

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

  // Set properties
  $objPHPExcel->getProperties()->setCreator("Maarten Balliauw")
                               ->setLastModifiedBy("Maarten Balliauw")
                               ->setTitle("Office开发者_开发问答 2007 XLSX Test Document")
                               ->setSubject("Office 2007 XLSX Test Document")
                               ->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
                               ->setKeywords("office 2007 openxml php")
                               ->setCategory("Test result file");

  $result = 'select * from table1';

  for($i=0;$i<count($result);$i++) {

    $result1 = 'select * from table2 where table1_id = ' . $result[$i]['table1_id'];

    for ($j=0;$j<count($result1);$j++) {
      $objPHPExcel->setActiveSheetIndex(0)->setCellValue('A' . $j, $result1[$j]['name']);
    }

    // Set active sheet index to the first sheet, so Excel opens this as the first sheet
    $objPHPExcel->setActiveSheetIndex(0);

    // Save Excel 2007 file
    $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
    $objWriter->save(str_replace('.php', '.xlsx', __FILE__));

    // Echo done
    echo date('H:i:s') . " Done writing file.\r\n";       
  }

?>

The above code executes and save n no of .xlsx files in the folder, but the problem I'm getting is biggest count(result1) in the for loop executing in all saved excel files.


Before going deep, one thing is clear

You haven't executed the query for $result, neither fetch it and on $result1 query you are using it which is not possible,

Same problem exist with $result1

You can use:

$result = mysql_query('select * from table1');
while($fetch_result=mysql_fetch_array($result))
{
    $result2 = mysql("select * from table2 where table1_id = '".$fetch_result['table1_id']."'");

   while($fetch_result2=mysql_fetch_array($result2)
   {
    --your code--

But it is preferable to write JOIN instead of these two query


You will want mysql_num_rows - http://php.net/manual/en/function.mysql-num-rows.php

$q = 'select * from table1';
$res = mysql_query($q);
$count = mysql_num_rows($result);

for ($j=0;$j<$count;$j++) {
    #code here
}

you may also want to look at mysql_fetch_assoc

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜