爆炸功能PHPEXCEL文件

时间:2017-11-08 02:17:52

标签: php excel pdf phpexcel explode

我在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文件上爆炸?

由于

1 个答案:

答案 0 :(得分:0)

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

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

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

此外,我会考虑切换到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++;

}