【问题标题】:how to format exported excel sheet with column color in php?如何在php中用列颜色格式化导出的excel表?
【发布时间】:2012-12-14 08:57:39
【问题描述】:

我已经导出了一个 Excel 表,它将从我的 customer(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;

?>

现在我想给我的第一行上色...

【问题讨论】:

    标签: php


    【解决方案1】:

    嘿,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/

    【讨论】:

      【解决方案2】:

      CSV 文件是一个简单的文本文件,不能用颜色格式化 http://en.wikipedia.org/wiki/Comma-separated_values

      【讨论】:

      • 您使用什么库来导出 .xls?您当前的代码无法以 .xls 格式导出,请尝试使用 phpexcel.codeplex.com
      【解决方案3】:

      您好,您可以使用PHPExcel库而不是使用标题导出到excel。它提供了更简单的内置方法来实现你想要的......甚至你可以应用样式、合并单元格等

      【讨论】:

      猜你喜欢
      • 2011-10-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-30
      • 2020-09-04
      • 1970-01-01
      • 1970-01-01
      • 2020-12-18
      • 1970-01-01
      相关资源
      最近更新 更多