Exemplo n.º 1
0
 public function actionDownload()
 {
     $model = new Coupon();
     if (isset($_GET['filter'])) {
         $filter = strval($_GET['filter']);
     }
     if (isset($_GET['value'])) {
         $value = intval($_GET['value']);
     }
     $result = $model->getFilterResult($filter, $value);
     //$result = Coupon::$lastresult;
     // Instantiate a new PHPExcel object
     $objPHPExcel = new \PHPExcel();
     // Set the active Excel worksheet to sheet 0
     $objPHPExcel->setActiveSheetIndex(0);
     // Iterate through each result from the SQL query in turn
     // We fetch each database result row into $row in turn
     $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(4);
     $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(20);
     $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(40);
     $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(40);
     //Make Headings
     $objPHPExcel->getActiveSheet()->setCellValue('A1', 'S.No')->setCellValue('B1', 'Vendor')->setCellValue('C1', 'Coupon Title')->setCellValue('D1', 'Coupon Details');
     // Initialise the Excel row number
     $rowCount = 2;
     foreach ($result as $row) {
         // Set cell An to the "name" column from the database (assuming you have a column called name)
         //    where n is the Excel row number (ie cell A1 in the first row)
         $objPHPExcel->getActiveSheet()->SetCellValue('A' . $rowCount, $rowCount - 1);
         // Set cell Bn to the "age" column from the database (assuming you have a column called age)
         //    where n is the Excel row number (ie cell A1 in the first row)
         $objPHPExcel->getActiveSheet()->SetCellValue('B' . $rowCount, $row->website->WebsiteName);
         $objPHPExcel->getActiveSheet()->SetCellValue('C' . $rowCount, $row->Title);
         $objPHPExcel->getActiveSheet()->SetCellValue('D' . $rowCount, $row->Description);
         // Increment the Excel row counter
         $rowCount++;
     }
     // Instantiate a Writer to create an OfficeOpenXML Excel .xlsx file
     $objWriter = new \PHPExcel_Writer_Excel2007($objPHPExcel);
     // Write the Excel file to filename some_excel_file.xlsx in the current directory
     $objWriter->save('CouponData.xlsx');
 }