我一直在互联网上搜索某些函数来使用PHP操作excel COM对象。我能够创建一个新的Excel工作表并保存数据但我无法使用样式函数来使报告看起来很好。
这是我的代码,以防它以任何方式提供帮助:
private $users;
private $brdata;
private $excel;
private $book;
private $sheet;
private $today;
private $columns;
public function __construct()
{
$this->users = $this->model('users');
$this->brdata = $this->model('brdata');
$this->excel = new COM("Excel.Application");
$this->excel->Workbooks->Add();
$this->book = $this->excel->Workbooks(1);
$this->sheet = $this->book->Worksheets(1);
$this->today = date('Y-m-d', strtotime("-1 days"));
$this->columns = array("UPC" => "UPC", "VDR ITEM #" => "CertCode", "BRAND" => "Brand", "ITEM DESCRIPTION" => "ItemDescription", "PACK" => "Pack", "SIZE" => "SizeAlpha", "CASE COST" => "CaseCost", "RETAIL" => "Retail", "ON-HAND" => "onhand", "LAST ORDER" => "lastReceiving", "LAST ORDER DATE" => "lastReceivingDate", "SALES" => "sales", "VDR #" => "VdrNo", "VDR NAME" => "VdrName");;
}
public function index()
{
$header = array("A" => "UPC", "B" => "VDR ITEM #", "C" => "BRAND", "D" => "ITEM DESCRIPTION", "E" => "PACK", "F" => "SIZE", "G" => "CASE COST", "H" => "RETAIL", "I" => "ON-HAND", "J" => "LAST ORDER", "K" => "LAST ORDER DATE", "L" => "SALES", "M" => "VDR #", "N" => "VDR NAME");
$this->setSheetName("Sheet 1");
$this->setHeader("Example Report", $header);
$this->setReport('0035', '2016-07-01', '2016-08-01', $header);
$this->saveReport('example');
}
private function setSheetName($sheetName)
{
$this->sheet->Name = $sheetName;
}
private function setHeader($title, $header)
{
$cell = $this->sheet->Range('A1');
$cell->value = $title;
foreach($header as $key => $value)
{
$cell = $this->sheet->Range($key . '2');
$cell->value = $value;
}
}
private function setReport($value, $from, $to, $header)
{
$report = $this->brdata->get_sectionReport($value, $this->today, $from, $to);
for ($i=3;$i<count($report);$i++)
{
foreach($header as $key => $value)
{
$cell = $this->sheet->Range($key . $i);
$cell->value = $report[$i][$this->columns[$value]];
if($value == "ITEM DESCRIPTION")
{
$cell->HorizontalAlignment=-4108;
}
}
}
}
private function styleReport()
{
}
private function SaveReport($documentName)
{
$this->book->SaveAs('C:\wamp\documents\\' . $documentName . '.xls');//Enregistrement du document
$this->sheet = null;//Libération de $sheet
$this->book = null;//Libération de $book
$this->excel->Workbooks->Close();//Fermeture du classeur
$this->excel->Quit();//On quitte Excel
$this->excel = null;//Libération de l'objet $excel
}
}
f我理解正确,PHP COM对象使用VBA ......功能类似吗?
感谢您的帮助! : - )