18

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_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>";

how do i export this table to excel sheet. And what should b the code in excel.php. Please help me.. thank you in advance..

Freddy789
  • 506
  • 4
  • 15
Nandu
  • 231
  • 1
  • 2
  • 10

5 Answers5

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

Isaac
  • 625
  • 1
  • 12
  • 30
NullPoiиteя
  • 56,591
  • 22
  • 125
  • 143
  • 5
    Just an FYI: saving html as an xls file works fine for Excel 2003, but just be aware that the current version of Excel which most people have these days gives a warning when opening the file, and this is not ideal if the download is for a client. Changing the filename and mimetype for xlsx doesn't work at all (I tried!). Like the others, I recommend PHPExcel. – FluffyKitten Feb 21 '13 at 15:06
  • @NullPoiиteя its work great with only table that is .. but its not loading images avalable in the html page – Azad Hussain Aug 20 '18 at 06:00
19

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.

darkrat
  • 673
  • 6
  • 14
  • excel 07 still complains about this – chiliNUT Jun 26 '15 at 19:35
  • 4
    This is not working with windows 10 Microsoft excel 2016 – Lonare Aug 16 '16 at 13:31
  • 1
    I'll add that if you have trouble with getting the proper encoding (eg. UTF-8) in your export file, adding this inside the -tags will make the Excel-file use the proper heading: – hansmei Jul 31 '20 at 12:15
  • How do I put data on each Sheet created? I'm creating a foreach, which will be the name of each company, and the equipment will be inside each company. – Tiago Jul 19 '21 at 22:13
10

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/

Aaron Mason
  • 310
  • 3
  • 11
Yogesh Suthar
  • 30,424
  • 18
  • 72
  • 100
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'];
?>
Praveen Srinivasan
  • 1,562
  • 4
  • 25
  • 52
-1

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;
Love Kumar
  • 1,056
  • 9
  • 10
  • Too simplistic. There are more issues than just simple hello world string. – Mulli Feb 27 '21 at 22:06
  • You solution is exporting file containing whole page html instead of just table html which we store in here `$excel_file="Your Html Table Code";` – Rishabh Jul 17 '21 at 09:24