2013-10-09 72 views
1

我正嘗試使用phpexcel設置模板採購訂單。 我已經設置第25至43列是爲了購買項目的細節。這意味着它將從數據庫檢索數據並將第25列中的所有值設置爲43。此外,這些列僅適用於第6項。當購買物品太多而無法放入我設定的欄目時,我正面臨問題。 我的代碼是可用的,但它太長了。任何人有更好的解決方案? 創建兩張表後,我必須在兩個表單中逐一設置其他單元格值。它使代碼變得非常長。讚賞誰可以提供幫助。在phpexcel中創建新工作表並設置單元格值

$objPHPExcel->setActiveSheetIndex(0); 

    $result = mysql_query("SELECT purchasematerial.PONum,purchasematerial.QuoNum,purchasematerial.date,   material.product,purchasematerial.description,purchasematerial.unit_price,purchasematerial.UOM,purchasematerial.qua  ntity ,purchasematerial.amount FROM purchasematerial INNER JOIN material ON  purchasematerial.product=material.id where PONum='$PONum'"); 
    $col=25; 
    $num=1; 
    while($row = mysql_fetch_array($result)){ 

if($col<43){ 
$objPHPExcel->getActiveSheet()->setCellValue('AA11', $row['PONum']); 
$date = new DateTime($row['date']); 
$objPHPExcel->getActiveSheet()->setCellValue('AA12', $date->format('d-M-y')); 
$objPHPExcel->getActiveSheet()->setCellValue('I45', '=AA12+7'); 
$QuoNum=$row['QuoNum']; 
if ($QuoNum=='' || $QuoNum== null) 
$objPHPExcel->getActiveSheet()->setCellValue('I47', "N/A"); 
else 
$objPHPExcel->getActiveSheet()->setCellValue('I47', $row['QuoNum']); 
$objPHPExcel->getActiveSheet()->setCellValue('A'.$col, $num); 
$objPHPExcel->getActiveSheet()->setCellValue('B'.$col, $row['product']); 
$objPHPExcel->getActiveSheet()->SetCellValue('J'.$col, $row['description']); 
$objPHPExcel->getActiveSheet()->SetCellValue('S'.$col, $row['unit_price']); 
$objPHPExcel->getActiveSheet()->setCellValue('V'.$col, $row['UOM']); 
$objPHPExcel->getActiveSheet()->SetCellValue('Y'.$col, $row['quantity']); 
$objPHPExcel->getActiveSheet()->SetCellValue('AB'.$col, $row['amount']); 
$col=$col+3; 
$num++; 
} 

else{ 

$objPHPExcel->createSheet(); 
$objPHPExcel->setActiveSheetIndex(1); 
$col=25; 
$objPHPExcel->getActiveSheet()->setCellValue('AA11', $row['PONum']); 
$date = new DateTime($row['date']); 
$objPHPExcel->getActiveSheet()->setCellValue('AA12', $date->format('d-M-y')); 
$objPHPExcel->getActiveSheet()->setCellValue('I45', '=AA12+7'); 
$QuoNum=$row['QuoNum']; 
if ($QuoNum=='' || $QuoNum== null) 
$objPHPExcel->getActiveSheet()->setCellValue('I47', "N/A"); 
else 
$objPHPExcel->getActiveSheet()->setCellValue('I47', $row['QuoNum']); 
$objPHPExcel->getActiveSheet()->setCellValue('A'.$col, $num); 
$objPHPExcel->getActiveSheet()->setCellValue('B'.$col, $row['product']); 
$objPHPExcel->getActiveSheet()->SetCellValue('J'.$col, $row['description']); 
$objPHPExcel->getActiveSheet()->SetCellValue('S'.$col, $row['unit_price']); 
$objPHPExcel->getActiveSheet()->setCellValue('V'.$col, $row['UOM']); 
$objPHPExcel->getActiveSheet()->SetCellValue('Y'.$col, $row['quantity']); 
$objPHPExcel->getActiveSheet()->SetCellValue('AB'.$col, $row['amount']); 
$col=$col+3; 
$num++; 
} 
} 

