使用 Symfony 4 将数据导出到 excel 时出现问题
Problem with exporting data to excell with Symfony 4
我有问题。我想从我的数据库中导出我的数据手册,以便在 symfony 中表现出色。
我尝试使用 phpSpreadSheet 。我的功能有效,但仅适用于一个值(最后一个值)。我不明白为什么我无法检索我的所有 values.If 你有任何想法,它会很棒。
/**
* @Route("/create",name="createE")
*
*
*/
public function createSpreadsheet(){
$this->allExcel();
return $this->render('admin/book/sendExcelOk.html.twig');
}
public function allExcel(){
$books =$this->getDoctrine()->getRepository(Book::class)->findAll();
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet()->setTitle('Export Books');
$sheet->setCellValue('C1', 'Book')->mergeCells('C1:D1');
$columnNames = [
'Title',
'Author',
'PublicationDate',
'Format',
'Language'
];
$columnLetter = 'A';
foreach ($columnNames as $columnName) {
// Allow to access AA column if needed and more
$columnLetter++;
$sheet->setCellValue($columnLetter.'3', $columnName);
}
foreach ($books as $book) {
$columnValues = [
[$book->getTitle(),
$book->getAuthor(),
$book ->getPublicationDate(),
$book->getFormat(),
$book->getLanguage()],
];
}
$i = 4; // Beginning row for active sheet
$columnLetter = 'A';
foreach ($columnValues as $columnValue) {
foreach ($columnValue as $value) {
$columnLetter++;
$sheet->setCellValue($columnLetter.$i, $value);
}
$i++;
}
$writer = new Xlsx($spreadsheet);
$nombre =Book::$nombreExcell++;
$name ="Book ".$nombre;
$writer->save($name.'.xlsx');
return $spreadsheet;
}
您正在覆盖变量 $columnValues
,而不是将新值附加到数组。尝试先初始化数组并将值添加到数组 ($columnValues[] = …
),如下所示:
$columnValues = [];
foreach ($books as $book) {
$columnValues[] = [
$book->getTitle(),
$book->getAuthor(),
$book ->getPublicationDate(),
$book->getFormat(),
$book->getLanguage(),
];
}
我有问题。我想从我的数据库中导出我的数据手册,以便在 symfony 中表现出色。 我尝试使用 phpSpreadSheet 。我的功能有效,但仅适用于一个值(最后一个值)。我不明白为什么我无法检索我的所有 values.If 你有任何想法,它会很棒。
/** * @Route("/create",name="createE") * * */
public function createSpreadsheet(){
$this->allExcel();
return $this->render('admin/book/sendExcelOk.html.twig');
}
public function allExcel(){
$books =$this->getDoctrine()->getRepository(Book::class)->findAll();
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet()->setTitle('Export Books');
$sheet->setCellValue('C1', 'Book')->mergeCells('C1:D1');
$columnNames = [
'Title',
'Author',
'PublicationDate',
'Format',
'Language'
];
$columnLetter = 'A';
foreach ($columnNames as $columnName) {
// Allow to access AA column if needed and more
$columnLetter++;
$sheet->setCellValue($columnLetter.'3', $columnName);
}
foreach ($books as $book) {
$columnValues = [
[$book->getTitle(),
$book->getAuthor(),
$book ->getPublicationDate(),
$book->getFormat(),
$book->getLanguage()],
];
}
$i = 4; // Beginning row for active sheet
$columnLetter = 'A';
foreach ($columnValues as $columnValue) {
foreach ($columnValue as $value) {
$columnLetter++;
$sheet->setCellValue($columnLetter.$i, $value);
}
$i++;
}
$writer = new Xlsx($spreadsheet);
$nombre =Book::$nombreExcell++;
$name ="Book ".$nombre;
$writer->save($name.'.xlsx');
return $spreadsheet;
}
您正在覆盖变量 $columnValues
,而不是将新值附加到数组。尝试先初始化数组并将值添加到数组 ($columnValues[] = …
),如下所示:
$columnValues = [];
foreach ($books as $book) {
$columnValues[] = [
$book->getTitle(),
$book->getAuthor(),
$book ->getPublicationDate(),
$book->getFormat(),
$book->getLanguage(),
];
}