Recently in the study of the YII framework of PHP, like, encountered the problem of exporting Excel, studied, there is the following method:
1, first add a reference to the Phpexcel in config\main.php, my way Is this:
12345678 |
// autoloading model and component classes 'import' => array ( 'application.models.*' , 'application.components.*' , 'application.extensions.phpexcel.*' , ), |
There are also people using the components of this configuration, but I have a problem, so use the above method.
2. Modify the autoloader.php file in the Phpexcel code directory according to the following code:
12345678910111213141516 |
public static function Register() {
$functions = spl_autoload_functions();
foreach (
$functions as $function
)
spl_autoload_unregister(
$function
);
$functions =
array_merge
(
array
(
array
(
'PHPExcel_Autoloader'
,
'Load'
)),
$functions
);
foreach (
$functions as $function
)
$x = spl_autoload_register(
$function
);
return $x
;
}
// function Register()
|
In the above function, the commented out is the original code.
3, the following code is the output of Excel, as well as some common property settings, in your controller:
public
function
actionExport()
{
$objectPHPExcel
=
new
PHPExcel();
$objectPHPExcel
->setActiveSheetIndex(0);
$page_size
= 52;
//数据的取出
$model
= Yii::app()->session[
'printdata'
];
$dataProvider
=
$model
->search();
$dataProvider
->setPagination(false);
$data
=
$dataProvider
->getData();
$count
=
$dataProvider
->getTotalItemCount();
//总页数的算出
$page_count
= (int)(
$count
/
$page_size
) +1;
$current_page
= 0;
$n
= 0;
foreach
(
$data
as
$product
)
{
if
(
$n
%
$page_size
=== 0 )
{
$current_page
=
$current_page
+1;
//报表头的输出
$objectPHPExcel
->getActiveSheet()->mergeCells(
'B1:G1'
);
$objectPHPExcel
->getActiveSheet()->setCellValue(
'B1'
,
'产品信息表'
);
$objectPHPExcel
->setActiveSheetIndex(0)->setCellValue(
'B2'
,
'产品信息表'
);
$objectPHPExcel
->setActiveSheetIndex(0)->setCellValue(
'B2'
,
'产品信息表'
);
$objectPHPExcel
->setActiveSheetIndex(0)->getStyle(
'B1'
)->getFont()->setSize(24);
$objectPHPExcel
->setActiveSheetIndex(0)->getStyle(
'B1'
)
->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objectPHPExcel
->setActiveSheetIndex(0)->setCellValue(
'B2'
,
'日期:'
.
date
(
"Y年m月j日"
));
$objectPHPExcel
->setActiveSheetIndex(0)->setCellValue(
'G2'
,
'第'
.
$current_page
.
'/'
.
$page_count
.
'页'
);
$objectPHPExcel
->setActiveSheetIndex(0)->getStyle(
'G2'
)
->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
//表格头的输出
$objectPHPExcel
->getActiveSheet()->getColumnDimension(
'A'
)->setWidth(5);
$objectPHPExcel
->setActiveSheetIndex(0)->setCellValue(
'B3'
,
'编号'
);
$objectPHPExcel
->getActiveSheet()->getColumnDimension(
'B'
)->setWidth(6.5);
$objectPHPExcel
->setActiveSheetIndex(0)->setCellValue(
'C3'
,
'名称'
);
$objectPHPExcel
->getActiveSheet()->getColumnDimension(
'C'
)->setWidth(17);
$objectPHPExcel
->setActiveSheetIndex(0)->setCellValue(
'D3'
,
'生产厂家'
);
$objectPHPExcel
->getActiveSheet()->getColumnDimension(
'D'
)->setWidth(22);
$objectPHPExcel
->setActiveSheetIndex(0)->setCellValue(
'E3'
,
'单位'
);
$objectPHPExcel
->getActiveSheet()->getColumnDimension(
'E'
)->setWidth(15);
$objectPHPExcel
->setActiveSheetIndex(0)->setCellValue(
'F3'
,
'单价'
);
$objectPHPExcel
->getActiveSheet()->getColumnDimension(
'F'
)->setWidth(15);
$objectPHPExcel
->setActiveSheetIndex(0)->setCellValue(
'G3'
,
'在库数'
);
$objectPHPExcel
->getActiveSheet()->getColumnDimension(
'G'
)->setWidth(15);
//设置居中
$objectPHPExcel
->getActiveSheet()->getStyle(
'B3:G3'
)
->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
//设置边框
$objectPHPExcel
->getActiveSheet()->getStyle(
'B3:G3'
)
->getBorders()->getTop()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
$objectPHPExcel
->getActiveSheet()->getStyle(
'B3:G3'
)
->getBorders()->getLeft()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
$objectPHPExcel
->getActiveSheet()->getStyle(
'B3:G3'
)
->getBorders()->getRight()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
$objectPHPExcel
->getActiveSheet()->getStyle(
'B3:G3'
)
->getBorders()->getBottom()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
$objectPHPExcel
->getActiveSheet()->getStyle(
'B3:G3'
)
->getBorders()->getVertical()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
//设置颜色
$objectPHPExcel
->getActiveSheet()->getStyle(
'B3:G3'
)->getFill()
->setFillType(PHPExcel_Style_Fill::FILL_SOLID)->getStartColor()->setARGB(
'FF66CCCC'
);
}
//明细的输出
$objectPHPExcel
->getActiveSheet()->setCellValue(
'B'
.(
$n
+4) ,
$product
->id);
$objectPHPExcel
->getActiveSheet()->setCellValue(
'C'
.(
$n
+4) ,
$product
->product_name);
$objectPHPExcel
->getActiveSheet()->setCellValue(
'D'
.(
$n
+4) ,
$product
->product_agent->name);
$objectPHPExcel
->getActiveSheet()->setCellValue(
'E'
.(
$n
+4) ,
$product
->unit);
$objectPHPExcel
->getActiveSheet()->setCellValue(
'F'
.(
$n
+4) ,
$product
->unit_price);
$objectPHPExcel
->getActiveSheet()->setCellValue(
'G'
.(
$n
+4) ,
$product
->library_count);
//设置边框
$currentRowNum
=
$n
+4;
$objectPHPExcel
->getActiveSheet()->getStyle(
'B'
.(
$n
+4).
':G'
.
$currentRowNum
)
->getBorders()->getTop()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
$objectPHPExcel
->getActiveSheet()->getStyle(
'B'
.(
$n
+4).
':G'
.
$currentRowNum
)
->getBorders()->getLeft()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
$objectPHPExcel
->getActiveSheet()->getStyle(
'B'
.(
$n
+4).
':G'
.
$currentRowNum
)
->getBorders()->getRight()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
$objectPHPExcel
->getActiveSheet()->getStyle(
'B'
.(
$n
+4).
':G'
.
$currentRowNum
)
->getBorders()->getBottom()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
$objectPHPExcel
->getActiveSheet()->getStyle(
'B'
.(
$n
+4).
':G'
.
$currentRowNum
)
->getBorders()->getVertical()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
$n
=
$n
+1;
}
//设置分页显示
//$objectPHPExcel->getActiveSheet()->setBreak( 'I55' , PHPExcel_Worksheet::BREAK_ROW );
//$objectPHPExcel->getActiveSheet()->setBreak( 'I10' , PHPExcel_Worksheet::BREAK_COLUMN );
$objectPHPE
http://www.bkjia.com/PHPjc/761580.html www.bkjia.com true http://www.bkjia.com/PHPjc/761580.html techarticle recently in the study of the YII framework of PHP, like, encountered the problem of exporting Excel, studied, there is the following method: 1, first in the config\main.php to add a reference to Phpexcel, I ...