详解PhpOffice如何写一个漂亮的表格

详解PhpOffice如何写一个漂亮的表格,第1张

概述详解PhpOffice如何写一个漂亮的表格

推荐:《PHP视频教程》

文章不长,文章不长,文章不长

本表格是模仿橙心优选团长面单来做的

先上表格

思路确定总共有多少列,需要确定表头要合并多少单元格,可以多预留 1~2 列,如果用不到,最后把宽度设置为 0剩下的就是合并单元格,设置单元格样式excel 部分类结构

注意,里面列出来的有些是方法名,有些是类属性,并且只列出来了本文中使用的属性,具体还要去看看相应的类文件

Speadsheet      // 实例化 excel    Sheet           // 当前活动 sheet   PHPOffice\PHPSpreadsheet\Worksheet\Worksheet        getColumnDimension  //  *** 作列            wIDth               // 设置列宽            autoSize            // 自动大小        getRowDimension     //  *** 作行            height              // 设置行高        getCell             // 获取要 *** 作的单元格(An:Gn),如 (A2:G7)            style                同Speadsheet 下的 Style            setValue            // 设置值        mergeCell           // 合并单元格        pageSetup           // 页面设置,包含纸张大小,比如 A4            ...        pagemargins         // 页边距            ...        headerfooter        // 页眉页脚            ...        ...    Style           // 处理样式             PHPOffice\PHPSpreadsheet\Style\Style        Font                // 处理字体            size                // 字体大小            bold                // 加粗            underline           // 下划线            color               // 处理颜色                argb                // 带透明度颜色                rgb                 // 颜色        Fill                // 处理填充            fillType            // 填充方式            startcolor          // 开始颜色(不清楚用处)            endcolor            // 结束颜色(不清楚用处)            color           // 处理颜色                argb                // 带透明度颜色 背景色带透明                rgb                 // 颜色         背景色        borders        Alignment        NumberFormat        Protection
实例化

之后的例子,将使用下面的变量

