将所选字段导出为ex​​cel

时间:2015-08-18 15:58:54

标签: php mysqli export-to-excel

我正在尝试将所选数据导出到excel(.xls),但它会显示数据库中的所有字段。我没有得到如何只选择几列。

这是代码

表格

<form name="users" method="post" action="">
 <div class="formSep">
<select name="sales_area" class="span2">

<option value="">Select Sales Region</option>
<?php $sql = mysqli_query($con, "select * from sales_area ORDER BY name") or die (mysqli_error());
while($row = mysqli_fetch_array($sql))
{
    ?>
<option value="<?php echo $row['id'];?>" ><?php echo $row['name'];?></option>
<?php } ?>
</select>
&nbsp;&nbsp;&nbsp;


<select name="assigned_to" class="span2">

<option value="">Select Person</option>
<?php $sql = mysqli_query($con, "select * from users ORDER BY firstname") or die (mysqli_error());
while($row = mysqli_fetch_array($sql))
{
    ?>
<option value="<?php echo $row['id'];?>" ><?php echo $row['firstname'].' '.$row['lastname'];?></option>
<?php } ?>
</select>

&nbsp;&nbsp;&nbsp;

<select name="category" class="span2">

<option value="">Select Category</option>
<?php $sql = mysqli_query($con, "select * from customer_category ORDER BY category") or die (mysqli_error());
while($row = mysqli_fetch_array($sql))
{
    ?>
<option value="<?php echo $row['category'];?>" ><?php echo $row['category'];?></option>
<?php } ?>
</select>
&nbsp;&nbsp;&nbsp;

  <select name="source">

<option value="">Select Source</option>
<?php $sql = mysqli_query($con, "select * from lead_source") or die (mysqli_error());
while($row = mysqli_fetch_array($sql))
{
    ?>
<option value="<?php echo $row['source'];?>" ><?php echo $row['source'];?></option>
<?php } ?>
</select> 

&nbsp;&nbsp;&nbsp;

  <select name="status" >

<option value="">Status</option>
<?php $sql = mysqli_query($con, "select * from lead_status") or die (mysqli_error());
while($row = mysqli_fetch_array($sql))
{
    ?>
<option value="<?php echo $row['percentage'];?>" ><?php echo $row['status'];?> - <?php echo $row['percentage'];?>%</option>
<?php } ?>
</select> 


 <input type="submit" name="submit" id="save" value="VIEW" class="btn btn-info" />
 &nbsp;&nbsp;&nbsp;
 <input type="submit" name="export" id="save" value="Export To Excel" class="btn btn-info" /></div>

</form>

导出到excel

 <?php
if(isset($_POST['export']))
 {           
include("../connect.php");
include("../admin_auth.php");

$sales_area = $_POST['sales_area'];
$assigned_to = $_POST['assigned_to'];
$category = $_POST['category'];
$source = $_POST['source'];
$status = $_POST['status'];

if(isset($_POST['export']) && ($_POST['sales_area']!='') && ($_POST['assigned_to']!='') && ($_POST['category']!='') && ($_POST['source']='') && ($_POST['status']!==''))
{
$sql = "select leads.id AS LID, leads.company, leads.website, leads.email, leads.phone, leads.products, leads.source, leads.status, leads.comments, leads.category, leads.sales_area, leads.assigned_to, leads.last_modified, leads.lead_customer, sales_area.id, sales_area.name as areaname, users.id, users.firstname, users.lastname from leads inner join sales_area on leads.sales_area=sales_area.id inner join users on leads.assigned_to=users.id WHERE leads.sales_area=".$sales_area." AND leads.assigned_to = ".$assigned_to." AND leads.category = '".$category."' AND leads.source = '".$source."' AND leads.status = ".$status." AND leads.lead_customer='Lead'";
}

elseif(isset($_POST['export']) && ($_POST['sales_area']!='') && ($_POST['category']!='') && ($_POST['status']!==''))
{
$sql = "select leads.id AS LID, leads.company, leads.website, leads.email, leads.phone, leads.products, leads.source, leads.status, leads.comments, leads.category, leads.sales_area, leads.assigned_to, leads.last_modified, leads.lead_customer, sales_area.id, sales_area.name as areaname, users.id, users.firstname, users.lastname from leads inner join sales_area on leads.sales_area=sales_area.id inner join users on leads.assigned_to=users.id WHERE leads.sales_area=".$sales_area." AND leads.category = '".$category."' AND leads.status = ".$status." AND leads.lead_customer='Lead'";
}

elseif(isset($_POST['export']) && ($_POST['sales_area']!='') && ($_POST['status']!==''))
{
$sql = "select leads.id AS LID, leads.company, leads.website, leads.email, leads.phone, leads.products, leads.source, leads.status, leads.comments, leads.category, leads.sales_area, leads.assigned_to, leads.last_modified, leads.lead_customer, sales_area.id, sales_area.name as areaname, users.id, users.firstname, users.lastname from leads inner join sales_area on leads.sales_area=sales_area.id inner join users on leads.assigned_to=users.id WHERE leads.sales_area=".$sales_area." AND leads.status = ".$status." AND leads.lead_customer='Lead'";
}
elseif(isset($_POST['export']))
{
$sql = "select leads.id AS LID, leads.company, leads.website, leads.email, leads.phone, leads.products, leads.source, leads.status, leads.comments, leads.category, leads.sales_area, leads.assigned_to, leads.last_modified, leads.lead_customer, sales_area.id, sales_area.name as areaname, users.id, users.firstname, users.lastname from leads inner join sales_area on leads.sales_area=sales_area.id inner join users on leads.assigned_to=users.id WHERE leads.lead_customer='Lead'";
}
$header = '';
$data ='';

$query = mysqli_query($con, $sql) or die("Couldn't execute query:<br>" . mysqli_error($con). "<br>" . mysqli_errno($con));

while ($fieldinfo=mysqli_fetch_field($query))
{
    $header .= $fieldinfo->name."\t";

}

while($row = mysqli_fetch_row($query))

    {

        $line = '';

        foreach( $row as $value )
    {                                            
        if ( ( !isset( $value ) ) || ( $value == "" ) )
        {
            $value = "\t";
        }
        else
        {
            $value = str_replace( '"' , '""' , $value );
            $value = '"' . $value . '"' . "\t";
        }
        $line .= $value;
    }
    $data .= trim( $line ) . "\n";
}

   $data = str_replace( "\r" , "" , $data );

if ( $data == "" )
{
    $data = "\nNo Record(s) Found!\n";                        
}  


    header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=leads_export.xls");
header("Pragma: no-cache");
header("Expires: 0");

print "$header\n ".$data."";
 }
?> 

现在它显示了查询中的所有字段,但我想只显示几个字段,而且我想要更改列标题。

我想只显示

$company = $row['company'];
         $person = $row['firstname']." ".$row['lastname'];
         $source = $row['source'];
         $status = $row['status']; 
         $category = $row['category'];
         $products = $row['products'];
         $area = $row['areaname'];

如何仅显示少数几列。请建议。

0 个答案:

没有答案