Nikhil
Nikhil

Reputation: 51

how to format exported excel sheet with column color in php?

i have export a excel sheet which will collect data from my customer table (Mysql database) now i want to format it with column color.

here is my code to export excel sheet....

<?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;

?>

now i want to color my first row...

Upvotes: 1

Views: 3647

Answers (3)

Bhavik Shah
Bhavik Shah

Reputation: 2291

Heay Cnik, Ayyappan Sekar is right. PHPExcel is a library that can be used to generate xls( and many other formats ) file. Its purpose is to generate a file and not sending it on some id.

What you can do is, first generate a file using PHPExcel library and save it somewhere. For sending email to some id, you can use http://php.net/manual/en/function.mail.php'>mail function. However, its little complex to send emails with attachments using php's mail function though not very difficult.

Refer the links given below:
1] http://www.shotdev.com/php/php-mail/php-send-email-upload-form-attachment-file/
2] Attach File Through PHP Mail (refer to the answer of asprin)
3] http://www.texelate.co.uk/blog/send-email-attachment-with-php/

Upvotes: 1

Ayyappan Sekar
Ayyappan Sekar

Reputation: 11475

Hi instead of using header to export to excel, U can use PHPExcel library. It provides much easier inbuilt methods to achieve what u want.. even u can apply styles, merge cells etc

Upvotes: 0

Igor Mancos
Igor Mancos

Reputation: 314

The CSV file is a simple text file, you can't format it with color http://en.wikipedia.org/wiki/Comma-separated_values

Upvotes: 0

Related Questions