0

我有一个页面,显示所有具有过滤功能的数据库..过滤后我想下载到 excel 格式...我成功获取所有数据并 fwrite() 它..现在请解释我如何下载它到excel表

http://sms.weddingsplanner.co.in/all_inquiry.php这是我的页面,其中出现了 Excel 按钮。

      <?php 
                include 'functions.php';
              ob_start();




     if($_POST['types'] == 'name') {  

          $query = "SELECT * FROM visitor_detail WHERE name='".$_POST['filter']."'                               ORDER BY id     DESC ";  
  }  
                         elseif($_POST['types'] == 'mobile') {  

            $query = "SELECT * FROM visitor_detail WHERE mobile='".$_POST['filter']."' ORDER BY id   DESC ";  
       } 
              elseif($_POST['types'] == 'OccasionType') {  

    $query = "SELECT * FROM visitor_detail WHERE OccasionType='".$_POST['filter']."' ORDER BY id DESC ";  
            } 

       elseif($_POST['types'] == 'InquiryDate') {  
    $query="SELECT * FROM visitor_detail WHERE TodayDate between '".$_POST['From']."' and   '".$_POST['TO']."' ORDER BY id DESC " ;  
     }

    elseif($_REQUEST['types'] == 'OccasionDate') {  
                     $que="SELECT * FROM visitor_detail WHERE date between      '".$_REQUEST['From']."' and '".$_REQUEST['To']."' ORDER BY id DESC ";  
                    }
    else {  
                 $query = "SELECT * FROM visitor_detail ORDER BY id DESC ";  
            } 

  $sql = mysql_query($query); 


               $line1="Id\tgender\tname\tmobile\taddress\temail\totherno\tzipcode\tOccasionType\tdate\tgue      st\tdescription\tTodayDate\tauther\t";
             $data="$line1\n";
           $br = '<br>';

         $fileName = "./my.txt";
 $file = fopen($fileName, "w");
fwrite($file, $data."\n");
        $i=1;  
  while($row = mysql_fetch_assoc($sql))
{
            $line2=$i . "\t" . $row['gender'] . "\t" . $row['name'] . "\t" .                           $row['mobile'] . "\t" .    $row['address'] . "\t" . $row['email'] . "\t" . $row['otherno']  . "\t" .     $row['zipcode'] .    "\t" . $row['OccasionType'] . "\t" . $row['date'] . "\t"                              . $row['guest'] . "\t" .    $row['description'] . "\t" . $row['TodayDate'] . "\t" . $row['auther'] . "\t";
      $data1=$line2.$br;
         echo $data1;
              fwrite($file, $data1);
                $i++;
   }
                fclose($file);

  ?>
4

1 回答 1

0

要强制下载服务器上的所有 csv,您可以将其添加到您的 .htaccess 文件中:

AddType application/octet-stream pdf

编辑(您的 php 文件中的内联解决方案将更像这样):

header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename=example.pdf');
header('Pragma: no-cache');
readfile("/path/to/yourfile.pdf");
于 2013-03-14T11:52:56.823 回答