convert html to excel

0

Well I'm doing the conversion this way:

$html = "
<table width='90%' border='1'>
   <tr style='background: #D7D7D7;'>
      <th>Título </th>
      <th>Título 2</th>
      <th>Título 3</th>
   </tr>
   <tr>
      <td>Dados 1</td>
      <td>Dados 2</td>
      <td>Dados 3</td>
   </tr>
   <tr>
      <td>Dados 4</td>
      <td>Dados 5</td>
      <td>Dados 6</td>
   </tr>
   <tr>
      <td>Dados 7</td>
      <td>Dados 8</td>
      <td>Dados 9</td>
   </tr>
</table>
";

// Determina que o arquivo é uma planilha do Excel
header("Content-type: application/vnd.ms-excel");

// Força o download do arquivo
header("Content-type: application/force-download");

// Seta o nome do arquivo
header("Content-Disposition: attachment; filename=file.xls");

header("Pragma: no-cache");
// Imprime o conteúdo da nossa tabela no arquivo que será gerado
echo $html;

But I'm facing 2 problems. 1 ° I need to save the file on the server instead of downloading it, 2 ° I need to make the CSS be recognized.

Does anyone know how to solve this?

    
asked by anonymous 25.09.2018 / 20:18

1 answer

0

You'll need to build a spreadsheet using the official format , which is nothing more than a ZIP file with the assets, XMLs and other files inside.

You can mount this .xlsx on hand (crazy in my humble opinion) or by using a library like the PHPSpreadSheet .

An example from the documentation:

<?php

require 'vendor/autoload.php';

use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;

$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->setCellValue('A1', 'Hello World !');

$writer = new Xlsx($spreadsheet);
$writer->save('hello world.xlsx');
    
25.09.2018 / 20:26