PHPExcel设置带变量的单元格

时间:2013-11-22 21:27:02

标签: php phpexcel

我有这个测试脚本,就像我的生产脚本一样,除了从SQL数据库中提取生产脚本。我想使用基于数组计数的变量来设置单元格位置。在下面的脚本中,(您可以复制并运行作为示例)我想根据阵列中相同的汽车数量来设置单元格编号。举个例子,阵列中列出了7辆宝马。所以我想要注释:数据使用代码$ num = $ no + 5复制到单元格A12; $细胞= 'A' $ NUM。

我遇到的问题是它确实将它放在那条线上,但它也将它放置了7次以上。如果有4种类型的汽车,它会将它放在右侧,但也将它放在它上面3次。我只是想把它放在所需的位置一次。任何帮助都会很棒。这是代码:

<?PHP

require_once 'Classes/PHPExcel.php';
    include 'Classes/PHPExcel/Writer/Excel2007.php';

$dataArray= array();
$cars=array("Versa","Volt","Volt","Volt","Volt","Volkswagen","Bentley","Benz","BMW","BMW","BMW","BMW","BMW","BMW","BMW","Cobra","Cord","Daewoo","Datsun","Dodge","Dodge","Dixi");




$objPHPExcel = new PHPExcel();
$objPHPExcel->setActiveSheetIndex(0);

while (list($var, $val) = each($cars)) {

if ($val!=$id){

$dataArray = array();

    $objWorksheet = new PHPExcel_Worksheet($objPHPExcel);
    $objPHPExcel->addSheet($objWorksheet);
    $objWorksheet->setTitle(''. $val);

        $row_array[$val] = $val;

        array_push($dataArray,$row_array);
        $no = count($dataArray);
        $num=$no+5;
    $cell='A'.$num;

$objWorksheet->setCellValue('A1' , $no);
$objWorksheet->setCellValue('A2' , $val);
$objWorksheet->setCellValue($cell , 'Comments:');
$id = $val; 
    }
    else {



    $row_array[$val] = $val;
        $count=count($cars);
        $count2=count($loc);
        array_push($dataArray,$row_array);
    $no = count($dataArray);
    $num=$no+5;
    $cell='A'.$num;

$objWorksheet->setCellValue('A2' , $no);
$objWorksheet->setCellValue('A3' , $val);

$objWorksheet->setCellValue($cell , 'Comments:');

    $id = $val; 

    }
    }

    // Save Excel 2007 file
#echo date('H:i:s') . " Write to Excel2007 format\n";
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
ob_end_clean();
// We'll be outputting an excel file
header('Content-type: application/vnd.ms-excel');
// It will be called file.xls
header('Content-Disposition: attachment; filename="cars.xlsx"');
$objWriter->save('php://output');
Exit;

?>

1 个答案:

答案 0 :(得分:0)

这解决了这个问题。

<?PHP
require_once 'Excel/PHPExcel.php';//path for my config, rewrite for yours
//include 'Classes/PHPExcel/Writer/Excel2007.php'; // not needed, lazy loader job

$cars=array("Versa","Volt","Volt","Volt","Volt","Volkswagen","Bentley","Benz","BMW","BMW","BMW","BMW","BMW","BMW","BMW","Cobra","Cord","Daewoo","Datsun","Dodge","Dodge","Dixi");

$objPHPExcel = new PHPExcel();
$objWorksheet=$objPHPExcel->setActiveSheetIndex(0);
$id='';
$countRows=0;
while (list($var, $val) = each($cars)) {
    if ($val!=$id && $id!=''){
        $objWorksheet->setTitle($id);
        $num=$countRows+5;
        $cell='A'.$num;

        $objWorksheet->setCellValue($cell , 'Comments:');

        $objWorksheet = new PHPExcel_Worksheet($objPHPExcel);
        $objPHPExcel->addSheet($objWorksheet);

        $id = $val;
        $countRows=0;
    }//end if
    if($id=='') $id=$val;//the first car
    $no = ++$countRows;
    $objWorksheet->setCellValue('A'.$no , $no);
    $objWorksheet->setCellValue('B'.$no , $id);//Versa, Volt, ...
}
if($countRows>0 && $id!=''){// the last car - if $id=='' the workbook is empty
    $objWorksheet->setTitle($id);
    $num=$countRows+5;
    $cell='A'.$num;

    $objWorksheet->setCellValue($cell , 'Comments:');
}//end if

// Save Excel 2007 file
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
ob_end_clean();
// We'll be outputting an excel file
header('Content-type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); // note : use correct mime type (not xls for xlsx)
// It will be called cars.xlsx
header('Content-Disposition: attachment; filename="cars.xlsx"');
$objWriter->save('php://output');
Exit;

?>