Skip to content

3. 对齐

ogenes edited this page Jun 30, 2022 · 1 revision

对齐设置

属性 说明 默认值
horizontal 水平对齐 Alignment::HORIZONTAL_LEFT; 居左
vertical 垂直对齐 Alignment::VERTICAL_CENTER; 居中
wrapText 自动换行 false
textRotation 旋转 0
indent 缩进 0
shrinkToFit 内容自动缩小

1. demo

$alignment = [];
$alignment['horizontal'] = Alignment::HORIZONTAL_LEFT;
$alignment['vertical'] = Alignment::VERTICAL_CENTER;

2. 分别对单元格、 指定行、 指定列、 整个sheet 维度生效请参考 字体设置。

3. 水平具右

$alignment = [];
$alignment['horizontal'] = Alignment::HORIZONTAL_RIGHT;

$data['sheet1'] = [
  ['goodsName' => '半裙', 'price' => 1490, 'actualStock' => 2,],
  ['goodsName' => '半裙', 'price' => 1590, 'actualStock' => 1]
];
$config['sheet1'] = [
  ['bindKey' => 'goodsName', 'columnName' => '商品名称'],
  ['bindKey' => 'price', 'columnName' => '售价', 'style' => ['alignment' => $alignment]],
  ['bindKey' => 'actualStock', 'columnName' => '实际库存'],
];
$client = ExportClient::getInstance();

return $client->setFilepath(__DIR__ . '/file/' . date('Y/m/d/'))
  ->setFilename('alignmentDemo' . date('His'))
  ->setData($data)
  ->setConfig($config)
  ->export();

4. 垂直居中

$alignment['vertical'] = Alignment::VERTICAL_CENTER;

5. 文本自动换行

$alignment = [];
$alignment['wrapText'] = false;

$data['sheet1'] = [
  ['goodsName' => '半裙', 'price' => 1490, 'actualStock' => 2, 'remark' => "像裙子\r\n像连衣裙"],
  ['goodsName' => '半裙', 'price' => 1590, 'actualStock' => 1, 'remark' => "像裙子\r\n像连衣裙"]
];
$config['sheet1'] = [
  ['bindKey' => 'goodsName', 'columnName' => '商品名称'],
  ['bindKey' => 'price', 'columnName' => '售价'],
  ['bindKey' => 'actualStock', 'columnName' => '实际库存'],
  ['bindKey' => 'remark', 'columnName' => '备注', 'style' => ['alignment' => $alignment] ],
];

6. 旋转

$alignment['textRotation'] = 90;

7. 缩进

$alignment['indent'] = 20;

8. 自动缩小

$alignment['shrinkToFit'] = true;
Clone this wiki locally