$spreadsheet = new Spreadsheet();       // 实例化 excel  *** 作类,默认初始化 sheet 序号为 0$sheet = $spreadsheet->getActiveSheet(0);       // 拿到要 *** 作的 sheet,必须是已存在的// 获取 *** 作表格样式的类(全局样式)$defaultStyle = $spreadsheet->getDefaultStyle();        // PHPOffice\PHPSpreadsheet\Style\Style 实例
使用示例设置表格样式 *** 作文字对齐方式
// 获取 *** 作对齐方式 类$align = $defaultStyle->getAlignment();// 设置 Horizontal(水平) 和 Vertical(垂直) 都居中,一个类中的方法,可以连贯 *** 作$align->setHorizontal(Alignment::HORIZONTAL_CENTER)->setVertical(Alignment::VERTICAL_CENTER)// 仅水平居中$align->setHorizontal(Alignment::HORIZONTAL_CENTER);// 仅垂直居中$align->setVertical(Alignment::VERTICAL_CENTER);
*** 作边框
// 获取 *** 作对齐方式 类$border = $defaultStyle->getborders();// 设置底部边框$border->getBottom()->setborderStyle(border::border_THIN)
*** 作字体
// 获取字体 *** 作类$Font = $defaultStyle->getFont()// 设置字体 18, 加粗,加下划线$Font->setSize(18)->setBold(true)->setUnderline(Font::UNDERliNE_SINGLE);//  *** 作颜色,需要先获取颜色 *** 作 类$Font->getcolor()->setRGB('333333');
*** 作列
$column = $sheet->getColumnDimension('A')// 设置列宽$column->setWIDth(7);
完整可直接运行示例
// 引入必要类use PHPOffice\PHPSpreadsheet\Spreadsheet;use PHPOffice\PHPSpreadsheet\Writer\Xlsx;use PHPOffice\PHPSpreadsheet\Style\Alignment;use PHPOffice\PHPSpreadsheet\Style\Fill;use PHPOffice\PHPSpreadsheet\Style\border;$spreadsheet = new Spreadsheet();// 获取活动 sheet$sheet = $spreadsheet->getActiveSheet(0);// 设置表格全部上下居中$defaultStyle = $spreadsheet->getDefaultStyle();$defaultStyle->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER)->setVertical(Alignment::VERTICAL_CENTER);$defaultStyle->getFont()->getcolor()->setRGB('333333');// 设置列宽$sheet->getColumnDimension('A')->setWIDth(7);$sheet->getColumnDimension('B')->setWIDth(35);$sheet->getColumnDimension('C')->setWIDth(11);$sheet->getColumnDimension('D')->setWIDth(12);$sheet->getColumnDimension('E')->setWIDth(12);$sheet->getColumnDimension('F')->setWIDth(0);           // 预留列$sheet->getColumnDimension('G')->setWIDth(14);$line = 1;// 大标题// 合并单元格$sheet->mergeCells('A'. $line .':G'. $line);            // 合并单元格$sheet->getRowDimension($line)->setRowHeight(40);       // 设置行高$ATitle = $sheet->getCell('A' . $line);                 // 获取单元格$ATitle->getStyle('A' . $line)->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER);        // 内容水平居中$ATitle->getStyle('A' . $line)->getFont()->setSize(22)->setBold(true);                              // 字体大小,加粗$ATitle->setValue('Smallnews - 门店订单');$line ++;// 店长信息$sheet->mergeCells('A' . $line . ':G' . $line);$sheet->getStyle('A' . $line . ':G' . $line)->getborders()->getBottom()->setborderStyle(border::border_THIN);       // 下边框样式$AStore = $sheet->getCell('A' . $line);$AStore->getStyle('A' . $line)->getAlignment()->setHorizontal(Alignment::HORIZONTAL_left);                          // 内容水平居左$AStore->getStyle('A' . $line)->getFont()->setSize(16)->setBold(true);                                              // 字体大小,加粗$AStore->setValue('Smallnews/157****1560');$line ++;// 门店地址$sheet->mergeCells('A' . $line . ':G' . $line);$AAddress = $sheet->getCell('A' . $line);$AAddress->getStyle('A' . $line)->getAlignment()->setHorizontal(Alignment::HORIZONTAL_left);$AAddress->getStyle('A' . $line)->getFont()->setSize(14);$AAddress->setValue('北京望京 SOHO');$line ++;// 运单统计$sheet->mergeCells('A' . $line . ':B' . $line);         // AB 合并$sheet->getRowDimension($line)->setRowHeight(40);       // 设置行高$ATotalOrder = $sheet->getCell('A' . $line);$ATotalOrder->getStyle('A' . $line)->getAlignment()->setHorizontal(Alignment::HORIZONTAL_left)->setVertical(Alignment::VERTICAL_BottOM);        // 内容水平居左,垂直居下$ATotalOrder->getStyle('A' . $line)->getFont()->setSize(12);$ATotalOrder->setValue('订单数量:5');$sheet->mergeCells('C' . $line . ':D' . $line);         // CD 合并$CTotalGoods = $sheet->getCell('C' . $line);$CTotalGoods->getStyle('C' . $line)->getAlignment()->setHorizontal(Alignment::HORIZONTAL_left)->setVertical(Alignment::VERTICAL_BottOM);        // 内容水平居左,垂直居下$CTotalGoods->getStyle('C' . $line)->getFont()->setSize(12);$CTotalGoods->setValue('商品总量:20');$sheet->mergeCells('E' . $line . ':G' . $line);         // EFG 合并$ESend = $sheet->getCell('E' . $line);$ESend->getStyle('E' . $line)->getAlignment()->setHorizontal(Alignment::HORIZONTAL_RIGHT)->setVertical(Alignment::VERTICAL_BottOM);             // 内容水平居左,垂直居下$ESend->getStyle('E' . $line)->getFont()->setSize(12);$ESend->setValue('发货时间:' . date('Y-m-d'));$line ++;// 增加一个空行,充当上下内容的 margin$sheet->mergeCells('A' . $line . ':G' . $line);$sheet->getRowDimension($line)->setRowHeight(6);$line ++;// 模拟订单数据$orders = [    ['items' => [        ['goods_Title' => '这是个名字很长的商品,真的很长, 不信你看,肯定超过了表格宽度'],        ['goods_Title' => '这是个名字比较短的商品'],    ]],    ['items' => [        ['goods_Title' => '转向 卫衣秋季潮牌新款宽松时尚套头紫橘色橙色短款连帽卫衣女'],        ['goods_Title' => '芙清医美面膜医用男女淡化痘印抗菌敷料水光针术后修复皮炎祛痘'],        ['goods_Title' => '经典麻辣锅底'],    ]]];// 订单数据 foreach ($orders as $order) {    // 购买信息    $sheet->getRowDimension($line)->setRowHeight(30);    $sheet->getStyle('A' . $line . ':G' . $line)->getFont()->setSize(14);    $sheet->getStyle('A' . $line . ':G' . $line)->getFill()->setFillType(Fill::FILL_SolID)->getStartcolor()->setRGB('CCCCCC');    $sheet->mergeCells('A' . $line . ':B' . $line);    $AUser = $sheet->getCell('A' . $line);    $AUser->getStyle('A' . $line)->getAlignment()->setHorizontal(Alignment::HORIZONTAL_left);    $AUser->getStyle('A' . $line)->getFont()->setSize(15)->setBold(true);    // 模拟用户数据    $user = [ 'nickname' => 'Smallnews', 'mobile' => '15788881560' ];    $nickname = mb_strlen($user['nickname']) > 7 ? mb_substr($user['nickname'], 0, 6) . '**' : $user['nickname'];    $AUser->setValue($nickname . ($user['mobile'] ?  ' /  ' .substr($user['mobile'], 0, 3) . '****' . substr($user['mobile'], 7) : ''));    $sheet->mergeCells('C' . $line . ':G' . $line);    $CTotal = $sheet->getCell('C' . $line);    $CTotal->getStyle('C' . $line)->getAlignment()->setHorizontal(Alignment::HORIZONTAL_RIGHT);    $CTotal->getStyle('C' . $line)->getFont()->setSize(14);    $CTotal->setValue('共 2 种商品,共 3 件,实付 200 元');    $line++;    // 增加一个空行,充当上下内容的 margin    $sheet->mergeCells('A' . $line . ':G' . $line);    $sheet->getRowDimension($line)->setRowHeight(6);    $line ++;    // 订单商品信息    $sheet->getStyle('A' . $line . ':G' . ($line + count($order['items'])))->getborders()->getAllborders()->setborderStyle(border::border_THIN);     // 根据商品数量, 设置区域的边框    $sheet->setCellValue('A' . $line, '序号');    $sheet->setCellValue('B' . $line, '商品名称');    $sheet->setCellValue('C' . $line, '单价');    $sheet->setCellValue('D' . $line, '优惠');    $sheet->setCellValue('E' . $line, '数量');    $sheet->setCellValue('F' . $line, '');    $sheet->setCellValue('G' . $line, '是否提货');    foreach ($order['items'] as $key => $item) {        $line ++;        $sheet->setCellValue('A' . $line, ($key + 1));        $sheet->getStyle('B' . $line)->getAlignment()->setHorizontal(Alignment::HORIZONTAL_left);               // 商品名称 水平居左        $goods_Title = mb_strlen($item['goods_Title']) > 16 ? mb_substr($item['goods_Title'], 0, 14) . '**' : $item['goods_Title'];        $sheet->setCellValue('B' . $line, $goods_Title);        $sheet->setCellValue('C' . $line, '22.22');        $sheet->setCellValue('D' . $line, '11.11');        $sheet->setCellValue('E' . $line, 3);        $sheet->setCellValue('F' . $line, '');        $sheet->setCellValue('G' . $line, '');    }    $line++;    $sheet->mergeCells('A' . $line . ':G' . $line);    $sheet->getRowDimension($line)->setRowHeight(6);    $line++;}ob_end_clean();header('pragma:public');header('Content-type:application/vnd.ms-excel;charset=utf-8;name="' . '门店面单' . '.xls"');header("Content-disposition:attachment;filename=门店面单.xls"); //attachment新窗口打印inline本窗口打印$writer = \PHPOffice\PHPSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xlsx');$writer->save('PHP://output');
总结

以上是内存溢出为你收集整理的详解PhpOffice如何写一个漂亮的表格全部内容,希望文章能够帮你解决详解PhpOffice如何写一个漂亮的表格所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/998168.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-21
下一篇 2022-05-21

发表评论

登录后才能评论

评论列表(0条)

保存