$objPHPExcel->setActiveSheetIndex(0); 
$objPHPExcel->getActiveSheet()->getStyle('I45')->getNumberFormat()- >setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_DATE_XLSX15); 
$objPHPExcel->getActiveSheet()->setCellValue('AC1', 'WEEKNUM(I45)'); 
$objPHPExcel->getActiveSheet()->setCellValue('Q44', 'TOTAL'); 
$objPHPExcel->getActiveSheet()->SetCellValue('Y44','RM'); 
$objPHPExcel->getActiveSheet()->SetCellValue('AB44', '=SUM(AB25:AB43)'); 
$objPHPExcel->getActiveSheet()->getStyle('Q44')->getFont()->setBold(true); 
$objPHPExcel->getActiveSheet()->getStyle('Y44')->getFont()->setBold(true); 
$objPHPExcel->getActiveSheet()->getStyle('AA44')->getFont()->setBold(true); 

$objPHPExcel->setActiveSheetIndex(1); 
$objPHPExcel->getActiveSheet()->getStyle('I45')->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_DATE_XLSX15); 
$objPHPExcel->getActiveSheet()->setCellValue('AC1', 'WEEKNUM(I45)'); 
$objPHPExcel->getActiveSheet()->setCellValue('Q44', 'TOTAL'); 
$objPHPExcel->getActiveSheet()->SetCellValue('Y44','RM'); 
$objPHPExcel->getActiveSheet()->SetCellValue('AB44', '=SUM(AB25:AB43)'); 
$objPHPExcel->getActiveSheet()->getStyle('Q44')->getFont()->setBold(true); 
$objPHPExcel->getActiveSheet()->getStyle('Y44')->getFont()->setBold(true); 
$objPHPExcel->getActiveSheet()->getStyle('AA44')->getFont()->setBold(true); 

回答

0

一種方法可能是使用流暢的接口。取而代之的

$objPHPExcel->getActiveSheet()->setCellValue('I47', $row['QuoNum']); 
$objPHPExcel->getActiveSheet()->setCellValue('A'.$col, $num); 
$objPHPExcel->getActiveSheet()->setCellValue('B'.$col, $row['product']); 
$objPHPExcel->getActiveSheet()->SetCellValue('J'.$col, $row['description']); 
$objPHPExcel->getActiveSheet()->SetCellValue('S'.$col, $row['unit_price']); 
$objPHPExcel->getActiveSheet()->setCellValue('V'.$col, $row['UOM']); 
$objPHPExcel->getActiveSheet()->SetCellValue('Y'.$col, $row['quantity']); 
$objPHPExcel->getActiveSheet()->SetCellValue('AB'.$col, $row['amount']); 

$objPHPExcel->getActiveSheet()->setCellValue('I47', $row['QuoNum']) 
    ->setCellValue('A'.$col, $num) 
    ->setCellValue('B'.$col, $row['product']) 
    ->SetCellValue('J'.$col, $row['description']) 
    ->SetCellValue('S'.$col, $row['unit_price']) 
    ->setCellValue('V'.$col, $row['UOM']) 
    ->SetCellValue('Y'.$col, $row['quantity']) 
    ->SetCellValue('AB'.$col, $row['amount']); 

,這樣你不會經常打電話$objPHPExcel->getActiveSheet()

對於設置第二片,因爲細胞是等同於第一;你可以創建一個函數來設置單元格的值,然後調用該函數兩次(基本PHP 101)。或者,您可以設置第一個工作表的單元格,然後克隆該工作表並將該克隆設置爲第二個工作表。

+0

thx for yr suggestion。 – jannet

+0

我發現了另一個使用$ objPHPExcel-> getActiveSheet() - > getPageSetup() - > setRowsToRepeatAtTopByStartAndEnd(1,23)的解決方案。 – jannet

+0

你意識到要重複的行根本不會做同樣的事情;但相關的打印選項 –

相關問題