You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
100 lines
4.0 KiB
PHTML
100 lines
4.0 KiB
PHTML
2 years ago
|
<?php
|
||
|
|
||
|
include('PHPExcel/Classes/PHPExcel.php');
|
||
|
include('PHPExcel/Classes/PHPExcel/IOFactory.php');
|
||
|
// include('PHPExcel/Classes/PHPExcel/Border.php');
|
||
|
// include('PHPExcel/Classes/PHPExcel/Color.php');
|
||
|
// include('PHPExcel/Classes/PHPExcel/Fill.php');
|
||
|
|
||
|
// use PHPExcel_IOFactory;
|
||
|
// use PHPExcel_Style_Border;
|
||
|
// use PHPExcel_Style_Color;
|
||
|
// use PHPExcel_Style_Fill;
|
||
|
|
||
|
function exportOrderExcel($columns, $rows) {
|
||
|
|
||
|
$objPHPExcel = new PHPExcel();
|
||
|
|
||
|
|
||
|
|
||
|
// $objPHPExcel->getActiveSheet()->getStyle()->getFont()->setSize(14);
|
||
|
// $objPHPExcel->getActiveSheet()->getStyle()->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER)->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
|
||
|
|
||
|
//设置一整行的颜色,字体等样式.
|
||
|
// $objPHPExcel->getActiveSheet()->getStyle("2")->getFont()->setBold(true)->setSize(25);
|
||
|
// $objPHPExcel->getActiveSheet()->getStyle('6')->getFont()->setBold(true)->setSize(27)->getColor()->setRGB('FF4500');
|
||
|
// 如果是一整行,比如第六行,getStyle('6'),特殊行设置 getStyle('A6:E6')
|
||
|
// $objPHPExcel->getActiveSheet()->getStyle('A6:E6')->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID)->getStartColor()->setRGB("E066FF");
|
||
|
|
||
|
|
||
|
//设置单元格边框的颜色「所有的」
|
||
|
// $objPHPExcel->getActiveSheet()->getStyle('A1:E6')->getBorders()->getAllBorders()->setBorderStyle(PHPExcel_Style_Border::BORDER_THICK)->getColor()->setRGB('0A0A0A');
|
||
|
|
||
|
// 设置单个边框 border 的颜色
|
||
|
// $objPHPExcel->getActiveSheet()->getStyle('E6')->getBorders()->getAllBorders()->setBorderStyle(PHPExcel_Style_Border::BORDER_THICK)->getColor()->setRGB('00FA9A');
|
||
|
|
||
|
// 设置 border 边框的颜色
|
||
|
// $objPHPExcel->getActiveSheet()->getStyle('A1:E6')->getBorders()->getTop()->setBorderStyle(PHPExcel_Style_Border::BORDER_THICK)->getColor()->setRGB('141414');
|
||
|
// $objPHPExcel->getActiveSheet()->getStyle('A1:E6')->getBorders()->getRight()->setBorderStyle(PHPExcel_Style_Border::BORDER_THICK)->getColor()->setRGB('141414');
|
||
|
// $objPHPExcel->getActiveSheet()->getStyle('A1:E6')->getBorders()->getBottom()->setBorderStyle(PHPExcel_Style_Border::BORDER_THICK)->getColor()->setRGB('141414');
|
||
|
// $objPHPExcel->getActiveSheet()->getStyle('A1:E6')->getBorders()->getLeft()->setBorderStyle(PHPExcel_Style_Border::BORDER_THICK)->getColor()->setRGB('141414');
|
||
|
|
||
|
|
||
|
$active = $objPHPExcel->getActiveSheet();
|
||
|
|
||
|
$active->getStyle("1")->getFont()->setBold(true);
|
||
|
|
||
|
$column = 0;
|
||
|
foreach ($columns as $col)
|
||
|
{
|
||
|
$active->setCellValueByColumnAndRow($column, 1, $col['title']);
|
||
|
if (isset($col['width']))
|
||
|
{
|
||
|
$active->getColumnDimensionByColumn($column)->setWidth($col['width']);
|
||
|
}
|
||
|
$column++;
|
||
|
}
|
||
|
|
||
|
// $active->getDefaultRowDimension()->setRowHeight(30); // 设置所有单元格(行)的默认高度
|
||
|
// $active->getDefaultColumnDimension()->setWidth(100); // 设置所有单元格(列)的默认宽度
|
||
|
|
||
|
$rowIdx = 2;
|
||
|
foreach($rows as $row)
|
||
|
{
|
||
|
$column = 0;
|
||
|
foreach ($columns as $col)
|
||
|
{
|
||
|
if (isset($col['fmt']))
|
||
|
{
|
||
|
$active->setCellValueByColumnAndRow($column, $rowIdx, sprintf($col['fmt'], $row[$col['name']]));
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
$active->setCellValueByColumnAndRow($column, $rowIdx, iconv('UTF-8', 'GB2312', $row[$col['name']]));
|
||
|
}
|
||
|
|
||
|
$column++;
|
||
|
}
|
||
|
|
||
|
$rowIdx++;
|
||
|
}
|
||
|
|
||
|
foreach ($active->getColumnDimension() as $col)
|
||
|
{
|
||
|
$col->setAutoSize(true);
|
||
|
}
|
||
|
|
||
|
$active->calculateColumnWidths();
|
||
|
$active->freezePane('A2');
|
||
|
|
||
|
$name = date('YmdHis') . '.xls';
|
||
|
ob_end_clean(); // 清空缓冲区「可选」
|
||
|
header("Content-type:application/vnd.ms-excel;");
|
||
|
header("Content-Disposition:filename=".$name);
|
||
|
// $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
|
||
|
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
|
||
|
$objWriter->save("php://output");
|
||
|
// $objWriter->save('./exports/'.$name); // 保存到本地
|
||
|
|
||
|
}
|