Yii2-Как добавить два листа Excel в один файл Excel - PullRequest
0 голосов
/ 10 мая 2018

Я работаю с swift mail в yii2.Я отправляю письмо с приложением csv.Файл содержит один лист.Теперь я хочу добавить два листа в один файл.

  public function actionEmail()
{
    $sql = "SELECT COUNT(DISTINCT od.`meter_serial`) AS OGP_Created, 
            COUNT(DISTINCT mp.`meter_id`) AS Un_Verified_Meters, 
            COUNT(DISTINCT ins.`meter_msn`) AS Verified_Meters, 
            sd.`sub_div_code` AS SD_Code,sd.`name` AS SD_Name
            FROM `ogp_detail` od
            INNER JOIN `survey_hesco_subdivision` sd ON od.`sub_div` = sd.`sub_div_code`
            LEFT JOIN `meter_ping` mp ON od.`meter_id` = mp.`meter_id`
            LEFT JOIN `installations` ins ON od.`meter_serial` = ins.`meter_msn`
            WHERE od.`meter_type` = '3-Phase'
            GROUP BY sd.`name`";

    $query= "SELECT COUNT(DISTINCT od.`meter_serial`) AS OGP_Created, 
             COUNT(DISTINCT mp.`meter_id`) AS Un_Verified_Meters, 
             COUNT(DISTINCT ins.`meter_msn`) AS Verified_Meters, 
             sd.`sub_div_code` AS SD_Code,sd.`name` AS SD_Name
             FROM `ogp_detail` od
             INNER JOIN `survey_hesco_subdivision` sd ON od.`sub_div` = 
             sd.`sub_div_code`
             LEFT JOIN `meter_ping` mp ON od.`meter_id` = mp.`meter_id`
             LEFT JOIN `installations` ins ON od.`meter_serial` = 
             ins.`meter_msn`
             WHERE od.`meter_type` = 'L.T.TOU'
             GROUP BY sd.`name`";

    $result = Yii::$app->db->createCommand($sql)->queryAll();

    $result_set = Yii::$app->db->createCommand($query)->queryAll();

    //create a csv file
    $filename   =   $this->getAttachment($result,$result_set);

    //send email
    $this->sendEmail($filename);


    return $this->redirect(['index']);
}

Ниже моя getAttachment функция

 public function getAttachment($results,$result_set) {
    $sum_OGP = 0;
    $sum_UnVerified = 0;
    $sum_Verified = 0;

    $filename = Yii::getAlias('@webroot') . DIRECTORY_SEPARATOR . '3-Phase Breakdown List ' . date('d-m-y') . '.csv';

    //open a csv file
    $file = fopen($filename, "w");

    $headerInjected = false;
    $header = ['OGP Created', '3-Phase Un Verified Meters', '3-Phase Verified Meters', 'Sub Div Code', 'Sub Div Name'];

    //write lines to the csv file
    foreach ($results as $result) {
        if (!$headerInjected) {
            $headerInjected = true;
            fputcsv($file, $header);
        }
        fputcsv($file, $result);
        $sum_OGP +=$result['OGP_Created'];
        $sum_UnVerified +=$result['Un_Verified_Meters'];
        $sum_Verified +=$result['Verified_Meters'];
    }

    //add the sum in the last row
    fputcsv($file,["Total OGP","Total Unverified Meters","Total Verified Meters"]);
    fputcsv($file,[$sum_OGP,$sum_UnVerified,$sum_Verified]);

    //close the file handle
    fclose($file);
    return $filename;
}

Я хочу добавить содержимое $result_set, header для обоих будет одинаковым, содержимое fputcsv будетбудь другим.

Как я могу добавить их обоих в один файл Excel с двумя листами?

Любая помощь будет высоко оценена.

...