Search code examples
phpexcelpdfphpexcelexplode

EXPLODE FUNCTION PHPEXCEL FILE


Im creating a packing list on a PHPEXCEL FILE, I had create the same packing list on a pdf and works well.

On pdf file this is my code:

while($linha_itens = mysql_fetch_object($select_list_itens)){

                    $linha_sizer_run = explode(",", $linha_itens->ppk_size_run);

                    print '
                    <tr style="background-color:#E6E6E6; height:30px;" onmouseover="ButtonRegSet(this,1)" onmouseout="ButtonRegSet(this)" id="tr_linha_'.$count_linha.'">
                        <td class="fontstyle" style="text-align:left; max-width:60px; width:60px;">'.$linha_itens->po.'</td>
                        <td class="fontstyle" style="text-align:left; max-width:100px; width:100px;">&nbsp;'.$linha_itens->customer_name.'</td>
                        <td class="fontstyle" style="text-align:left; max-width:100px; width:100px;">&nbsp;'.$linha_itens->style_name.'</td>
                        <td class="fontstyle" style="text-align:left; max-width:100px; width:100px;">&nbsp;'.$linha_itens->style_id.'</td>
                        <td class="fontstyle" style="text-align:left; max-width:100px; width:100px;">&nbsp;'.$linha_itens->material.'</td>
                        <td class="fontstyle" style="text-align:left; max-width:100px; width:100px;">&nbsp;'.$linha_itens->colors.'</td>
                        <td class="fontstyle" style="text-align:center; max-width:50px; width:50px;">'.$linha_itens->case_ini.'</td>
                        <td class="fontstyle" style="text-align:center; max-width:50px; width:50px;">'.$linha_itens->case_fin.'</td>
                        <td class="fontstyle" style="text-align:center; max-width:50px; width:50px;">'.$linha_itens->ppk_style_name.'</td>
                        <td class="fontstyle" style="text-align:center;" width="20" align="center" id="0_'.$count_linha.'" title="4">'.$linha_sizer_run[0].'</td>
                        <td class="fontstyle" style="text-align:center;" width="20" align="center" id="1_'.$count_linha.'" title="4½">'.$linha_sizer_run[1].'</td>
                        <td class="fontstyle" style="text-align:center;" width="20" align="center" id="2_'.$count_linha.'" title="5">'.$linha_sizer_run[2].'</td>
                        <td class="fontstyle" style="text-align:center;" width="20" align="center" id="3_'.$count_linha.'" title="5½">'.$linha_sizer_run[3].'</td>
                        <td class="fontstyle" style="text-align:center;" width="20" align="center" id="4_'.$count_linha.'" title="6">'.$linha_sizer_run[4].'</td>
                        <td class="fontstyle" style="text-align:center;" width="20" align="center" id="5_'.$count_linha.'" title="6½">'.$linha_sizer_run[5].'</td>
                        <td class="fontstyle" style="text-align:center;" width="20" align="center" id="6_'.$count_linha.'" title="7">'.$linha_sizer_run[6].'</td>
                        <td class="fontstyle" style="text-align:center;" width="20" align="center  id="7_'.$count_linha.'" title="7½">'.$linha_sizer_run[7].'</td>
                        <td class="fontstyle" style="text-align:center;" width="20" align="center" id="8_'.$count_linha.'" title="8">'.$linha_sizer_run[8].'</td>
                        <td class="fontstyle" style="text-align:center;" width="20" align="center" id="9_'.$count_linha.'" title="8½">'.$linha_sizer_run[9].'</td>
                        <td class="fontstyle" style="text-align:center;" width="20" align="center" id="10_'.$count_linha.'" title="9">'.$linha_sizer_run[10].'</td>
                        <td class="fontstyle" style="text-align:center;" width="20" align="center" id="11_'.$count_linha.'" title="9½">'.$linha_sizer_run[11].'</td>
                        <td class="fontstyle" style="text-align:center;" width="20" align="center" id="12_'.$count_linha.'" title="10">'.$linha_sizer_run[12].'</td>
                        <td class="fontstyle" style="text-align:center;" width="20" align="center" id="13_'.$count_linha.'" title="10½">'.$linha_sizer_run[13].'</td>
                        <td class="fontstyle" style="text-align:center;" width="20" align="center" id="14_'.$count_linha.'" title="11">'.$linha_sizer_run[14].'</td>
                        <td class="fontstyle" style="text-align:center;" width="20" align="center" id="15_'.$count_linha.'" title="12">'.$linha_sizer_run[15].'</td>
                        <td class="fontstyle" style="text-align:center;" width="20" align="center" id="16_'.$count_linha.'" title="13">'.$linha_sizer_run[16].'</td>
                        <td class="fontstyle" style="text-align:center;" width="20" align="center" id="17_'.$count_linha.'" title="14">'.$linha_sizer_run[17].'</td>

My phpexcel code:

while ( $item = mysql_fetch_array ($itens)) { 

            $linha_sizer_run = explode(",", $item->ppk_size_run);

             $objPHPExcel->getActiveSheet()->SetCellValue('A'.$rowCount, $item['po']); 
             $objPHPExcel->getActiveSheet()->SetCellValue('B'.$rowCount, $item['style_name']); 
             $objPHPExcel->getActiveSheet()->SetCellValue('C'.$rowCount, $item['colors']); 
             $objPHPExcel->getActiveSheet()->SetCellValue('D'.$rowCount, $item['material']); 
             $objPHPExcel->getActiveSheet()->SetCellValue('F'.$rowCount, $item['case_ini']); 
             $objPHPExcel->getActiveSheet()->SetCellValue ('G'.$rowCount, '-');
             $objPHPExcel->getActiveSheet()->SetCellValue('H'.$rowCount, $item['case_fin']); 

             $objPHPExcel->getActiveSheet()->SetCellValue('I'.$rowCount, $linha_sizer_run[0]); 
             $objPHPExcel->getActiveSheet()->SetCellValue('J'.$rowCount, $linha_sizer_run[1]); 
             $objPHPExcel->getActiveSheet()->SetCellValue('K'.$rowCount, $linha_sizer_run[2]); 
             $objPHPExcel->getActiveSheet()->SetCellValue('L'.$rowCount, $linha_sizer_run[3]); 
             $objPHPExcel->getActiveSheet()->SetCellValue('M'.$rowCount, $linha_sizer_run[4]); 
             $objPHPExcel->getActiveSheet()->SetCellValue('N'.$rowCount, $linha_sizer_run[5]); 
             $objPHPExcel->getActiveSheet()->SetCellValue('O'.$rowCount, $linha_sizer_run[6]); 
             $objPHPExcel->getActiveSheet()->SetCellValue('P'.$rowCount, $linha_sizer_run[7]); 
             $objPHPExcel->getActiveSheet()->SetCellValue('Q'.$rowCount, $linha_sizer_run[8]); 
             $objPHPExcel->getActiveSheet()->SetCellValue('R'.$rowCount, $linha_sizer_run[9]); 
             $objPHPExcel->getActiveSheet()->SetCellValue('S'.$rowCount, $linha_sizer_run[10]); 
             $objPHPExcel->getActiveSheet()->SetCellValue('T'.$rowCount, $linha_sizer_run[11]); 
             $objPHPExcel->getActiveSheet()->SetCellValue('U'.$rowCount, $linha_sizer_run[12]); 
             $objPHPExcel->getActiveSheet()->SetCellValue('V'.$rowCount, $linha_sizer_run[13]); 
             $objPHPExcel->getActiveSheet()->SetCellValue('W'.$rowCount, $linha_sizer_run[14]); 
             $objPHPExcel->getActiveSheet()->SetCellValue('X'.$rowCount, $linha_sizer_run[15]); 
             $objPHPExcel->getActiveSheet()->SetCellValue('Y'.$rowCount, $linha_sizer_run[16]); 
             $objPHPExcel->getActiveSheet()->SetCellValue('Z'.$rowCount, $linha_sizer_run[17]); 
             $objPHPExcel->getActiveSheet()->SetCellValue('AA'.$rowCount, $linha_sizer_run[18]);

THis is how is my data on DB: database

This is how looks like on PDF: pdf_size_run

This is the error i get when I tried to open the excel using the same explode from pdf: excel_error

How could I fix this? How could I open the size_run and explode on excel file?

Thanks


Solution

  • First, you are pulling an array from the database and trying to access it as an object ($item->ppk_size_run). You have to use [] syntax to access an array value... for example $item['ppk_size_run']

    Second, there is way too much repeated code, which is why you probably couldn't see the issues at hand. Try using a helper function like the addRowToExcel() function in the code below.

    Third, not sure if this is an issue or not because I can't see the previous code, but inside the mysql_fetch_array method you have a variable spelled "$itens"... should that be "$items"?

    Also I would consider switching to PDO or mysqli as they are much more secure and will make your code more future proof as mysql is being deprecated.

    require_once("phpexcel/Classes/PHPExcel.php");
    
    $phpexcel = new PHPExcel();
    
    $phpexcel->getProperties()
        ->SetLastModifiedBy("")
        ->setTitle("")
        ->setSubject("")
        ->setDescription("");
    
    function addRowToExcel($column, $count, $value) {
        $phpexcel->getActiveSheet()->setCellValue($column.$count, $value);
    }
    
    $phpexcel->setActiveSheetIndex(0);
    
    $rowcount = 0;
    
    while($item = mysql_fetch_array($items)) {
    
        $runs = explode(",", $item['ppk_size_run']);
    
        $columns = [
            'A' => $item['po'],
            'B' => $item['style_name'],
            'C' => $item['colors'],
            'D' => $item['material'],
            'E' => $item['case_ini'],
            'F' => '-',
            'G' => $item['case_fin'],
            'H' => $runs[0],
            'I' => $runs[1],
            'J' => $runs[2],
            'L' => $runs[3],
            'M' => $runs[4],
            'N' => $runs[5],
            'O' => $runs[6],
            'P' => $runs[7],
            'Q' => $runs[8],
            'R' => $runs[9],
            'S' => $runs[10],
            'T' => $runs[11],
            'U' => $runs[12],
            'V' => $runs[13],
            'W' => $runs[14],
            'X' => $runs[15],
            'Y' => $runs[16],
            'Z' => $runs[17],
            'AA' => $runs[18]
        ];
    
        foreach($columns as $k => $v) {
            addRowToExcel($k, $rowcount, $v);
        }
    
        $rowcount++;
    
    }