PHPExcel:Excel无法打开文件

时间:2013-07-22 11:39:47

标签: php phpexcel

我正在从以下代码生成excel表:

$k = 3;//Three rows made outside loop
while($i<n){
    if(<condition>){
        //the code here works fine
    }else{
        $objPHPExcel->setActiveSheetIndex(0)
                    ->setCellValue('A'.($i+$k), detailedTaskActivitiesMap($i))
                    ->setCellValue('B'.($i+$k), " ")
                    ->setCellValue('C'.($i+$k), " ")
                    ->setCellValue('D'.($i+$k), " ")
                    ->setCellValue('E'.($i+$k), " ")
                    ->setCellValue('F'.($i+$k), " ")
                    ->setCellValue('G'.($i+$k), " ")
                    ->setCellValue('H'.($i+$k), " ")
                    ->setCellValue('I'.($i+$k), " ")
                    ->setCellValue('J'.($i+$k), " ")
                    ->setCellValue('K'.($i+$k), " ")
                    ->setCellValue('L'.($i+$k), " ")
                    ->setCellValue('M'.($i+$k), " ")
                    ->setCellValue('N'.($i+$k), " ")
                    ->setCellValue('O'.($i+$k), " ")
                    ->setCellValue('P'.($i+$k), " ")
                    ->setCellValue('Q'.($i+$k), " ")
                    ->setCellValue('R'.($i+$k), " ")
                    ->setCellValue('S'.($i+$k), " ");
        $sharedStyleA1 = new PHPExcel_Style();
        $sharedStyleA1->applyFromArray(array('fill' => array(
                                    'type'  => PHPExcel_Style_Fill::FILL_SOLID,
                                    'color' => array('argb' => 'FFf2f2f2')
                                   ),
                      'borders' => array('bottom'=> array('style' => PHPExcel_Style_Border::BORDER_THIN,'color' => array('argb' => 'FFbfbfbf'))
                                   )
                      ));
        $objPHPExcel->getActiveSheet()->setSharedStyle($sharedStyleA1, "A".($i+$k));

        $sharedStyleRest1 = new PHPExcel_Style();
        $sharedStyleRest1->applyFromArray(array('fill' => array(
                                    'type'  => PHPExcel_Style_Fill::FILL_SOLID,
                                    'color' => array('argb' => 'FFffffff')
                                   ),
                      'borders' => array(
                                    'left'=> array('style' => PHPExcel_Style_Border::BORDER_THIN,'color' => array('argb' => 'FFbfbfbf')),
                                    'bottom'=> array('style' => PHPExcel_Style_Border::BORDER_THIN,'color' => array('argb' => 'FFbfbfbf')),
                                    'right'=> array('style' => PHPExcel_Style_Border::BORDER_THICK,'color' => array('argb' => 'FF000000'))
                                   )
                      ));
        $objPHPExcel->getActiveSheet()->setSharedStyle($sharedStyleRest1, "S".($i+$k));
        $objPHPExcel->getActiveSheet()->getStyle('A'.($i+$k))->getFont()->setSize(10);
        $objPHPExcel->getActiveSheet()->getStyle('B'.($i+$k))->getFont()->setSize(8);
        $objPHPExcel->getActiveSheet()->getStyle('A'.($i+$k))->getFont()->getColor()->applyFromArray( array('argb' => 'FF000000','name'=>'Calibri') );
        $objPHPExcel->getActiveSheet()->getStyle('C'.($i+$k))->getFont()->setBold(true);
        $objPHPExcel->getActiveSheet()->getStyle('C'.($i+$k))->getFont()->getColor()->applyFromArray( array('argb' => 'FFc00000','name'=>'Calibri') );
        $objPHPExcel->getActiveSheet()->getStyle('A'.($i+$k))->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
        $objPHPExcel->getActiveSheet()->getStyle('A'.($i+$k))->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);


    }
    $i++;
}

//Code to generate other rows.

$objPHPExcel->getActiveSheet()->getStyle('A1:S100')
        ->getAlignment()->setWrapText(true); 

header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="'.$filename.'.xlsx"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('php://output');

exit();

只要他们是if条件失败的行,excel表就无法在Microsoft Excel中打开。

我已经测试了$ i,$ k的值以及其他函数和变量返回的值。他们是对的。

0 个答案:

没有答案