如何使用php更改excel单元格或列格式?
我用上面提到的代码生成了“我的报告”excel,它工作得很好,问题是打开excel后右键点击单元格->格式化单元格如何使用php更改excel单元格或列格式?,php,phpexcel,Php,Phpexcel,我用上面提到的代码生成了“我的报告”excel,它工作得很好,问题是打开excel后右键点击单元格->格式化单元格 然后它显示文本格式,但我需要显示如果我选择日期,那么格式是日期,如果时间,那么格式是时间,如果金额,那么格式是货币,我在这里使用了“$dat”来设置日期格式,但它不起作用。来自图书馆文档,可从github repo下载 <?php if($_POST) { //here i give test $result datas $result = array(array("Dat
然后它显示文本格式,但我需要显示如果我选择日期,那么格式是日期,如果时间,那么格式是时间,如果金额,那么格式是货币,我在这里使用了“$dat”来设置日期格式,但它不起作用。来自图书馆文档,可从github repo下载
<?php
if($_POST) {
//here i give test $result datas
$result = array(array("Date"=>"2016-01-01","Time"=>"01:08:06","num"=>"0123456789","amount"=>"0.12"), array("Date"=>"2016-01-02","Time"=>"02:08:06","num"=>"1234567890","amount"=>"1.12"),array("Date"=>"2016-01-03","Time"=>"03:08:06","num"=>"1234567899","amount"=>"3.12")
);
require_once "phpexcel/class.writeexcel_workbook.inc.php";
require_once "phpexcel/class.writeexcel_worksheet.inc.php";
$fname = tempnam("/tmp", "simple.xls");
$workbook = &new writeexcel_workbook($fname);
$f_volume =& $workbook->addformat();
//$f_volume->set_align('left');
$f_volume->set_num_format('@');
$gen =& $workbook->addformat();
$gen->set_align('left');
$gen->set_num_format('General');
$dat =& $workbook->addformat();
//$dat->set_align('left');
$dat->set_num_format('DD/MM/YYYY');
$inv_ref =& $workbook->addformat();
$inv_ref->set_num_format('YYYY-MM-DD HH:MM:SS');
$setup =& $workbook->addformat();
$setup->set_align('left');
$setup->set_num_format('#,###.00#');
$tax =& $workbook->addformat();
$tax->set_align('left');
$tax->set_num_format('0.###%');
$worksheet = &$workbook->addworksheet("My Report");
$worksheet->write_string(0,0,'Date');
$worksheet->write_string(0,1,'Time');
$worksheet->write_string(0,2,'Dialled');
$worksheet->write_string(0,3,'Rate');
$worksheet->set_column(0,0,10,$dat);
$worksheet->set_column(0,1,10,$f_volume);
$worksheet->set_column(0,2,20,$f_volume);
$worksheet->set_column(0,3,10,$f_volume);
$j=1;
$unitTotal = 0;
$totalTotal = 0;
if(count($result) > 0 ) {
foreach($result as $outKey=>$rr){
$worksheet->write($j,0,date("d/m/Y",strtotime($rr['Date'])));
$worksheet->write($j,1,$rr['Time']);
$worksheet->write($j,2,$rr['Dialled']);
$worksheet->write($j,3,$rr['Rate']);
}
}
$workbook->close();
//header("Content-Type: application/x-msexcel; charset=UTF-8");
header("Content-Type: application/x-msexcel; name=Calls Report.xls");
header("Content-Disposition: inline; filename=Calls Report.xls");
$fh=fopen($fname, "rb");
fpassthru($fh);
unlink($fname);
}
?>
那能达到你想要的吗?我现在无法自己测试它。来自图书馆文档,可从他们的github repo下载
<?php
if($_POST) {
//here i give test $result datas
$result = array(array("Date"=>"2016-01-01","Time"=>"01:08:06","num"=>"0123456789","amount"=>"0.12"), array("Date"=>"2016-01-02","Time"=>"02:08:06","num"=>"1234567890","amount"=>"1.12"),array("Date"=>"2016-01-03","Time"=>"03:08:06","num"=>"1234567899","amount"=>"3.12")
);
require_once "phpexcel/class.writeexcel_workbook.inc.php";
require_once "phpexcel/class.writeexcel_worksheet.inc.php";
$fname = tempnam("/tmp", "simple.xls");
$workbook = &new writeexcel_workbook($fname);
$f_volume =& $workbook->addformat();
//$f_volume->set_align('left');
$f_volume->set_num_format('@');
$gen =& $workbook->addformat();
$gen->set_align('left');
$gen->set_num_format('General');
$dat =& $workbook->addformat();
//$dat->set_align('left');
$dat->set_num_format('DD/MM/YYYY');
$inv_ref =& $workbook->addformat();
$inv_ref->set_num_format('YYYY-MM-DD HH:MM:SS');
$setup =& $workbook->addformat();
$setup->set_align('left');
$setup->set_num_format('#,###.00#');
$tax =& $workbook->addformat();
$tax->set_align('left');
$tax->set_num_format('0.###%');
$worksheet = &$workbook->addworksheet("My Report");
$worksheet->write_string(0,0,'Date');
$worksheet->write_string(0,1,'Time');
$worksheet->write_string(0,2,'Dialled');
$worksheet->write_string(0,3,'Rate');
$worksheet->set_column(0,0,10,$dat);
$worksheet->set_column(0,1,10,$f_volume);
$worksheet->set_column(0,2,20,$f_volume);
$worksheet->set_column(0,3,10,$f_volume);
$j=1;
$unitTotal = 0;
$totalTotal = 0;
if(count($result) > 0 ) {
foreach($result as $outKey=>$rr){
$worksheet->write($j,0,date("d/m/Y",strtotime($rr['Date'])));
$worksheet->write($j,1,$rr['Time']);
$worksheet->write($j,2,$rr['Dialled']);
$worksheet->write($j,3,$rr['Rate']);
}
}
$workbook->close();
//header("Content-Type: application/x-msexcel; charset=UTF-8");
header("Content-Type: application/x-msexcel; name=Calls Report.xls");
header("Content-Disposition: inline; filename=Calls Report.xls");
$fh=fopen($fname, "rb");
fpassthru($fh);
unlink($fname);
}
?>
那能达到你想要的吗?我现在无法亲自测试。不过,看起来您可能正在使用该库的其他/旧版本。你确定你正在使用的库支持你想要做的事情吗?我会检查并让你知道,查看你正在使用的库的文档通常需要几个步骤,然后才能得出结论,有些东西不能按你的预期工作。仅在excel中,日期记录必须另存为日期格式,时间作为时间格式和汇率作为货币格式链接到文档?不过,看起来您可能正在使用不同/旧版本的库。你确定你正在使用的库支持你想要做的事情吗?我会检查并让你知道,查看你正在使用的库的文档通常需要几个步骤,然后才能得出结论,有些东西不能按你的预期工作。仅在excel中,日期记录必须另存为日期格式,时间作为时间格式和汇率作为货币格式链接到文档?