如何在php中使用列颜色格式化导出的Excel工作表?

时间:2012-12-14 08:57:39

标签: php

我已导出excel表格,它将从我的客户(Mysql数据库)收集数据,现在我想用列颜色格式化它。

这是我导出excel表的代码....

<?php

ob_start();
session_start();
include("include/session.php");
include("common_function.php");
//connect the database

$customer_id=$_GET['pid'];
//Enter the headings of the excel columns
$contents="Sr.,Agent Name,Contact Person,Contact Number,Password,Deal With Customer,References,Time to reach,Package Offered,Mode of Payment,Note,NEAREST CROSS STREET,DATE OF BIRTH\n"; 

//Mysql query to get records from datanbase
//You can customize the query to filter from particular date and month etc...Which will depends your database structure.

$sql = "SELECT id,agent_id,fname1,mobile_no,password,fname,rentfrom,cheque_no,package_id,monthly_monitoring,final_comment,cross_street,dob1 FROM customer WHERE `id`='$customer_id'";

$user_query = mysql_query($sql);

//While loop to fetch the records
while($row = mysql_fetch_array($user_query))
{
$contents.=$row['id'].",";
$contents.=$row['agent_id'].",";
$contents.=$row['fname1'].",";
$contents.=$row['mobile_no'].",";
$contents.=$row['password'].",";
$contents.=$row['fname'].",";
$contents.=$row['rentfrom'].",";
$contents.=$row['cheque_no'].",";
$contents.=$row['package_id'].",";
$contents.=$row['monthly_monitoring'].",";
$contents.=$row['final_comment'].",";
$contents.=$row['cross_street'].",";
$contents.=$row['dob1']."\n";
}

// remove html and php tags etc.
$contents = strip_tags($contents); 

//header to make force download the file

header("Content-Disposition: attachment; filename=Sale_report".date('d-m-Y').".csv");
print $contents;

?>

现在我想为我的第一行着色......

3 个答案:

答案 0 :(得分:1)

Heay Cnik, Ayyappan Sekar是对的。 PHPExcel是一个可用于生成xls(和许多其他格式)文件的库。它的目的是生成一个文件而不是在某个id上发送它。

您可以做的是,首先使用PHPExcel库生成一个文件并将其保存在某处。 要向某个ID发送电子邮件,您可以使用http://php.net/manual/en/function.mail.php'>mail函数。然而,使用php的邮件功能发送带附件的电子邮件虽然不是很困难,但它很复杂。

参考下面给出的链接:
1] http://www.shotdev.com/php/php-mail/php-send-email-upload-form-attachment-file/
2] Attach File Through PHP Mail(参见asprin的回答)
3] http://www.texelate.co.uk/blog/send-email-attachment-with-php/

答案 1 :(得分:0)

CSV文件是一个简单的文本文件,您无法使用颜色对其进行格式化 http://en.wikipedia.org/wiki/Comma-separated_values

答案 2 :(得分:0)

您可以使用PHPExcel库,而不是使用标头导出到Excel。它提供了更容易的内置方法来实现你想要的东西..即使你可以应用样式,合并单元格等