2014-01-13 48 views
0

我必須爲用戶在xls文件中在我的網站中執行的每個訂單添加(追加)一行。其實我想PHPexcel是最好的解決方案。 從A到K的每一列都預留作爲送貨地址。從L到O的列被保留(空白)。 在那些列之後,我必須添加一列,並且每個產品用戶的ID都是有序的。我怎麼能不知道每一列字母呢?有沒有辦法爲每個項目增加它?這是我的實際代碼:使用PHPexcel查找順序列

require_once 'Classes/PHPExcel.php'; 
require_once 'Classes/PHPExcel/IOFactory.php'; 

$objPHPExcel = PHPExcel_IOFactory::load("ordini.xlsx"); 
$objPHPExcel->setActiveSheetIndex(0); 
$row = $objPHPExcel->getActiveSheet()->getHighestRow()+1; 
//echo $row; 
$objPHPExcel->getActiveSheet()->SetCellValue('A'.$row, $orderid); 
$objPHPExcel->getActiveSheet()->SetCellValue('B'.$row, $orderid); 
$objPHPExcel->getActiveSheet()->SetCellValue('C'.$row, $array['nome']); 
$objPHPExcel->getActiveSheet()->SetCellValue('D'.$row, $array['cognome']); 
$objPHPExcel->getActiveSheet()->SetCellValue('E'.$row, $array['cellulare']); 
$objPHPExcel->getActiveSheet()->SetCellValue('F'.$row, $shippingmethod); 
$objPHPExcel->getActiveSheet()->SetCellValue('G'.$row, "IT"); 
$objPHPExcel->getActiveSheet()->SetCellValue('H'.$row, $array['provincia']); 
$objPHPExcel->getActiveSheet()->SetCellValue('I'.$row, $array['citta']); 
$objPHPExcel->getActiveSheet()->SetCellValue('J'.$row, $array['indirizzo']." ".$array['civico']); 
$objPHPExcel->getActiveSheet()->SetCellValue('K'.$row, $array['indirizzo']." ".$array['civico']); 
$objPHPExcel->getActiveSheet()->SetCellValue('L'.$row, ""); 
$objPHPExcel->getActiveSheet()->SetCellValue('M'.$row, ""); 
$objPHPExcel->getActiveSheet()->SetCellValue('N'.$row, ""); 
$objPHPExcel->getActiveSheet()->SetCellValue('O'.$row, ""); 
$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel); 
$objWriter->save('ordini.xlsx'); 
?> 
+0

參見[第獲取e來自PHPExcel行和列索引的列的名稱](http://stackoverflow.com/questions/5536964/getting-the-name-of-a-column-from-the-row-and-column-indexes -in-phpexcel)。 – DCoder

回答

0

使用簡單的PHP字符串增加可能幫助:初始變量(例如$列)設置爲「A」,那麼就增加$列在一個循環

$column = 'A'; 
for ($i = 0; $i < 256; $i++) { 
    echo $column++, PHP_EOL; 
} 

$column = 'A'; 
$lastColumn = 'IV'; 
$lastColumn++; 
while ($column !== $lastColumn) { 
    echo $column++, PHP_EOL; 
} 

或者,setCellValueByColumnAndRow()方法使用簡單的行號和列號