PHPExcel - >创建重复的工作表

时间:2014-10-20 19:23:56

标签: php phpexcel

我正在尝试创建一个解析项目的CSV文件的应用程序,并为CSV文件中的每个项目执行特定操作。我已经找到了上传/解析,但是,当我将其导出到Excel时,每个CSV条目都在工作表中重复。例如,如果CSV文件中有4个条目,则第一个条目重复4次,第二个条目重复3次,等等。我查看了几天的文档,我无法找到我出错的地方。下面是生成XLSX文件的代码。

if (in_array($type,$csv_types)){
if (move_uploaded_file($tmp_name, $path)){
    foreach ($csv as $locar){
        foreach ($locar as $locid){
            $locs[] = $locid;
            $count = count($locs);
            /*if ($i < $count - 1){
                $objPHPExcel->createSheet();
                $i++;
            }*/
            $xmls = array();
            foreach ($locs as $locids){
                $url = 'Link to XML API' . $locids;
                $xmls[] = $url;
                $objWorkSheet = $objPHPExcel->createSheet();
                $objWorkSheet->setTitle($locids);
                $objPHPExcel->setActiveSheetIndexByName($locids);


            }
            foreach ($xmls as $links){
                $locations = explode("=", $links);
                $row = 1;

                //
                $objPHPExcel->getActiveSheet()->setCellValue('A'.$row,'Location ID:' . $locations[1]);
                $row++;
                $objPHPExcel->getActiveSheet()->SetCellValue('A'.$row,'Database');
                $objPHPExcel->getActiveSheet()->SetCellValue('B'.$row,'URL');
                $row++;


                $xml = simplexml_load_file($links);

                foreach ($xml as $product){
                    foreach ($product->title as $item){
                        $dbtitle = str_replace(',',' ',$item);
                        $dblink = $product->link;

                    }
                    $objPHPExcel->setActiveSheetIndexByName($locations[1]);
                    $objPHPExcel->getActiveSheet()->SetCellValue('A'.$row,$dbtitle);
                    $objPHPExcel->getActiveSheet()->SetCellValue('B'.$row,$dblink);
                    $row++;
                }
            }
        }
    }
}

代码生成的链接仅显示在正确的工作表中(第一个为csv中的条目创建的工作表)。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

试试这个......

if (in_array($type,$csv_types)){
if (move_uploaded_file($tmp_name, $path)){
    foreach ($csv as $key => $locar){
        $locs[] = $locid[$key];
        $count = count($locs);
            /*if ($i < $count - 1){
                $objPHPExcel->createSheet();
                $i++;
            }*/
        $xmls = array();
        $url = 'Link to XML API' . $locids[$key];
        $xmls[] = $url;
        $objWorkSheet = $objPHPExcel->createSheet();
        $objWorkSheet->setTitle($locids[$key]);
        $objPHPExcel->setActiveSheetIndexByName($locids[$key]);


        $locations = explode("=", $links[$key]);
        $row = 1;

                //
        $objPHPExcel->getActiveSheet()->setCellValue('A'.$row,'Location ID:' . $locations[1]);
        $row++;
        $objPHPExcel->getActiveSheet()->SetCellValue('A'.$row,'Database');
        $objPHPExcel->getActiveSheet()->SetCellValue('B'.$row,'URL');
        $row++;


        $xml = simplexml_load_file($links[$key]);

        foreach ($xml as $product){
            foreach ($product->title as $item){
                $dbtitle = str_replace(',',' ',$item);
                $dblink = $product->link;

            }
            $objPHPExcel->setActiveSheetIndexByName($locations[1]);
            $objPHPExcel->getActiveSheet()->SetCellValue('A'.$row,$dbtitle);
            $objPHPExcel->getActiveSheet()->SetCellValue('B'.$row,$dblink);
            $row++;
        }
    }
}
}

您也可以发布$xml数组(print_r)...

$xml = simplexml_load_file($links[$key]);