Php 用数组填充表
我有一个这样的数组:Php 用数组填充表,php,excel,phpexcel,Php,Excel,Phpexcel,我有一个这样的数组: $arr = [['checkTitle' => 'Проверка наличия файла robots.txt', "result" => "true", 'stateTitle' => 'Состоние', 'recommendTitle' => "Рекомендации", 'stateResult' => "Файл есть", 'recommendResult' =>
$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' => "Файл есть", "Рекомендации" => "Нету"]],];
然而,这也不起作用
如何使用此数组正确填充表格?我无法使用我认为您正在使用的PHPExcel,但我使用了: 如果您按照文档中所述的方式使用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();
恐怕您必须发布完整的类代码才能获得帮助。它看起来像PHPExcel,但我不能100%确定这是否是您想要的。@janmyszkier但我的类中没有其他内容:c only stylesWelcome@Racoon,运行代码时会发生什么?什么不起作用?有错误吗?