Simple Export Excel with PHP+Mysql : Simple Export Excel With PHP+Mysql
 

Blog :: Simple Export Excel With PHP+Mysql :: Simple Export Excel with PHP+Mysql

simple.php

<?
$connection=mysql_connect("localhost","root","1234");
mysql_select_db("databasename");
$q="select * from tb_name ";
$qr=mysql_query($q);
$row_num=mysql_num_rows($qr);
$col_arr=array("Field1","Field2","Field3","Field4","Field5","Field6","Field7");
$col_num=count($col_arr);
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");;
header("Content-Disposition: attachment;filename=data.xls ");
?>
<?php echo '<?xml version="1.0" encoding="UTF-8"?>'; ?>

<?php echo'<?mso-application progid="Excel.Sheet"?>';?>

<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
 xmlns:o="urn:schemas-microsoft-com:office:office"
 xmlns:x="urn:schemas-microsoft-com:office:excel"
 xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
 xmlns:html="http://www.w3.org/TR/REC-html40">
 <Styles>
  <Style ss:ID="Default" ss:Name="Normal">
   <Alignment ss:Vertical="Bottom"/>
   <Borders/>
   <Font x:CharSet="222"/>
   <Interior/>
   <NumberFormat/>
   <Protection/>
  </Style>
 </Styles>
 <Worksheet ss:Name="TEST EXCEL">
  <Table ss:ExpandedColumnCount="<?=$col_num?>" ss:ExpandedRowCount="<?=$row_num+1?>" x:FullColumns="1"
   x:FullRows="1">
   <Row>
   <?php foreach($col_arr as $key=>$value){ ?>
    <Cell><Data ss:Type="String"><?=$value?></Data></Cell>
    <?php } ?>    
   </Row>
<?php
while($rs=mysql_fetch_array($qr)){
?>    
   <Row>
    <Cell><Data ss:Type="Number"><?=$rs['id']?></Data></Cell>
    <Cell><Data ss:Type="String"><?=$rs['field1']?></Data></Cell>
    <Cell><Data ss:Type="String"><?=$rs['field2']?></Data></Cell>
    <Cell><Data ss:Type="String"><?=$rs['field3']?></Data></Cell>
    <Cell><Data ss:Type="String"><?=$rs['field4']?></Data></Cell>    
    <Cell><Data ss:Type="String"><?=$rs['field5']?></Data></Cell>
    <Cell><Data ss:Type="String"><?=$rs['field6']?></Data></Cell>            
   </Row>
<?php  }  ?>     
  </Table>
  <WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel">
   <Selected/>
   <ProtectObjects>False</ProtectObjects>
   <ProtectScenarios>False</ProtectScenarios>
  </WorksheetOptions>
 </Worksheet>
</Workbook>



0 Comment

  • Have no comment.

Comment Blog

 
 
 
Share This Topic Login with Facebook