簡體   English   中英

PHPExcel寫Excel並保存到服務器?

[英]PHPExcel write Excel and save to the server?

我目前的代碼:

<?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";       
  }

?>

上面的代碼執行並保存文件夾中沒有.xlsx文件,但我得到的問題是在所有保存的excel文件中執行的for循環中的最大count(result1)

在深入研究之前,有一點是清楚的

你還沒有執行$ result的查詢,既沒有獲取它,也沒有在$ result1查詢中使用它,這是不可能的,

$ result1存在同樣的問題

您可以使用:

$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--

但最好是編寫JOIN而不是這兩個查詢

你會想要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
}

您可能還想查看mysql_fetch_assoc

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM