如何在PHP中将html表导出为ex​​cel或pdf

时间:2012-09-22 05:28:04

标签: php

在我的php页面中,我有一个表格,如果用户要求他必须将该表格导出到excel表格。

显示表格的代码是:

    $sql=mysql_query("SELECT * FROM attendance WHERE (year = '" . mysql_real_escape_string($_SESSION['year']) . "') and ( branch= '" . mysql_real_escape_string(($_SESSION['branch'])). "') and ( sem= '" . mysql_real_escape_string(($_SESSION['sem'])). "') and (sec= '" . mysql_real_escape_string(($_SESSION['sec'])). "')"); print "<body background='bg.jpg'>"; Print "<br><br><BR><center><table border cellpadding=3><tr><th>idno</th><th>name</th><th>subject</th><th>Held Classes</th><th>Attended Classes</th></tr>"; while($data=mysql_fetch_array( $sql ))   { 

    echo "<tr><td>".$data['idno']." </td><td>".$data['name'] . " <td>".$data['subject']." </td><td>".$data['heldcls'] . "<td>".$data['attendcls']." </td>"; } 
 Print "</table><br><br><form action = excel.php method = POST><input type = 'submit' name = 'submit' Value = 'Export to excel'></form></center>";

如何将此表导出到Excel工作表。 excel.php 中的代码应该是什么?请帮帮我..提前谢谢..

5 个答案:

答案 0 :(得分:23)

您可以使用CSV functionsPHPExcel

或者您可以尝试如下

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

.xlsx文件的标头为Content-type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet

答案 1 :(得分:14)

如果你想要的只是一个简单的excel工作表,试试这个:

header('Content-type: application/excel');
$filename = 'filename.xls';
header('Content-Disposition: attachment; filename='.$filename);

$data = '<html xmlns:x="urn:schemas-microsoft-com:office:excel">
<head>
    <!--[if gte mso 9]>
    <xml>
        <x:ExcelWorkbook>
            <x:ExcelWorksheets>
                <x:ExcelWorksheet>
                    <x:Name>Sheet 1</x:Name>
                    <x:WorksheetOptions>
                        <x:Print>
                            <x:ValidPrinterInfo/>
                        </x:Print>
                    </x:WorksheetOptions>
                </x:ExcelWorksheet>
            </x:ExcelWorksheets>
        </x:ExcelWorkbook>
    </xml>
    <![endif]-->
</head>

<body>
   <table><tr><td>Cell 1</td><td>Cell 2</td></tr></table>
</body></html>';

echo $data;

这里的关键是xml数据。这将使excel不再抱怨该文件。

答案 2 :(得分:6)

使用PHP Excel生成Excel文件。你可以在这里找到一个名为PHPExcel的好文章:https://github.com/PHPOffice/PHPExcel

PDF代使用http://princexml.com/

答案 3 :(得分:1)

<script src="jquery.min.js"></script>
<table border="1" id="ReportTable" class="myClass">
    <tr bgcolor="#CCC">
      <td width="100">xxxxx</td>
      <td width="700">xxxxxx</td>
      <td width="170">xxxxxx</td>
      <td width="30">xxxxxx</td>
    </tr>
    <tr bgcolor="#FFFFFF">
      <td><?php                 
            $date = date_create($row_Recordset3['fecha']);
            echo date_format($date, 'd-m-Y');
            ?></td>
      <td><?php echo $row_Recordset3['descripcion']; ?></td>
      <td><?php echo $row_Recordset3['producto']; ?></td>
      <td><img src="borrar.png" width="14" height="14" class="clickable" onClick="eliminarSeguimiento(<?php echo $row_Recordset3['idSeguimiento']; ?>)" title="borrar"></td>
    </tr>
  </table>

  <input type="hidden" id="datatodisplay" name="datatodisplay">  
    <input type="submit" value="Export to Excel"> 

exporttable.php

<?php
header('Content-Type: application/force-download');
header('Content-disposition: attachment; filename=export.xls');
// Fix for crappy IE bug in download.
header("Pragma: ");
header("Cache-Control: ");
echo $_REQUEST['datatodisplay'];
?>

答案 4 :(得分:0)

将Excel导出到HTML表的最简单方法

$file_name ="file_name.xls";
$excel_file="Your Html Table Code";
header("Content-type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=$file_name");
echo $excel_file;