1. 程式人生 > >PHPExcel 匯出 (在iwebshop裡寫的)

PHPExcel 匯出 (在iwebshop裡寫的)

 

<?php 
include(“phpexcel / PHPExcel.php”);
include(“phpexcel / PHPExcel / IOFactory.php”);
include(“phpexcel / PHPExcel / Writer / Excel2007.php”);
 
class comm extends pluginBase
{
    / **
     *匯出
     * /
    public function excelout(){
        //例項化表格
        $ objPHPExcel = new PHPExcel();
        //根據excel座標,新增資料
        $ objPHPExcel-> setActiveSheetIndex(0)
            - > setCellValue('A1','id')
            - > setCellValue('B1','文章名稱')
            - > setCellValue('C1','評論內容');
 
        //例項化表查詢(用的兩表聯查)
        $ comm = new IModel('comm,article');
        $ ARR = $ comm->查詢();
 
        //從第二行開始匯入的資料
        $ I = 2;
        foreach($ arr as $ value){
            $ objPHPExcel-> getActiveSheet() - > setCellValue('A'。$ i,$ value ['id']);
            $ objPHPExcel-> getActiveSheet() - > setCellValue( 'B' $ I,$值[ '標題']);
            $ objPHPExcel-> getActiveSheet() - > setCellValue( 'C' $ I,$值[ '內容']);
            $ I ++;
        }
 
         //方法一:
        //出現的一個下載的框匯出表格
        header('Content-Type:application / vnd.ms -excel');
        標題('Content-Disposition:attachment; filename =白雅文.xlsx');
        header('Cache-Control:max-age = 0');
        $ write = new PHPExcel_Writer_Excel2007($ objPHPExcel);
        $直寫>儲存(“PHP://輸出');
 
 
        //方法二
        //直接匯出表格  
        //儲存Excel 2007格式檔案,儲存路徑為當前路徑,名字為export.xlsx
        // $ objWriter = PHPExcel_IOFactory :: createWriter($ objPHPExcel,'Excel2007');
        // $ objWriter-> save('export.xlsx');
    }
}