Phpexcel-满足条件(值更改)时触发功能

问题描述 投票:0回答:1

我已经具有发送电子邮件功能(抱歉,由于某些隐私问题,我在这里无法显示它)和周数功能。然而,我希望在“星期”列中的值更改时发送电子邮件(例如:第10周-> 11)。

我目前能想到的是编写一个if语句,如果星期值与最后一个值不同,则执行发送电子邮件功能,否则不发送。我想知道它是正确的,如果有人可以告诉我该怎么做,将不胜感激。谢谢!

// Get the current week of year

$date = new DateTime('now');
$week = $date->format("W");

// Path to the file storage 
$path = __DIR__;

// Set file name for excel file and combine with the path
$file = 'hii.xls';

// Determine if file exists
if (!is_file($file)) {
    // File doesn't exists, so create a new file
    $objPHPExcel = new PHPExcel();
} else {
    // File exists, so load this file
    $objPHPExcel = PHPExcel_IOFactory::load($file);
}

// Existing code to write the data
$objPHPExcel->setActiveSheetIndex(0);
$objPHPExcel->getActiveSheet()->setCellValue('A1','Name of Stakeholder(s)');
$objPHPExcel->getActiveSheet()->setCellValue('B1','NRIC No./Passport No./Company No.');
$objPHPExcel->getActiveSheet()->setCellValue('C1','Email');
$objPHPExcel->getActiveSheet()->setCellValue('D1','Contact Number');
$objPHPExcel->getActiveSheet()->setCellValue('E1','CDS Account No.');
$objPHPExcel->getActiveSheet()->setCellValue('F1','Correspondence Address');
$objPHPExcel->getActiveSheet()->setCellValue('G1','Date Submitted');
$objPHPExcel->getActiveSheet()->setCellValue('H1','Week');

$row = $objPHPExcel->getActiveSheet()->getHighestRow()+1;
$objPHPExcel->getActiveSheet()->SetCellValue('A'.$row, $_POST['name']);
$objPHPExcel->getActiveSheet()->SetCellValue('B'.$row, $_POST['ic']);
$objPHPExcel->getActiveSheet()->SetCellValue('C'.$row, $_POST['email']);
$objPHPExcel->getActiveSheet()->SetCellValue('D'.$row, $_POST['phone']);
$objPHPExcel->getActiveSheet()->SetCellValue('E'.$row, $_POST['cds']);
$objPHPExcel->getActiveSheet()->SetCellValue('F'.$row, $_POST['address']);
$objPHPExcel->getActiveSheet()->SetCellValue('G'.$row, $_POST['date']);
$objPHPExcel->getActiveSheet()->SetCellValue('H'.$row, $week);
$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
$from = "A1";
$to = "H1";
$objPHPExcel->getActiveSheet()->getStyle("$from:$to")->getFont()->setBold(true);

// Save the excel file
$objWriter->save($file);
php html phpexcel
1个回答
0
投票

基于我对您的last question的回答以发送旧的生成的Excel文件的示例。

如果您只想发送新生成的文件,则需要澄清您的问题。

// Get the current week of year
$date = new DateTime('now');
$week = $date->format("W");

// Path to the file storage (in this case the same folder as this script)
$path = __DIR__;

// Set file name for excel file and combine with the path
$file = $path . '/week-' . $week . '.xls';

// Determine if file exists
if (!is_file($file)) {
    // File doesn't exists, so we create a new file
    $objPHPExcel = new PHPExcel();

    // Determine old file for e-mail
    $week = $week-1; // decrease week number
    $oldFile = $path . '/week-' . $week . '.xls';
    if (is_file($oldFile) {
        /* 
         * Here your write your code to send out 
         * the e-mail with the old file as attachment
         */
    }
} else {
    // File exists, so we load this file
    $objPHPExcel = PHPExcel_IOFactory::load($file);
}
© www.soinside.com 2019 - 2024. All rights reserved.