分解函数 PHPEXCEL 文件

EXPLODE FUNCTION PHPEXCEL FILE

我在 PHPEXCEL 文件上创建了一个装箱单,我在一个 pdf 文件上创建了相同的装箱单并且效果很好。

在 pdf 文件中,这是我的代码:

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>

我的phpexcel代码:

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]);

这是我在数据库中的数据:database

这是 PDF 格式的样子:pdf_size_run

这是我尝试使用来自 pdf 的相同爆炸打开 excel 时遇到的错误:excel_error

我该如何解决这个问题?我如何打开 size_run 并在 excel 文件上展开?

谢谢

首先,您从数据库中提取一个数组并尝试将其作为对象访问($item->ppk_size_run)。您必须使用 [] 语法来访问数组值...例如 $item['ppk_size_run']

其次,重复代码太多,这就是为什么您可能看不到手头的问题。尝试在下面的代码中使用辅助函数,例如 addRowToExcel() 函数。

第三,不确定这是否是一个问题,因为我看不到以前的代码,但是在 mysql_fetch_array 方法中你有一个拼写为“$itens”的变量...应该是"$items"?

此外,我会考虑切换到 PDO 或 mysqli,因为它们更安全,并且会使您的代码更适合未来,因为 mysql 已被弃用。

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++;

}