如何将mysql数据导出到Excel工作表

时间:2013-09-19 09:31:48

标签: php html excel

    <div style="width:2140px;">
<table width="100%" border="1" cellpadding="1" cellspacing="1">
        <tr>
              <th >Sr.No</th>
        <th >Id</th>
          <th >Fornavn</th>
            <th >Etternavn</th>
           <th >Postnr</th>
        <th >Poststed</th>
            <th >Gate</th>
            <th >Husnummer</th>
            <th >Oppgang</th>
            <th >Etasje</th>
            <th >Leilighet</th>
            <th >Telefon</th>`enter code here`
            <th >Epost</th>
            <th >Campaigns_id</th>
            <th >Ip</th>
            <th >Mc_id</th> 
            <th >Recruiter_id</th>
             <th >utm_source</th>
            </tr>
<?php 
$count=1;
foreach($data as $value)
{
//print_r($value);

?>
        <tr>
        <td style="text-align:center"><?php echo $count;?></td>
        <td style="text-align:center"><?php echo $value['id'];?></td>
<?php /*?>   <td style="text-align:center"><?php echo $value['registered'];?></td>
<?php */?><td style="text-align:center"><?php echo $value['fornavn'];?></td>
        <td style="text-align:center"><?php echo $value['etternavn'];?></td>
        <td style="text-align:center"><?php echo $value['postnr']; ?></td>       
        <td style="text-align:center"><?php echo $value['poststed'];?></td>
        <td style="text-align:center"><?php echo $value['gate'];?></td>
        <td style="text-align:center"><?php echo $value['husnummer'];?></td>
        <td style="text-align:center"><?php echo $value['oppgang'];?></td>
        <td style="text-align:center"><?php echo $value['etasje'];?></td>
        <td style="text-align:center"><?php echo $value['leilighet'];; ?></td>       
        <td style="text-align:center"><?php echo $value['telefon'];?></td>
        <td style="text-align:center"><?php echo $value['epost'];?></td>
        <td style="text-align:center"><?php echo $value['campaigns_id'];?></td>
        <td style="text-align:center"><?php echo $value['ip'];?></td>
        <td style="text-align:center"><?php echo $value['mc_id'];?></td>
        <td style="text-align:center"><?php echo $value['recruiter_id'];?></td>
        <td style="text-align:center"><?php echo $value['utm_source'];?></td>
        </tr>
    <?php 
$count++;
}
 ?>
  <td  colspan="8">  <input class="button" type="submit" value="Export to Excel" name="export_excel" id="export_excel">

     </table>
     </div>

 ****hello i want to export mydata to excelsheet can anyone help me out?? 
提前谢天谢地,我正在显示我的管理员的数据,我想在excell onclick of button上显示它 ello我想将mydata导出到excelsheet任何人都可以帮助我吗?? ** 提前谢谢我显示我的管理员的数据我想在按钮上显示它的按钮**

*

1 个答案:

答案 0 :(得分:0)

您可以轻松生成Excel文件,只需发送正确的标题:

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=file.xls");
header("Content-Transfer-Encoding: binary ");

应定义一些功能

function xlsBOF() { 
    echo pack("ssssss", 0x809, 0x8, 0x0, 0x10, 0x0, 0x0);  
    return; 
} 

function xlsEOF() { 
    echo pack("ss", 0x0A, 0x00); 
    return; 
} 

function xlsWriteNumber($Row, $Col, $Value) { 
   echo pack("sssss", 0x203, 14, $Row, $Col, 0x0); 
   echo pack("d", $Value); 
   return; 
} 

function xlsWriteLabel($Row, $Col, $Value ) { 
    $L = strlen($Value); 
    echo pack("ssssss", 0x204, 8 + $L, $Row, $Col, 0x0, $L); 
    echo $Value; 
    return; 
}

您可以通过以下方式生成数据:

xlsBOF(); 
xlsWriteLabel(7,0,"Cell 1");
xlsWriteLabel(7,1,"Cell 2");
.
.
xlsWriteLabel(7,1000,"Cell n");
xlsEOF();
exit();