henryspace

记录精彩的程序人生 开始使用

PHPExcel 相关使用

使用方式

$newExcel = new \PhpOffice\PhpSpreadsheet\Spreadsheet();  //创建一个新的excel文档
$newExcel->getDefaultStyle()->getAlignment()->setHorizontal('center')->setVertical('center');
$objSheet = $newExcel->getActiveSheet();  //获取当前操作sheet的对象
$objSheet->setTitle('数据报表');  //设置当前sheet的标题

//设置宽度
$objSheet->getColumnDimension('A')->setWidth(20);

//设置第一栏的标题
$objSheet->setCellValue('A1', '标题');
// 设置第一行的行高
$objSheet->getActiveSheet()->getDefaultRowDimension(1)->setRowHeight(18);
//单元格内换行
$objSheet->setCellValue('A2', 'Hello\nWorld');
$objSheet->getStyle('A2')->getAlignment()->setWrapText(true);
//设置字体,大小
$objSheet->getStyle("A3")->getFont()->setBold(true)->setName('Verdana')->setSize(10):
//设置字体颜色
$objSheet->getStyle("A3")->getFont()->getColor()->setRGB('6F6F6F');
//合并单元格
$objSheet->mergeCells(chr($count-1) . $cursor_start . ':'.chr($count-1) . $cursor_end);
//单元格数字按字符串填入
$objSheet->setCellValueExplicit(chr($count-1) . $cursor_start, sprintf("%.2f", $vv['score3']), 's');

//设置边框
$styleArray = [
    'borders' => [
        'allBorders' => [
            'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN,
            'color' => ['argb' => 'black'],
        ]
    ],
];

$objSheet->getStyle('A1:'.chr($count-1) . $cursor_end)->applyFromArray($styleArray);

//输出
if ($format == 'Xlsx') {
    header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
} elseif ($format == 'Xls') {
    header('Content-Type: application/vnd.ms-excel');
}

header("Content-Disposition: attachment;filename="
    . $filename . date('Y-m-d') . '.' . strtolower($format));
header('Cache-Control: max-age=0');
$objWriter = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($newExcel, $format);

$objWriter->save('php://output');

请成为永远疯狂永远浪漫永远清澈的存在。

评论
留下你的脚步
推荐阅读