如何在phpexcel中设置普通表标题

时间:2015-06-22 03:52:11

标签: php codeigniter phpexcel

我正在尝试打印里面有多张纸的Excel工作表。除了表格标题外,一切正常。我已经设置了工作表标题,因此它正在进行中。但是,对于给定的表格标题,额外的数字也会出现,这会造成混乱。我相信,这是表索引号或其他东西,但无法删除它。这是我的代码:

$y=0;
for ($w=1; $w<3; $w++) :
            $sheetId =$y;
            $objPHPExcel->createSheet(NULL, $sheetId);
            $objPHPExcel->setActiveSheetIndex($sheetId);
            $objPHPExcel->getActiveSheet()->setTitle("Week ".$w);
            $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(12);
            $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(12);
            $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(14);
            $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(20);
            $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(16);
            $objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(12);
            $objPHPExcel->getActiveSheet()->getColumnDimension('G')->setWidth(10);
            $objPHPExcel->getActiveSheet()->getColumnDimension('H')->setWidth(15);
            $objPHPExcel->getActiveSheet()->getColumnDimension('I')->setWidth(10);
            $objPHPExcel->getActiveSheet()->getColumnDimension('J')->setWidth(17.5);

            $objPHPExcel->setActiveSheetIndex($sheetId)->mergeCells('A1:J6');
            $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setWrapText(true);
            $objPHPExcel->getActiveSheet()->getStyle("A1")->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->getActiveSheet()->getStyle('A1:J5')->applyFromArray($styleArray);
            $objPHPExcel->getActiveSheet()->getStyle('A:E')->applyFromArray($centreAlign);
endfor;

输出:

第一周1和第2周是正确的,但在其他情况下,额外的数字即将到来,这是不可取的。

任何帮助都将受到高度赞赏。

3 个答案:

答案 0 :(得分:8)

好像你正在执行两次发布的代码。这将创建两组同名的工作表(第1周,第2周,第1周,第2周),Excel不喜欢这些工作表。它会自动为重复的标题添加“1”。

这不是你的代码添加1,它是Excel。

所以你需要维护全局周迭代器,所以它变成第1周,第2周,第3周,第4周:

// you may need to do global $week if this code runs in a function
if ( ! isset( $week ) )
  $week = 0;
for ($w=1; $w<3; $w++) :
  // ...
  $week += $w;
  $objPHPExcel->getActiveSheet()->setTitle("Week ".$week);
  // ...
endfor;

或者您需要为自己实施一些其他工作表命名方案。

答案 1 :(得分:0)

使用for循环设置工作表名称

{{1}}

答案 2 :(得分:-1)

我已经重新格式化了代码,这对我来说非常有用。请试一试     

 
    error_reporting(E_ALL);
define('EOL', (PHP_SAPI == 'cli') ? PHP_EOL : '
');

require_once dirname('your file') . '/PHPExcel/Classes/PHPExcel.php'; $objPHPExcel = new PHPExcel(); $objPHPExcel->getProperties()->setCreator("Maarten Balliauw") ->setLastModifiedBy("Maarten Balliauw") ->setTitle("Office 2007 XLSX Test Document") ->setSubject("Office 2007 XLSX Test Document") ->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.") ->setKeywords("office 2007 openxml php") ->setCategory("Test result file"); $y = 0; for ($w = 1; $w < 10; $w++) { $sheetId = $y; $objPHPExcel->createSheet(NULL, $sheetId); $objPHPExcel->setActiveSheetIndex($sheetId); $objPHPExcel->getActiveSheet()->setTitle("Week " . $w); $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(12); $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(12); $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(14); $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(20); $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(16); $objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(12); $objPHPExcel->getActiveSheet()->getColumnDimension('G')->setWidth(10); $objPHPExcel->getActiveSheet()->getColumnDimension('H')->setWidth(15); $objPHPExcel->getActiveSheet()->getColumnDimension('I')->setWidth(10); $objPHPExcel->getActiveSheet()->getColumnDimension('J')->setWidth(17.5); $styleArray = array( 'font' => array( 'bold' => true ) ); $centreAlign = array( 'font' => array( 'bold' => true ) ); $objPHPExcel->setActiveSheetIndex($sheetId)->mergeCells('A1:J6'); $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setWrapText(true); $objPHPExcel->getActiveSheet()->getStyle("A1")->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); $objPHPExcel->getActiveSheet()->getStyle('A1:J5')->applyFromArray($styleArray); $objPHPExcel->getActiveSheet()->getStyle('A:E')->applyFromArray($centreAlign); } $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007'); ob_end_clean(); header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); header('Content-Disposition: attachment;filename="simple.xlsx"'); header('Cache-Control: max-age=0'); header('Cache-Control: max-age=1'); header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); // always modified header('Cache-Control: cache, must-revalidate'); // HTTP/1.1 header('Pragma: public'); // HTTP/1.0 $objWriter->save('php://output'); </pre>

检查以下链接以了解我的输出:
https://www.screencast.com/t/PnQsQZ8JC6dt