PHPExcel How to automatically create new excel file in every week routine?

tengxuanp :

In my site there is a form when users fill in data, the data will be written in a .xls file and stored in my pc. However, all of the data are stored under one .xls file which will eventually becomes very long and untidy. I want to make my system generate a new .xls file for every week so the data can be stored according to week.

This is my current code and I wonder is there any way to make it happen? thank you!

$objPHPExcel = PHPExcel_IOFactory::load("hii.xls");
$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');

$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']);
$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
      $from = "A1";
      $to = "G1";
$objPHPExcel->getActiveSheet()->getStyle("$from:$to")->getFont()->setBold(true);
$objWriter->save('hii.xls'); 
CodyKL :

This you can archive with DateTime() to determine the current week of the year.

This should then look like below.

// 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();
} else {
    // File exists, so we load this file
    $objPHPExcel = PHPExcel_IOFactory::load($file);
}

// Your 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');

$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']);
$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
$from = "A1";
$to = "G1";
$objPHPExcel->getActiveSheet()->getStyle("$from:$to")->getFont()->setBold(true);

// Save the excel file
$objWriter->save($file);

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=346969&siteId=1