我有一个这样的阵列:
$arr = [['checkTitle' => 'Проверка наличия файла robots.txt', "result" => "true",
'stateTitle' => 'Состоние', 'recommendTitle' => "Рекомендации",
'stateResult' => "Файл есть", 'recommendResult' => "Нету"],];
这样一张桌子:
我尝试使用以下代码填充表格:
foreach ( $this->createArray() as $key => $item ) {
$currentColumn = 0;
$this->sheet->setCellValueByColumnAndRow($currentColumn, $startLine, $key + 1);
$this->sheet->getStyle("A2")->applyFromArray($this->textAlignCenter());
$this->document->getActiveSheet()->mergeCells("A" . $startLine . ":" . "A" . ($startLine + 1));
$this->document->getActiveSheet()->mergeCells("B" . $startLine . ":" . "B" . ($startLine + 1));
$this->document->getActiveSheet()->mergeCells("C" . $startLine . ":" . "C" . ($startLine + 1));
foreach ( $item as $value ) {
$this->sheet->getStyle("A")->applyFromArray($this->textAlignCenter());
$currentColumn++;
$this->sheet->setCellValueByColumnAndRow($currentColumn, $startLine, $value);
}
$startLine += 3;
}
但是这不起作用,我用另一种方式重写了数组:
$arr = ['checkTitle' => 'Проверка наличия файла robots.txt', "status" => "true",
'info' => ['title' => "Файл есть", "Рекомендации" => "Нету"]],];
然而,这也不起作用。
如何使用此数组正确填充表格?
答案 0 :(得分:0)
我无法使用我认为你正在使用的PHPExcel但是我用过: https://phpspreadsheet.readthedocs.io/en/develop/#getting-started
如果你用他们的文档说的方式用composer安装那个库,然后把它放在你的index.php中:
<?php
require 'vendor/autoload.php';
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
class test
{
/* @var Xlsx\Worksheet $sheet */
public $sheet;
function createArray()
{
$arr = [
[
'checkTitle' => 'Проверка наличия файла robots.txt',
"result" => "true",
'stateTitle' => 'Состоние',
'recommendTitle' => "Рекомендации",
'stateResult' => "Файл есть",
'recommendResult' => "Нету"
]
];
return $arr;
}
function createTable()
{
$spreadsheet = new Spreadsheet();
/* @var \PhpOffice\PhpSpreadsheet\Worksheet\Worksheet $activeSheet */
$activeSheet = $spreadsheet->getActiveSheet();
$this->sheet = $activeSheet;
/*Header*/
$this->sheet->setCellValueByColumnAndRow(1, 1, 'No');
$this->sheet->setCellValueByColumnAndRow(2, 1, 'checkTitle');
$this->sheet->setCellValueByColumnAndRow(3, 1, 'stateTitle');
$this->sheet->setCellValueByColumnAndRow(4, 1, '---');
$this->sheet->setCellValueByColumnAndRow(5, 1, 'fifth header');
/*Actual Lines from your array */
$startLine = 2;
foreach ($this->createArray() as $key => $item) {
$currentColumn = 0;
$this->sheet->setCellValueByColumnAndRow($currentColumn, $startLine, $key);
$currentColumn = 1;
$this->sheet->setCellValueByColumnAndRow($currentColumn, $startLine, $key + 1);
$this->sheet->mergeCells("A" . $startLine . ":" . "A" . ($startLine + 1));
$this->sheet->mergeCells("B" . $startLine . ":" . "B" . ($startLine + 1));
$this->sheet->mergeCells("C" . $startLine . ":" . "C" . ($startLine + 1));
foreach ($item as $value) {
$currentColumn++;
var_dump('setting '. $value.' to '.$currentColumn.'.'.$startLine);
$this->sheet->setCellValueByColumnAndRow($currentColumn, $startLine, $value);
}
$startLine += 3;
}
$writer = new \PhpOffice\PhpSpreadsheet\Writer\Xls($spreadsheet);
$writer->save('hello world.xlsx');
}
}
$table = new test();
$table->createTable();
你会得到一些接近你要求的东西,但没有造型。虽然这不能100%解决您的问题,但这是您解决问题的良好起点。
OH,还有一个注意事项:请在您的代码和图片中使用英语,并非所有人都知道您的语言,因为某种原因使用英语创建编程语言,请在重新编写编程问题时使用它来提高您获取回答;)