Laravel Excel导出行号从总数总数开始,而不是1

发布于 2025-02-01 17:13:07 字数 1894 浏览 4 评论 0原文

我知道有一个非常简单的解决方案,需要对我的代码进行少量调整,但是我卡住了,我浪费了很多时间试图找到解决方案。

使用Laravel Excel,除了关闭行号外,我能够成功导出。

我能够推断出编号始于集合中的总数行,但应该从1开始。

任何帮助都非常感谢。

protected $table_data;
private $row = 0;

public function __construct(array $table_data)
{
    $this->table_data = $table_data;

}

public function model(array $row)
{

    ++$this->row;

}

public function columnFormats(): array
  {
      return [
          'E' => '0',
      ];
  }

public function map($table_data): array
{

  $department = (empty($table_data['department'])) ? 'Cast' : $table_data['department']['name'];

    return [
        ++$this->row,
        $department,
        $table_data['name'],
        $table_data['name_eng'],
        $table_data['phone_number'],
        $table_data['email'],
    ];
}

public function startCell(): string
{
   return 'A6';
}


public function drawings()
{
    $drawing = new Drawing();
    $drawing->setName('Logo');
    $drawing->setPath(public_path('/images/form_logo.png'));
    $drawing->setHeight(90);
    $drawing->setCoordinates('A1');

    return $drawing;
}

public function headings(): array
{
    return [
      [
        '#',
        'Department',
        'Position/Role',
        'Name',
        'Phone',
        'Email',
      ]
    ];
}

public function styles(Worksheet $sheet)
{
  $sheet->getStyle('A6:F6')->getFill()->applyFromArray(['fillType' => 'solid','rotation' => 0, 'color' => ['rgb' => '7BC1FA'],]);
  $styleArray = array(
    'font'  => array(
        'bold'  => true,
        'color' => array('rgb' => 'FFFFFF'),
        'size'  => 12,
        'name'  => 'Arial'
    ));
  $sheet->getStyle('A6:F6')->applyFromArray($styleArray)->getAlignment()->setWrapText(true)->setHorizontal('left');
}

public function array(): array
{
    return $this->table_data;
}

I know there is a very simple solution requiring minor adjustment to my code but I'm stuck and I have wasted a lot of time trying to find the solution.

Using Laravel Excel I am able to export successfully except that the row numbers are off.

I was able to deduce that the numbering begins with the total number rows within the collection, but they are supposed to begin at 1.

Any help is greatly appreciated.

protected $table_data;
private $row = 0;

public function __construct(array $table_data)
{
    $this->table_data = $table_data;

}

public function model(array $row)
{

    ++$this->row;

}

public function columnFormats(): array
  {
      return [
          'E' => '0',
      ];
  }

public function map($table_data): array
{

  $department = (empty($table_data['department'])) ? 'Cast' : $table_data['department']['name'];

    return [
        ++$this->row,
        $department,
        $table_data['name'],
        $table_data['name_eng'],
        $table_data['phone_number'],
        $table_data['email'],
    ];
}

public function startCell(): string
{
   return 'A6';
}


public function drawings()
{
    $drawing = new Drawing();
    $drawing->setName('Logo');
    $drawing->setPath(public_path('/images/form_logo.png'));
    $drawing->setHeight(90);
    $drawing->setCoordinates('A1');

    return $drawing;
}

public function headings(): array
{
    return [
      [
        '#',
        'Department',
        'Position/Role',
        'Name',
        'Phone',
        'Email',
      ]
    ];
}

public function styles(Worksheet $sheet)
{
  $sheet->getStyle('A6:F6')->getFill()->applyFromArray(['fillType' => 'solid','rotation' => 0, 'color' => ['rgb' => '7BC1FA'],]);
  $styleArray = array(
    'font'  => array(
        'bold'  => true,
        'color' => array('rgb' => 'FFFFFF'),
        'size'  => 12,
        'name'  => 'Arial'
    ));
  $sheet->getStyle('A6:F6')->applyFromArray($styleArray)->getAlignment()->setWrapText(true)->setHorizontal('left');
}

public function array(): array
{
    return $this->table_data;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

云雾 2025-02-08 17:13:07

问题可能是++ $ this-> row至少按照您预期执行的频率两倍。我不确定这是否是因为您在模型MAP方法中都有它,但是如果仅在MAP中,它可能会出错。或者您不使用导入功能,并且它在模型中。

所以我建议一个不同的解决方案:
如果您仅导出数据,并专门使用数据的方法您可以在数据集上添加行索引,并在MAP等中使用它:

public function __construct(array $table_data)
{
    $newTableData = [];
    foreach($table_data as $index => $data) {
       // add row index
       $newTableData[] = array_merge(['row' => $index], $data);
    }
    $this->table_data = $newTableData;
}

//...

public function map($table_data): array
{
    $department = (empty($table_data['department'])) ? 'Cast' : $table_data['department']['name'];

    return [
        // use row index
        $table_data['row'],
        $department,
        $table_data['name'],
        $table_data['name_eng'],
        $table_data['phone_number'],
        $table_data['email'],
    ];
}

The problem is probably ++$this->row being executed at least twice as often as you expect. I'm not sure if that's because you have it both in model and map method but it might as well go wrong if it's only in map or you are not using import features and it's in model.

So I'd suggest a different solution:
If you are only exporting Data and specifically using the array approach for your data you could add the row index on the data set and use it in map and so on:

public function __construct(array $table_data)
{
    $newTableData = [];
    foreach($table_data as $index => $data) {
       // add row index
       $newTableData[] = array_merge(['row' => $index], $data);
    }
    $this->table_data = $newTableData;
}

//...

public function map($table_data): array
{
    $department = (empty($table_data['department'])) ? 'Cast' : $table_data['department']['name'];

    return [
        // use row index
        $table_data['row'],
        $department,
        $table_data['name'],
        $table_data['name_eng'],
        $table_data['phone_number'],
        $table_data['email'],
    ];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文