Hello, I am trying to export certain HTML elements to word to generate a report and the user to download the document. After doing some research and seeing the same question in The English forum I found with this answer:
just keep following code in top of the page need to convert:
<? header("Content-Type: application/vnd.ms-word");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("content-disposition: attachment;filename=Report.doc");
?>
I do not understand very well where I have to place this code and how do I download the page when I click on a button.
Even so, I add that I would not have any problem using a JavaScript library and doing it with it.
Edition
Another solution that I could find is this jsfiddle but when I use it in my code instead of exporting the content I export all the html as such.
I have the following table:
<table id="test" class="table table-striped table-custom table-responsive">
<thead id="tablahead">
<tr>
<th class="col-xs-1 col-sm-2 col-md-2" id="TRPANUM"><?php echo $lang["ORDEN"]?></th>
<th class="col-xs-4 col-sm-3 col-md-4" id="PARCOD"><?php echo $lang["CODIGO"]?></th>
<th class="col-xs-4 col-sm-3 col-md-4" id="PARDESE"><?php echo $lang["PARADA_CAS"]?></th>
<th class="col-xs-4 col-sm-3 col-md-4" id="CLLDESE"><?php echo $lang["CALLE_EUS"]?></th>
<th class="col-xs-4 col-sm-3 col-md-4" id="TRPANPAS"><?php echo $lang["NUMERO_PASAJEROS"]?></th>
</tr>
</thead>
<tbody id="tablabody">
<tr>
<td><?php echo $key["TRPANUM"]; ?></td>
<td><?php echo $key["PARCOD"]; ?></td>
<td><?php echo $key["PARDESE"]; ?></td>
<td><?php echo $key["CLLDESE"].", ".$key["PARCLLN"]; ?></td>
<td><?php echo $key["TRPANPAS"]; ?></td>
</tr>
<?php
}else{
?>
<tr>
<td><?php echo $key["TRPANUM"]; ?></td>
<td><?php echo $key["PARCOD"]; ?></td>
<td><?php echo $key["PARDESE"]; ?></td>
<td><?php echo $key["CLLDESE"].", ".$key["PARCLLN"]; ?></td>
<td><?php echo $key["TRPANPAS"]; ?></td>
</tr>
<?php
}
}?>
<?php
$aux = NULL;
$valor = NULL;
foreach ($resultado as $key) {
$valor = $key["TRECCOD"];
if ($valor != $aux){
$aux = $valor;
?>
</tbody>
</table>
The php that there is simply is a foreach, which starts like this:
$aux = NULL;
$valor = NULL;
foreach ($resultado as $key) {
$valor = $key["TRECCOD"];
if ($valor != $aux){
$aux = $valor;
?>
And to export I do it by pressing a button that calls the following method:
function creaWord(){
texto = window.test.innerHTML; //texto a guardar
blob = new Blob(['\ufeff', texto], {
type: 'application/msword'
});
url = URL.createObjectURL(blob);
link = document.createElement('A');
link.href = url;
link.download = "Documento"; //nombre del archivo
document.body.appendChild(link);
if (navigator.msSaveOrOpenBlob) navigator.msSaveOrOpenBlob(blob, 'Document.doc'); // IE10-11
else link.click(); // other browsers
document.body.removeChild(link);
}
The problem is that with the example exported without html tags, the following result is exported to me:
<thead id="tablahead">
<tr>
<th class="col-xs-1 col-sm-2 col-md-2" id="TRPANUM">XXX</th>
<th class="col-xs-4 col-sm-3 col-md-4" id="PARCOD">XXX</th>
<th class="col-xs-4 col-sm-3 col-md-4" id="PARDESE">XXX</th>
<th class="col-xs-4 col-sm-3 col-md-4" id="CLLDESE">XXX</th>
<th class="col-xs-4 col-sm-3 col-md-4" id="TRPANPAS">XXX</th>
</tr>
</thead>
<tbody id="tablabody">
<tr>
<td>XXX</td>
<td>XXX</td>
<td>XXX</td>
<td>XXX</td>
<td>XXX</td>
</tr>
</tbody>