我必须为用户在xls文件中执行的每个订单添加(追加)一行。实际上我认为PHPexcel是最好的解决方案。 从A到K的每列都保留用于送货地址。从L到O的列保留(空白)。 在那些列之后,我必须添加一列,其中每个产品用户的ID都是订购的。如何在不知道每个专栏信的情况下这样做?有没有办法为每个项目增加它?这是我的实际代码:
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 :(得分:0)
使用简单的PHP字符串递增可能会有所帮助:将初始变量(例如$ column)设置为“A”,然后在循环中增加$ column
$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()
方法使用简单的行号和列号