V
V
venom19962021-03-05 10:51:18
Bitrix24
venom1996, 2021-03-05 10:51:18

How to export html table to excel?

$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;

I use this code, but it exports the whole page, how can I make it export what I sent to the file variable?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
T
ThunderCat, 2021-03-05
@ThunderCat

I suspect that at the end you just need to put exit();

A
Andrey Nikolaev, 2021-03-05
@gromdron

If we are talking about Bitrix24, then for a start it would not be bad not to display the contents of the buffer before calling your code.
This can be done with code:

global $APPLICATION;
$APPLICATION->RestartBuffer();

Then, in order for all this to be read correctly, it is also necessary to prevent the output after this code.
A simplified version of how you can do this:
\CMain::FinalActions();
Thus, your code can be extended to the following result:
global $APPLICATION;
$APPLICATION->RestartBuffer();

$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;

\CMain::FinalActions();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question