1

我正在尝试使用 phpexcel 来设置模板采购订单。我已将第 25 列设置为 43 列是购买项目的详细信息。这意味着它将从数据库中检索数据并将第 25 列中的所有值设置为 43。此外,这些列只能容纳 6 项。当购买项目太多并且无法放入我设置的列时,我会遇到问题。我的代码可以工作,但是太长了。有人有更好的解决方案吗?创建两个表后,我必须在两个表中一个一个地设置另一个单元格值。它使代码变得很长。感谢谁能提供帮助。

   $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);
4

1 回答 1

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)。或者,您可以为第一个工作表设置单元格,然后克隆该工作表并将克隆设置为第二个工作表。

于 2013-10-09T08:04:08.710 回答