How to export html table to excel or pdf in php

前端 未结 5 1097
甜味超标
甜味超标 2020-11-30 02:56

In my php page i have a table and if the user requires he has to export that table to excel sheet..

The code for displaying the table is:

$sql=mysql         


        
相关标签:
5条回答
  • 2020-11-30 03:06
    <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'];
    ?>
    
    0 讨论(0)
  • 2020-11-30 03:07

    Easiest way to export Excel to Html table

    $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;
    
    0 讨论(0)
  • 2020-11-30 03:24

    Use a PHP Excel for generatingExcel file. You can find a good one called PHPExcel here: https://github.com/PHPOffice/PHPExcel

    And for PDF generation use http://princexml.com/

    0 讨论(0)
  • 2020-11-30 03:30

    If all you want is a simple excel worksheet try this:

    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;
    

    The key here is the xml data. This will keep excel from complaining about the file.

    0 讨论(0)
  • 2020-11-30 03:32

    Either you can use CSV functions or PHPExcel

    or you can try like below

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

    The header for .xlsx files is Content-type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet

    0 讨论(0)
提交回复
热议问题