Skip to content

1. 字体

ogenes edited this page Jun 30, 2022 · 1 revision

字体设置

注意: Exceler 支持对单元格、 指定行、 指定列、 整个sheet 维度的字体设置。 设置的优先级规则为: cell > row > column > sheet 。 与代码先后顺序无关。

属性 备注 默认
bold 加粗 false
color 颜色 ['argb' => '00595959']
italic 斜体 false
name 字体名称 微软雅黑
size 字号 8
strikethrough 删除线 false
subscript 下标,通常用不到 false
superscript 上标,通常用不到 false
underline 下划线 false

1. demo

$font = [];
$font['name'] = '宋体';
$font['size'] = 14;
$font['bold'] = true;

2. 对整个sheet生效

 $data['sheet1'] = [
    ['goodsName' => '半裙', 'price' => 1490, 'actualStock' => 2,],
    ['goodsName' => '半裙', 'price' => 1590, 'actualStock' => 1,]
  ];
  
  $config['sheet1'] = [
    ['bindKey' => 'goodsName', 'columnName' => '商品名称'],
    ['bindKey' => 'price', 'columnName' => '售价'],
    ['bindKey' => 'actualStock', 'columnName' => '实际库存'],
  ];
  $client = ExportClient::getInstance();
  
  $font = [];
  $font['name'] = '宋体';
  $font['size'] = 14;
  $font['bold'] = true;
  
  // 对表头生效
  $client->setStyleHeaderFont($font);
  
  $font['bold'] = false;
  // 对内容生效
  $client->setStyleFont($font);
  
  return $client->setFilepath(__DIR__ . '/file/' . date('Y/m/d/'))
    ->setFilename('fontDemo')
    ->setData($data)
    ->setConfig($config)
    ->export();

3. 对整列数据生效

$data['sheet1'] = [
    ['goodsName' => '半裙', 'price' => 1490, 'actualStock' => 2,],
    ['goodsName' => '半裙', 'price' => 1590, 'actualStock' => 1,]
  ];
  
  $font = [];
  $font['name'] = '宋体';
  $font['size'] = 14;
  $style['font'] = $font;
  //仅对 实际库存 列生效
  $config['sheet1'] = [
    ['bindKey' => 'goodsName', 'columnName' => '商品名称'],
    ['bindKey' => 'price', 'columnName' => '售价'],
    ['bindKey' => 'actualStock', 'columnName' => '实际库存', 'style' => $style],
  ];
  $client = ExportClient::getInstance();
  
  return $client->setFilepath(__DIR__ . '/file/' . date('Y/m/d/'))
    ->setFilename('fontDemo' . date('His'))
    ->setData($data)
    ->setConfig($config)
    ->export();

4. 对指定行或指定单元格生效

$font = [];
  $font['name'] = '宋体';
  $font['size'] = 14;
  //	仅对指定行的实际库存列生效
  $style['actualStock']['font'] = $font;
  //对指定行所有字段生效
  //	$style['*']['font'] = $font;
  $data['sheet1'] = [
    ['goodsName' => '半裙', 'price' => 1490, 'actualStock' => 2,],
    ['goodsName' => '半裙', 'price' => 1590, 'actualStock' => 1, 'cellStyle' => $style ]
  ];
  
  $config['sheet1'] = [
    ['bindKey' => 'goodsName', 'columnName' => '商品名称'],
    ['bindKey' => 'price', 'columnName' => '售价'],
    ['bindKey' => 'actualStock', 'columnName' => '实际库存'],
  ];
  $client = ExportClient::getInstance();
  
  return $client->setFilepath(__DIR__ . '/file/' . date('Y/m/d/'))
    ->setFilename('fontDemo' . date('His'))
    ->setData($data)
    ->setConfig($config)
    ->export();

Clone this wiki locally