Monday 14 April 2014

Export database table data to excel format

Export database table data to excel format

Select the table to be exported to Excel Format
excel.php
<table style="width:100%;left:0px;position:relative;empty-cells:show;" border=0 cellpadding=0 cellspacing=0>
<tr height=50>
    <td style="color:#CC3300;font-size:13px;font-weight:bold;">Select the table to be exported to Excel Format</td>
   
</tr>
<tr>
    <td style="color:blue;font-size:12px;">Select Table</td>
</tr>
<tr>
    <td style="color:blue;font-size:12px;">
    <select name="selected_table" >
    <option value=""> </option>
    <?php 
            $fromdb=$_SESSION['COMPANY_DB'];
            $sql = "SHOW TABLES FROM ".$fromdb;
            $result = mysql_query($sql);
            while ($row = mysql_fetch_row($result))
            {
                        $table=$row[0];
                        echo "<option value=$table>$table</option>";
            }   
    ?>
    </select>
    </td>
</tr>
</table>
<BR></br>
<input type=submit name="export" value="Export">
export.php
if($export)
{
            $my_table=$_REQUEST['selected_table'];
            $result = mysql_query('select * from '.$my_table);
            $count = mysql_num_fields($result);
           
            for ($i = 0; $i < $count; $i++){
            $header .= mysql_field_name($result, $i)."\t";
            }
           
            while($row = mysql_fetch_row($result)){
            $line = '';
            foreach($row as $value){
            if(!isset($value) || $value == ""){
            $value = "\t";
            }else{
            # important to escape any quotes to preserve them in the data.
            $value = str_replace('"', '""', $value);
            # needed to encapsulate data in quotes because some data might be multi line.
            # the good news is that numbers remain numbers in Excel even though quoted.
            $value = '"' . $value . '"' . "\t";
            }
             
           
            $line .= $value;
            }
            $data .= trim($line)."\n";
            }
            # this line is needed because returns embedded in the data have "\r"
            # and this looks like a "box character" in Excel
            $data = str_replace("\r", "", $data);
           
             
            # Nice to let someone know that the search came up empty.
            # Otherwise only the column name headers will be output to Excel.
            if ($data == "") {
            $data = "\nno matching records found\n";
            }
           
            header("Content-type: application/x-msdownload");
            # This line will stream the file to the user rather than spray it across the screen
            //header("Content-Type: application/vnd.ms-excel; name='excel'");
           
           
           
            header("Content-Disposition: attachment; filename=excelfile.xls");
            header("Pragma: no-cache");
            header("Expires: 0");
           
            echo $header."\n".$data;
           
            //print "done";
}
header("Location: excel.php");

No comments:

Post a Comment