Magento后台,自定义导出一段时间内销售最好的商品

1.页面

Magento后台,自定义导出一段时间内销售最好的商品_第1张图片


\app\design\adminhtml\default\default\template\report\grid\container.phtml

 
getBlockHtml('formkey')?> 开始日期: 结束日期:
2.控制器里

\app\code\core\Mage\Adminhtml\controllers\Report\SalesController.php

 /**
     * @date 2016年3月4日
     * 导出一定日期范围内的销量最好的产品
     */
    public function exportAction()
    {
        $start_date = $_GET['start_date'];
        $end_date = $_GET['end_date'];
        $read = Mage::getSingleton('core/resource')->getConnection('core_read');
        $sql = "SELECT t1.period,t2.sku,t3.`value` AS price ,t1.product_name, SUM(t1.qty_ordered) AS qty_ordered
                from sales_bestsellers_aggregated_daily AS t1
                LEFT JOIN catalog_product_entity AS t2  ON t1.product_id=t2.entity_id
                LEFT JOIN catalog_product_entity_decimal AS t3 ON t1.product_id=t3.entity_id
                where t1.period >= '{$start_date}' AND t1.period <= '{$end_date}' AND t1.store_id=1 AND t3.attribute_id=75
                GROUP BY t1.product_id
                ORDER BY qty_ordered DESC
                LIMIT 300";
        $results = $read->fetchAll($sql);

        //导出到excel
        $this->exportExcel($results);
    }


    /*
    *+----------------------------------------------------------------------
    *   PHPExcel导出Excel表格
    *   array $rearr  需要导出的数组
    *+----------------------------------------------------------------------
    */
    protected function exportExcel($rearr) {
        $result = array(
            'SKU',
            'product_name',
            'qty_ordered',
            'price'
        );

        $arr = array(
            'A',
            'B',
            'C',
            'D'
        );

        //导入excel类,phpexcel库在lib/目录下
        include_once('PHPExcel/PHPExcel.php');
        // 创建一个excel
        $objPHPExcel = new PHPExcel();
        /****************************************设置居中开始**************************************/
        foreach ($arr as $key => $value) {
            $objPHPExcel->getActiveSheet()->getStyle($value)->getAlignment()->setHorizontal         (PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        }
        /****************************************设置居中结束**************************************/
        // 循环$arr定义的列设置每列内容居中
        $objPHPExcel->getProperties()->setCreator("Maarten Balliauw")->setLastModifiedBy("Maarten Balliauw")->setTitle("Office 2007 XLSX Test Document")->setSubject("Office 2007 XLSX Test Document")->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")->setKeywords("office 2007 openxml php")->setCategory("Test result file");

        /**************************************设置标题开始*****************************************/
        // 循环$arr定义的列和$result设置表头
        $objPHPExcel->setActiveSheetIndex(0);
        foreach ($arr as $key => $value) {
            $objPHPExcel->getActiveSheet()->setCellValue($value . "1", $result[$key]);
        }
        /**************************************设置标题结束*****************************************/

        /**************************************设置内容开始*****************************************/
        $objPHPExcel->setActiveSheetIndex(0);
        $i = 2;
        // $rearr需要导出的数据二维数组
        foreach ($rearr as $key => $value) {
            // 这里从二维数组里面通过键名获取到值放到相应的表格中
            $objPHPExcel->getActiveSheet()->setCellValue('A' . $i, $value['sku']);
            $objPHPExcel->getActiveSheet()->setCellValue('B' . $i, $value['product_name']);
            $objPHPExcel->getActiveSheet()->setCellValue('C' . $i, $value['qty_ordered']);
            $objPHPExcel->getActiveSheet()->setCellValue('D' . $i, $value['price']);
            $i++;
        }
        /**************************************设置内容结束*****************************************/

        /**************************************设置宽度开始*****************************************/
        // 循环$arr定义的列设置每列宽度
        foreach ($arr as $key => $value) {
            if($value == 'B'){
                $objPHPExcel->getActiveSheet()->getColumnDimension($value)->setWidth(70);
            }else{
                $objPHPExcel->getActiveSheet()->getColumnDimension($value)->setWidth(20);
            }

        }
        /**************************************设置宽度结束*****************************************/

        //导出的文件名
        $filename = 'qty_ordered'.date('Y-m-d',time()).'.xls';
        /**************************************设置导出下载开始*****************************************/
        $objPHPExcel->getSheet(0)->setTitle('phpexcel'); // 工作区域标题
        header('Content-Type: application/vnd.ms-excel');
        header("Content-Disposition: attachment;filename=$filename");//导出文件
        header('Cache-Control: max-age=0');
        header('Cache-Control: max-age=1');
        header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
        header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); // always modified
        header('Cache-Control: cache, must-revalidate'); // HTTP/1.1
        header('Pragma: public'); // HTTP/1.0
        $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
        $objWriter->save('php://output');//导出文件
        /**************************************设置导出下载结束*****************************************/
    }

你可能感兴趣的:(Magento)