convert html to excel

Asked

Viewed 480 times

0

Well I’m doing the conversion that 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 to the server instead of downloading it, 2° I need to make CSS recognized.

Someone knows how to fix this?

1 answer

1

You will need to assemble a spreadsheet using the official format, which is nothing more than a ZIP file with the Assets, Xmls and other files inside.

You can ride this .xlsx at hand (a madness in my humble opinion) or using a library such as the Phpspreadsheet.

An example taken 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');
  • In your example you are creating a xlsx file with Hello World !. it is possible to upload my html to that library?

  • In fact you will need to assemble cell by cell to your spreadsheet. In the same way you have assembled your HTML... Or your HTML is already ready and you don’t have the data you need?

  • My html is already ready. That’s why I’m trying to convert it.

  • Then you would have wanted to make one Scrapping from your HTML to extract the data and then assemble the spreadsheet.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.