使用数据库中的数据填写PHPEXCEL中的矩阵

时间:2014-12-28 10:22:58

标签: php phpexcel

我在PHP中创建了一个日历,向我的老板展示我的假期。

日历效果很好。

我必须以这种格式将日历数据导出到Excel工作表中:

enter image description here

如果我在3月9日休假,我想知道如何在09年3月在这张桌子上放一个十字架。

我试过这个,但我的方式似乎不是最好的方式:

我从第一个单元格开始浏览所有单元格,如果找到假期,我会放一个十字架。

修改 我在Mark的建议上更改了我的代码(见下文)

$sql = "SELECT id, nom, prenom FROM utilisateurs";
$fiches = $bdd->prepare($sql);
$users = $fiches->execute();
$user_number = 0;

$res = $fiches->fetchAll(PDO::FETCH_OBJ);
$ligne = 0;
foreach($res as $user)
{
  $ligne = 1;
  $user_number++;
  $objPHPExcel->createSheet($user_number);
  $objPHPExcel->setActiveSheetIndex($user_number);
  $objPHPExcel->getActiveSheet()->setTitle($user->nom);

  $ligne++;
  $num_jours = cal_days_in_month(CAL_GREGORIAN, $month, $annee_en_cours);

  $col = 'A';


     $sql = "SELECT jour, commentaire
             FROM reservations
             WHERE id_user = ".$user->id;
     $sql.= " ORDER BY jour";
     echo $sql.'<br />';
     $req = $bdd->prepare($sql);
     $execution = $req->execute();
     $res = $req->fetchAll(PDO::FETCH_OBJ);
     echo '<pre>';
     print_r($res);
     echo '</pre>';
     if (count($res) > 0)
     {



       $objPHPExcel->getActiveSheet()->SetCellValue($col.$ligne, $res[0]->jour);
     }
     echo $col.$ligne.'</br />';
     $col++;




  $ii = 1;
  for($col = 'A'; $col !== 'AF'; $col++)
  {
       $objPHPExcel->getActiveSheet()->SetCellValue($col.'1', $ii);

   }
}

我得到了这个:

SELECT jour, commentaire FROM reservations WHERE id_user = 57 ORDER BY jour

阵 (     [0] =&gt; stdClass对象         (             [jour] =&gt; 2014_08_14_AM             [commentaire] =&gt;         )

[1] => stdClass Object
    (
        [jour] => 2014_10_02_AM
        [commentaire] => 
    )

[2] => stdClass Object
    (
        [jour] => 2014_10_16_AM
        [commentaire] => 
    )

[3] => stdClass Object
    (
        [jour] => 2014_11_05_AM
        [commentaire] => 
    )

[4] => stdClass Object
    (
        [jour] => 2014_11_06_AM
        [commentaire] => 
    )

[5] => stdClass Object
    (
        [jour] => 2014_12_03_AM
        [commentaire] => 
    )

[6] => stdClass Object
    (
        [jour] => 2014_12_10_AM
        [commentaire] => 
    )

A2 SELECT jour,commentaire FROM reservations WHERE id_user = 58 ORDER BY jour

阵 (     [0] =&gt; stdClass对象         (             [jour] =&gt; 2014_06_03_AM             [commentaire] =&gt;         )

[1] => stdClass Object
    (
        [jour] => 2014_06_12_AM
        [commentaire] => 
    )

[2] => stdClass Object
    (
        [jour] => 2014_06_17_AM
        [commentaire] => 
    )

[3] => stdClass Object
    (
        [jour] => 2014_08_01_AM
        [commentaire] => 
    )

[4] => stdClass Object
    (
        [jour] => 2014_08_07_AM
        [commentaire] => 
    )

[5] => stdClass Object
    (
        [jour] => 2014_12_01_AM
        [commentaire] => test
    )

[6] => stdClass Object
    (
        [jour] => 2014_12_03_AM
        [commentaire] => 
    )

[7] => stdClass Object
    (
        [jour] => 2014_12_11_AM
        [commentaire] => test
    )

[8] => stdClass Object
    (
        [jour] => 2014_12_24_AM
        [commentaire] => test
    )

[9] => stdClass Object
    (
        [jour] => 2015_01_01_AM
        [commentaire] => 
    )

A2 SELECT jour,commentaire FROM reservations WHERE id_user = 59 ORDER BY jour

阵 (     [0] =&gt; stdClass对象         (             [jour] =&gt; 2014_12_03_AM             [commentaire] =&gt;         )

[1] => stdClass Object
    (
        [jour] => 2014_12_04_AM
        [commentaire] => 
    )

A2 世博会好,veuillez cliquer倒sauvegarder le文件 这不是最好的方法,你有什么好主意吗?

1 个答案:

答案 0 :(得分:1)

最好检索一年中所有假期的列表(一个单独的数据库查询返回所有假日行,而不是每个日期的365/366查询)并循环遍历该结果集....你应该是通过提取月份和日期组件,能够计算单元格地址以存储日期中的X