You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
77 lines
1.7 KiB
77 lines
1.7 KiB
<?php
|
|
|
|
namespace app\service;
|
|
|
|
class ExportService
|
|
{
|
|
|
|
/**
|
|
* 导出
|
|
* @param string $fileName
|
|
* @param array $headArr
|
|
* @param array $data
|
|
* @throws \PHPExcel_Exception
|
|
* @throws \PHPExcel_Reader_Exception
|
|
* @throws \PHPExcel_Writer_Exception
|
|
*/
|
|
public static function excelExport(string $fileName = '', array $headArr = [], array $data = [])
|
|
{
|
|
|
|
$fileName .= "-" . date("YmdHi", time()) . ".xls";
|
|
|
|
$objPHPExcel = new \PHPExcel();
|
|
|
|
//$objPHPExcel->getProperties();
|
|
|
|
$key = ord("A"); // 设置表头
|
|
|
|
foreach ($headArr as $v) {
|
|
|
|
$colum = chr($key);
|
|
|
|
$objPHPExcel->setActiveSheetIndex(0)->setCellValue($colum . '1', $v);
|
|
|
|
$objPHPExcel->setActiveSheetIndex(0)->setCellValue($colum . '1', $v);
|
|
|
|
$key += 1;
|
|
|
|
}
|
|
|
|
$column = 2;
|
|
|
|
$objActSheet = $objPHPExcel->getActiveSheet();
|
|
|
|
foreach ($data as $key => $rows) { // 行写入
|
|
|
|
$span = ord("A");
|
|
|
|
foreach ($rows as $keyName => $value) { // 列写入
|
|
|
|
$objActSheet->setCellValue(chr($span) . $column, $value);
|
|
|
|
$span++;
|
|
|
|
}
|
|
|
|
$column++;
|
|
|
|
}
|
|
|
|
$fileName = iconv("utf-8", "gb2312", $fileName); // 重命名表
|
|
|
|
$objPHPExcel->setActiveSheetIndex(0); // 设置活动单指数到第一个表,所以Excel打开这是第一个表
|
|
|
|
header('Content-Type: application/vnd.ms-excel');
|
|
|
|
header("Content-Disposition: attachment;filename=$fileName");
|
|
|
|
header('Cache-Control: max-age=0');
|
|
|
|
$objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
|
|
|
|
$objWriter->save('php://output'); // 文件通过浏览器下载
|
|
|
|
exit();
|
|
}
|
|
|
|
}
|