当前位置:Gxlcms > PHP教程 > 详解PhpOffice如何写一个漂亮的表格

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

时间:2021-07-01 10:21:17 帮助过:11人阅读

推荐:《PHP视频教程》

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

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

先上表格

PhpOffice 写一个漂亮的表格

思路

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

excel 部分类结构

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

  1. Speadsheet // 实例化 excel
  2. Sheet // 当前活动 sheet PhpOffice\PhpSpreadsheet\Worksheet\Worksheet
  3. getColumnDimension // 操作列
  4. width // 设置列宽
  5. autoSize // 自动大小
  6. getRowDimension // 操作行
  7. height // 设置行高
  8. getCell // 获取要操作的单元格(An:Gn),如 (A2:G7)
  9. style
  10. 同Speadsheet 下的 Style
  11. setValue // 设置值
  12. mergeCell // 合并单元格
  13. pageSetup // 页面设置,包含纸张大小,比如 A4
  14. ...
  15. pageMargins // 页边距
  16. ...
  17. headerFooter // 页眉页脚
  18. ...
  19. ...
  20. Style // 处理样式 PhpOffice\PhpSpreadsheet\Style\Style
  21. Font // 处理字体
  22. size // 字体大小
  23. bold // 加粗
  24. underline // 下划线
  25. color // 处理颜色
  26. argb // 带透明度颜色
  27. rgb // 颜色
  28. Fill // 处理填充
  29. fillType // 填充方式
  30. startColor // 开始颜色(不清楚用处)
  31. endColor // 结束颜色(不清楚用处)
  32. color // 处理颜色
  33. argb // 带透明度颜色 背景色带透明
  34. rgb // 颜色 背景色
  35. Borders
  36. Alignment
  37. NumberFormat
  38. Protection

实例化

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

  1. $spreadsheet = new Spreadsheet(); // 实例化 excel 操作类,默认初始化 sheet 序号为 0
  2. $sheet = $spreadsheet->getActiveSheet(0); // 拿到要操作的 sheet,必须是已存在的
  3. // 获取操作表格样式的类(全局样式)
  4. $defaultStyle = $spreadsheet->getDefaultStyle(); // PhpOffice\PhpSpreadsheet\Style\Style 实例

使用示例

设置表格样式

操作文字对齐方式
  1. // 获取操作对齐方式 类
  2. $align = $defaultStyle->getAlignment();
  3. // 设置 Horizontal(水平) 和 Vertical(垂直) 都居中,一个类中的方法,可以连贯操作
  4. $align->setHorizontal(Alignment::HORIZONTAL_CENTER)->setVertical(Alignment::VERTICAL_CENTER)
  5. // 仅水平居中
  6. $align->setHorizontal(Alignment::HORIZONTAL_CENTER);
  7. // 仅垂直居中
  8. $align->setVertical(Alignment::VERTICAL_CENTER);
操作边框
  1. // 获取操作对齐方式 类
  2. $border = $defaultStyle->getBorders();
  3. // 设置底部边框
  4. $border->getBottom()->setBorderStyle(Border::BORDER_THIN)
操作字体
  1. // 获取字体操作类
  2. $font = $defaultStyle->getFont()
  3. // 设置字体 18, 加粗,加下划线
  4. $font->setSize(18)->setBold(true)->setUnderline(Font::UNDERLINE_SINGLE);
  5. // 操作颜色,需要先获取颜色操作 类
  6. $font->getColor()->setRGB('333333');
操作列
  1. $column = $sheet->getColumnDimension('A')
  2. // 设置列宽
  3. $column->setWidth(7);

