【功能点】php导出excel

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qq_33862644/article/details/80356873

/**
 * 商品库存excel导出
 */
public function excel()
{
    //查询出要导出的数据
    $data = $this->request->param('ids/a');     //接收参数(用户勾选多个id)
    $model = new StockModel();
    $res = $model->excelSelect($data);                //查数据
 
    date_default_timezone_set('Asia/shanghai'); //设置时区,否则有警告
 
    $objPHPExcel = new PHPExcel();                    //composer下来的
 
    // 设置文件属性
    $objPHPExcel->getProperties()->setCreator("xiGua")//设置作者
    ->setLastModifiedBy("xiGua")                        //设置最后一个修改的人
    ->setTitle("库存报表")      //设置标题
    ->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");//EXCEL 类别
 
    $objPHPExcel->setActiveSheetIndex(0)
        ->setCellValue("A1", '通驰库存')
        ->mergeCells("A1:F1");      //合并单元格
    //设置行第一行高度为25px
    $objPHPExcel->getActiveSheet()->getRowDimension(1)->setRowHeight(25);
    $objPHPExcel->getActiveSheet() //设置行高 居中
    ->getStyle('A1')->getAlignment()
        ->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
 
    //添加数据
    $objPHPExcel->setActiveSheetIndex(0)  //指定在当前活动的表格中添加
    ->setCellValue('A2', '品牌')   //在xx中插入
    ->setCellValue('B2', '型号')
    ->setCellValue('C2', '进价')
    ->setCellValue('D2', '进货数量')
    ->setCellValue('E2', '现库存')
    ->setCellValue('F2', '进货时间');
 
    $num_key = 3;                        //第1行是合并后的标题,第2行是列名,所以数据需要从第3行开始
    foreach($res as $value){
        $num_key++;
        $objPHPExcel->setActiveSheetIndex(0)  //库中查出来的数据放这
        ->setCellValue('A'.$num_key, $value['brand'])
        ->setCellValue('B'.$num_key, $value['version'])
        ->setCellValue('C'.$num_key, $value['stock_bid'])
        ->setCellValue('D'.$num_key, $value['num'])
        ->setCellValue('E'.$num_key, $value['remnant_inventory'])
        ->setCellValue('F'.$num_key, date("Y-m-d H:i:s",$value['update_time']));
    }
 
 
 
    // Rename worksheet
    $objPHPExcel->getActiveSheet()->setTitle('标签1');
 
 
    // Set active sheet index to the first sheet, so Excel opens this as the first sheet
    $objPHPExcel->setActiveSheetIndex(0);
 
    ob_clean();
    // Redirect output to a client’s web browser (Excel5)
    header('Content-Type: application/vnd.ms-excel');
    header('Content-Disposition: attachment;filename="通驰库存.xls"'); //下载文件名字
    header('Cache-Control: max-age=0');
    // If you're serving to IE 9, then the following may be needed
    header('Cache-Control: max-age=1');
 
    // If you're serving to IE over SSL, then the following may be needed
    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');
    //exit;
}
 
 

 

posted @ 2019-08-17 10:11  御世制人  阅读(289)  评论(0编辑  收藏  举报