Home > Back-end >  PHP: Create an excel table
PHP: Create an excel table

Time:01-21

I need to create an excel table with help of php. Is that possible? The php code below creates and write to an excel file. I would like to create an excel table with the data, see picture below. I'm using PhpSpreadsheet (sorry I forgot to say that)

Edit: The code below uses library PhpSpreadsheet to ceate a excel file with some content.

I want to create an excel table:

  1. The code works and create the content as the second picture below shows. This is NOT an excel table, just plain text in cells.

  2. But that is not what I want. I want to be able to create the content as the first picture below shows. This is an excel table. When you create an excel table by hand you can choose colrs etc. I do not care about the colors. Excel add the column name and push the content down.

  3. What I have tried is to add: $sheet->setAutoFilter('A1:B5'); to the code, but this does not create an excel table as shown in the third picture below.

  4. So the question is: What do I need to add to the code above to be able to create the content as shown in the first picture below

  5. The fourth picture below shows how to crate an excel table in excel (and this is what I want the php code to do)

$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->setCellValue('A1', 'A1');
$sheet->setCellValue('A2', 'A2');
$sheet->setCellValue('A3', 'A3');
$sheet->setCellValue('A4', 'A4');
$sheet->setCellValue('A5', 'A5');
//$sheet->setCellValue('A6', 'A6');

$sheet->setCellValue('B1', 'B1');
$sheet->setCellValue('B2', 'B2');
$sheet->setCellValue('B3', 'B3');
$sheet->setCellValue('B4', 'B4');
$sheet->setCellValue('B5', 'B5');
$writer = new Xlsx($spreadsheet);
$writer->save('CreateExcelTable.xlsx'); 

The picture below show the tableI would like to create

enter image description here

With the code above this is created:

enter image description here

With the code added: $sheet->setAutoFilter('A1:B5');

The picture below show what is created.It is not a table

enter image description here

enter image description here

CodePudding user response:

Insert/Table is simply a GUI "shortcut" method for styling and setting autofilters against a block of cells. Both of these can be done as individual tasks using PHPSpreadSheet, but the library does not provide a "shortcut" way of doing this with a single method call.

Take a look at section of the Developer documentation.

CodePudding user response:

I have a code I wrote, use it if you want

look at the code

using



$excelExport = new ExcelExport(['name' => 'dılo'], 'file');
$excelExport->download();


shortest excel creation example with php
basic example

<?php
$file="demo.xls";
$test="<table  ><tr><td>Cell 1</td><td>Cell 2</td></tr></table>";
header("Content-type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=$file");
echo $test;
?>

  •  Tags:  
  • Related