如何将数据表从PHP导出到Excel文件

如何将数据表从PHP导出到Excel文件

问题描述:

我想将mysql数据导出到一个excel文件中,我已经完成了,但是如果我有希腊文单词,它们会显示无意义的字符.因此,我相信我必须添加一些编码标头,但未找到任何内容.

I want to export my mysql data to an excel file, I have done it but if I have Greek words they appear with nonsense characters. So I believe I have to add some encoding headers but I didn't found something.

这是我的代码:

<?php  
//export.php  
$pdo = Database::connect();
$output = '';

 $query = "SELECT * FROM clients";
 $stmt = $pdo->prepare($query);
 //Execute the statement.
 $stmt->execute();
 $clients= $stmt->fetchAll();

  $output .= '
   <table class="table" bordered="1">  
    <tr>  
        <th>Ονοματεπώνυμο</th>
        <th>Σταθερό Τηλέφωνο</th>
        <th>Email</th>
        <th>Διεύθυνση</th>  

    </tr>
  ';
  foreach($clients as $items):
   $output .= "
    <tr>
      <td>".$items['fullname']."</td>
      <td>".$items['phone1']."</td>
      <td>".$items['email']."</td>
      <td>".$items['address']."</td>

    </tr>
   ";
  endforeach;
  $output .= '</table>';
  header('Content-Type: application/xls');
  header('Default-Charset :   utf-8 ');
  header('Content-Disposition: attachment; filename=download.xls');
  echo $output;



?>

如果您能更好地向我展示,我不介意更改整个代码. 预先感谢

I don't mind to change the whole code if you got something better to show me. Thanks in advance

那不是真的.您不能只为表使用一些html代码,将.xls扩展名拍打到表上并在Excel中打开它.您需要一个库来为您处理.例如,看看这两个:

That's not really how it works. You can't just take some html code for a table, slap a .xls extension onto it and open it in Excel. You need a library to handle this for you. Have a look for example at these two:

  • Which is the best way to generate excel output in PHP?
  • https://pear.php.net/manual/en/package.fileformats.spreadsheet-excel-writer.intro.php