完整可直接运行示例

  1. // 引入必要类
  2. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  3. use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
  4. use PhpOffice\PhpSpreadsheet\Style\Alignment;
  5. use PhpOffice\PhpSpreadsheet\Style\Fill;
  6. use PhpOffice\PhpSpreadsheet\Style\Border;
  7. $spreadsheet = new Spreadsheet();
  8. // 获取活动 sheet
  9. $sheet = $spreadsheet->getActiveSheet(0);
  10. // 设置表格全部上下居中
  11. $defaultStyle = $spreadsheet->getDefaultStyle();
  12. $defaultStyle->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER)->setVertical(Alignment::VERTICAL_CENTER);
  13. $defaultStyle->getFont()->getColor()->setRGB('333333');
  14. // 设置列宽
  15. $sheet->getColumnDimension('A')->setWidth(7);
  16. $sheet->getColumnDimension('B')->setWidth(35);
  17. $sheet->getColumnDimension('C')->setWidth(11);
  18. $sheet->getColumnDimension('D')->setWidth(12);
  19. $sheet->getColumnDimension('E')->setWidth(12);
  20. $sheet->getColumnDimension('F')->setWidth(0); // 预留列
  21. $sheet->getColumnDimension('G')->setWidth(14);
  22. $line = 1;
  23. // 大标题
  24. // 合并单元格
  25. $sheet->mergeCells('A'. $line .':G'. $line); // 合并单元格
  26. $sheet->getRowDimension($line)->setRowHeight(40); // 设置行高
  27. $ATitle = $sheet->getCell('A' . $line); // 获取单元格
  28. $ATitle->getStyle('A' . $line)->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER); // 内容水平居中
  29. $ATitle->getStyle('A' . $line)->getFont()->setSize(22)->setBold(true); // 字体大小,加粗
  30. $ATitle->setValue('Smallnews - 门店订单');
  31. $line ++;
  32. // 店长信息
  33. $sheet->mergeCells('A' . $line . ':G' . $line);
  34. $sheet->getStyle('A' . $line . ':G' . $line)->getBorders()->getBottom()->setBorderStyle(Border::BORDER_THIN); // 下边框样式
  35. $AStore = $sheet->getCell('A' . $line);
  36. $AStore->getStyle('A' . $line)->getAlignment()->setHorizontal(Alignment::HORIZONTAL_LEFT); // 内容水平居左
  37. $AStore->getStyle('A' . $line)->getFont()->setSize(16)->setBold(true); // 字体大小,加粗
  38. $AStore->setValue('Smallnews/157****1560');
  39. $line ++;
  40. // 门店地址
  41. $sheet->mergeCells('A' . $line . ':G' . $line);
  42. $AAddress = $sheet->getCell('A' . $line);
  43. $AAddress->getStyle('A' . $line)->getAlignment()->setHorizontal(Alignment::HORIZONTAL_LEFT);
  44. $AAddress->getStyle('A' . $line)->getFont()->setSize(14);
  45. $AAddress->setValue('北京望京 SOHO');
  46. $line ++;
  47. // 运单统计
  48. $sheet->mergeCells('A' . $line . ':B' . $line); // AB 合并
  49. $sheet->getRowDimension($line)->setRowHeight(40); // 设置行高
  50. $ATotalOrder = $sheet->getCell('A' . $line);
  51. $ATotalOrder->getStyle('A' . $line)->getAlignment()->setHorizontal(Alignment::HORIZONTAL_LEFT)->setVertical(Alignment::VERTICAL_BOTTOM); // 内容水平居左,垂直居下
  52. $ATotalOrder->getStyle('A' . $line)->getFont()->setSize(12);
  53. $ATotalOrder->setValue('订单数量:5');
  54. $sheet->mergeCells('C' . $line . ':D' . $line); // CD 合并
  55. $CTotalGoods = $sheet->getCell('C' . $line);
  56. $CTotalGoods->getStyle('C' . $line)->getAlignment()->setHorizontal(Alignment::HORIZONTAL_LEFT)->setVertical(Alignment::VERTICAL_BOTTOM); // 内容水平居左,垂直居下
  57. $CTotalGoods->getStyle('C' . $line)->getFont()->setSize(12);
  58. $CTotalGoods->setValue('商品总量:20');
  59. $sheet->mergeCells('E' . $line . ':G' . $line); // EFG 合并
  60. $ESend = $sheet->getCell('E' . $line);
  61. $ESend->getStyle('E' . $line)->getAlignment()->setHorizontal(Alignment::HORIZONTAL_RIGHT)->setVertical(Alignment::VERTICAL_BOTTOM); // 内容水平居左,垂直居下
  62. $ESend->getStyle('E' . $line)->getFont()->setSize(12);
  63. $ESend->setValue('发货时间:' . date('Y-m-d'));
  64. $line ++;
  65. // 增加一个空行,充当上下内容的 margin
  66. $sheet->mergeCells('A' . $line . ':G' . $line);
  67. $sheet->getRowDimension($line)->setRowHeight(6);
  68. $line ++;
  69. // 模拟订单数据
  70. $orders = [
  71. ['items' => [
  72. ['goods_title' => '这是个名字很长的商品,真的很长, 不信你看,肯定超过了表格宽度'],
  73. ['goods_title' => '这是个名字比较短的商品'],
  74. ]],
  75. ['items' => [
  76. ['goods_title' => '转向 卫衣秋季潮牌新款宽松时尚套头紫橘色橙色短款连帽卫衣女'],
  77. ['goods_title' => '芙清医美面膜医用男女淡化痘印抗菌敷料水光针术后修复皮炎祛痘'],
  78. ['goods_title' => '经典麻辣锅底'],
  79. ]]
  80. ];
  81. // 订单数据
  82. foreach ($orders as $order) {
  83. // 购买信息
  84. $sheet->getRowDimension($line)->setRowHeight(30);
  85. $sheet->getStyle('A' . $line . ':G' . $line)->getFont()->setSize(14);
  86. $sheet->getStyle('A' . $line . ':G' . $line)->getFill()->setFillType(Fill::FILL_SOLID)->getStartColor()->setRGB('CCCCCC');
  87. $sheet->mergeCells('A' . $line . ':B' . $line);
  88. $AUser = $sheet->getCell('A' . $line);
  89. $AUser->getStyle('A' . $line)->getAlignment()->setHorizontal(Alignment::HORIZONTAL_LEFT);
  90. $AUser->getStyle('A' . $line)->getFont()->setSize(15)->setBold(true);
  91. // 模拟用户数据
  92. $user = [ 'nickname' => 'Smallnews', 'mobile' => '15788881560' ];
  93. $nickname = mb_strlen($user['nickname']) > 7 ? mb_substr($user['nickname'], 0, 6) . '**' : $user['nickname'];
  94. $AUser->setValue($nickname . ($user['mobile'] ? ' / ' .substr($user['mobile'], 0, 3) . '****' . substr($user['mobile'], 7) : ''));
  95. $sheet->mergeCells('C' . $line . ':G' . $line);
  96. $CTotal = $sheet->getCell('C' . $line);
  97. $CTotal->getStyle('C' . $line)->getAlignment()->setHorizontal(Alignment::HORIZONTAL_RIGHT);
  98. $CTotal->getStyle('C' . $line)->getFont()->setSize(14);
  99. $CTotal->setValue('共 2 种商品,共 3 件,实付 200 元');
  100. $line++;
  101. // 增加一个空行,充当上下内容的 margin
  102. $sheet->mergeCells('A' . $line . ':G' . $line);
  103. $sheet->getRowDimension($line)->setRowHeight(6);
  104. $line ++;
  105. // 订单商品信息
  106. $sheet->getStyle('A' . $line . ':G' . ($line + count($order['items'])))->getBorders()->getAllBorders()->setBorderStyle(Border::BORDER_THIN); // 根据商品数量, 设置区域的边框
  107. $sheet->setCellValue('A' . $line, '序号');
  108. $sheet->setCellValue('B' . $line, '商品名称');
  109. $sheet->setCellValue('C' . $line, '单价');
  110. $sheet->setCellValue('D' . $line, '优惠');
  111. $sheet->setCellValue('E' . $line, '数量');
  112. $sheet->setCellValue('F' . $line, '');
  113. $sheet->setCellValue('G' . $line, '是否提货');
  114. foreach ($order['items'] as $key => $item) {
  115. $line ++;
  116. $sheet->setCellValue('A' . $line, ($key + 1));
  117. $sheet->getStyle('B' . $line)->getAlignment()->setHorizontal(Alignment::HORIZONTAL_LEFT); // 商品名称 水平居左
  118. $goods_title = mb_strlen($item['goods_title']) > 16 ? mb_substr($item['goods_title'], 0, 14) . '**' : $item['goods_title'];
  119. $sheet->setCellValue('B' . $line, $goods_title);
  120. $sheet->setCellValue('C' . $line, '22.22');
  121. $sheet->setCellValue('D' . $line, '11.11');
  122. $sheet->setCellValue('E' . $line, 3);
  123. $sheet->setCellValue('F' . $line, '');
  124. $sheet->setCellValue('G' . $line, '');
  125. }
  126. $line++;
  127. $sheet->mergeCells('A' . $line . ':G' . $line);
  128. $sheet->getRowDimension($line)->setRowHeight(6);
  129. $line++;
  130. }
  131. ob_end_clean();
  132. header('pragma:public');
  133. header('Content-type:application/vnd.ms-excel;charset=utf-8;name="' . '门店面单' . '.xls"');
  134. header("Content-Disposition:attachment;filename=门店面单.xls"); //attachment新窗口打印inline本窗口打印
  135. $writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xlsx');
  136. $writer->save('php://output');

以上就是详解PhpOffice如何写一个漂亮的表格的详细内容,更多请关注gxlcms其它相关文章!

人气教程排行