dpwbc42604 2015-05-11 12:30
浏览 651
已采纳

setActiveSheetIndex无法正常工作

I'm using PHPExcel to make export xls with 2 worksheets. When I open the generated xls file, it opens directly to the second worksheet, even though I've declared in the code that generates the first worksheet:

$objPHPExcel->setActiveSheetIndex(0) 

This happened after I'd added the following lines in the code that generates the second worksheet.

$objPHPExcel->getSheet(1)->getStyle('A1:E1')->getFont()->setBold(true);

    $objPHPExcel->getSheet(1)->getColumnDimension('A')->setWidth(15);
    $objPHPExcel->getSheet(1)->getRowDimension(1)->setRowHeight(40);
    $objPHPExcel->getSheet(1)->getStyle('A:F')->getAlignment()->setWrapText(true); 
    $objPHPExcel->getSheet(1)->getColumnDimension('B')->setWidth(40);
    $objPHPExcel->getSheet(1)->getStyle('B')->getAlignment()->setWrapText(true); 
    $objPHPExcel->getSheet(1)->getColumnDimension('C')->setWidth(15);
    $objPHPExcel->getSheet(1)->getColumnDimension('D')->setWidth(15);
    $objPHPExcel->getSheet(1)->getColumnDimension('E')->setWidth(15);
    $objPHPExcel->getSheet(1)->getColumnDimension('E')->setAutoSize(true);

    $objPHPExcel->getSheet(1)->getPageMargins(1)->setTop(1);
    $objPHPExcel->getSheet(1)->getPageMargins(1)->setRight(0.75);
    $objPHPExcel->getSheet(1)->getPageMargins(1)->setLeft(0.75);
    $objPHPExcel->getSheet(1)->getPageMargins(1)->setBottom(1);

    $objPHPExcel->getSheet(1)->getStyle('A1:E1')->getFill()
                    ->setFillType(PHPExcel_Style_Fill::FILL_SOLID)
                    ->getStartColor()->setRGB('4EE900');

If I comment some of the code lines, (as shown in the picture) the active sheet index works fine.

enter image description here

So, if I remove these lines, it works fine. Any Idea how to fix this to keep first worksheet as the active worksheet?

  • 写回答

1条回答 默认 最新

  • douzi1350 2015-05-12 07:23
    关注

    Creating a new worksheet automatically sets that as the active sheet.

    Unless you use $objPHPExcel->setActiveSheetIndex(0); after creating your second worksheet, then the second sheet will be your active sheet.

    And you can always set the active sheet manually just before your save using

    $objPHPExcel->setActiveSheetIndex(0);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 seata使用出现报错,其他服务找不到seata
  • ¥35 引用csv数据文件(4列1800行),通过高斯-赛德尔法拟合曲线,在选取(每五十点取1点)数据,求该数据点的曲率中心。
  • ¥20 程序只发送0X01,串口助手显示不正确,配置看了没有问题115200-8-1-no,如何解决?
  • ¥15 Google speech command 数据集获取
  • ¥15 vue3+element-plus页面崩溃
  • ¥15 像这种代码要怎么跑起来?
  • ¥15 安卓C读取/dev/fastpipe屏幕像素数据
  • ¥15 pyqt5tools安装失败
  • ¥15 mmdetection
  • ¥15 nginx代理报502的错误