0

当我尝试它时,我遇到了问题。它只是加载 php 页面,但没有给我下载 .doc

这是我从第一页获取导出到 MSWord 请求的代码 非常感谢!!

<a href="admweeklyprint.php">
<button class="btn btn-primary">Export to MsWord</button>
</a>

这是我要导出的代码

<?php
include ('database_connection.php');


date_default_timezone_set('Asia/Manila');  

$currentdate = date("y-m-d");
$currentTime = date("g:i:s a");

$sqldata ="SELECT *
from tbl_orders c, tbl_orderitems p WHERE dateordered > NOW() - INTERVAL 7 DAY  AND c.orderID=p.orderID and paymentstatus='Complete' order by c.orderID ASC";

$getdata=mysql_query($sqldata);

?>


<html>
<head>
 <link href="bootstrap/css/bootstrap.css" rel="stylesheet">
    <link id="switch_style" href="bootstrap/css/united/bootstrap.css" rel="stylesheet">
    <link href="css/main.css" rel="stylesheet">
    <link href="css/jquery.rating.css" rel="stylesheet">


    </head>

    <body>
     <center> <h1><img src="mvramPDF.PNG" alt="Logo" /> </h1> 
    <h2>Sale Report </h2>


     </center>


<center>
    <table class="table table-bordered table-striped">
          <thead>
              <tr>
                <th>Order ID</th>
                <th>Product ID</th>
                <th>Model</th>
                <th>Quantity</th>
                <th>Price</th>
                <th>Total</th>
                <th>Status</th>
                <th>Date</th>
                <th>Total Price with Tax</th>
              </tr>
            </thead>

<?php

$docname = "records_" . $currentdate . ".doc";


           $x = 1;



       $totalFinal=0;    

 $LastorderID=0;                  
    while($data=  mysql_fetch_array($getdata))
    {
             $orderID = $data["orderID"];
             $totalprice = $data["totalprice"];
             $status = $data["paymentstatus"];
             $dordered = $data["dateordered"];    
            $prodID = $data["productID"];
            $model = $data["model"];
            $price = $data["price"];
            $quantity = $data["quantity"];            



         $tax = $totalprice * .12;    
   $overall = $tax + $totalprice;


            $total=$data['price']*$quantity;



    header("Content-type: application/vnd.ms-word");
    header("Content-Disposition: attachment;Filename=$docname");
echo "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=Windows-1252\">";     
     echo'<tr>
           <tbody>
              <td>'.(($LastorderID==$orderID) ? "&nbsp;" : $orderID).'</td>  
               <td>'.$prodID.'</td>
               <td>'.$model.'</td>
               <td>'.$quantity.'</td>
               <td>&#8369;'.$price.'</td>
               <td>&#8369;'.$total.'</td>
               <td>'.(($LastorderID==$orderID) ? "&nbsp;" : $status).'</td>
               <td>'.(($LastorderID==$orderID) ? "&nbsp;" : $dordered).'</td>
              <td>'.(($LastorderID==$orderID) ? "&nbsp;" : "&#8369;  $overall").'</td>  
                </tr>          
           ';    

        if($LastorderID!=$orderID) $totalFinal+=$overall;   



 $LastorderID=$orderID;
        $x++;     

    }



echo '              
                <td>&nbsp;</td>
                <td>&nbsp;</td>
                <td>&nbsp;</td>
                <td>&nbsp;</td>
                <td>&nbsp;</td>
                <td>&nbsp;</td>
                <td>&nbsp;</td>
                <td>&nbsp;</td>
                <td><strong>Total Sales: &#8369;'.$totalFinal.'</strong></td>
              </tr>         

</tbody>
         </table>';


 ?>

 </table>
</center>
</body>
</html>

非常感谢!!

4

1 回答 1

0

首先,您必须使用转换器将其转换为 .doc,使用 tcpdf 或其他一些库导出 pdf 更容易。

其次,如果要强制下载或显示文件,则必须在回显 html 的第一行之前发送 header() <html>

http://www.php.net/manual/en/function.header.php

于 2013-09-16T18:09:54.930 回答