How to set active sheet without loading an xlsx file?

你说的曾经没有我的故事 提交于 2020-01-01 03:26:11

问题


I am using PHPExcel to generate an xl using php. I am not loading an xl sheet but creating new sheets using

$phpExcel = new PHPExcel();
$phpExcel->getActiveSheet()->setTitle("My Sheet");

I want to set active sheet using phpExcel using $phpExcel->setActiveSheetIndexByName("2");

but im getting an error setActiveSheetIndexByName not defined function.

Please help


回答1:


You do, of course, need to create/add additional worksheets to be able to change the active sheet: using new PHPExcel() will only create a workbook containing a single sheet.

You can set the active sheet using either the sheet index (sheets are indexed from 0);

$objPHPExcel->setActiveSheetIndex(2);

or by name

$objPHPExcel->setActiveSheetIndexByName('My Second Sheet');

Adding a new sheet using either the createSheet() or addSheet() methods will automatically set that new worksheet to the active worksheet. By default, any new worksheet will be given a name comprising the word "Worksheet" and a number until you use setTitle() to change it.




回答2:


Add below function into Excel.php class file:

function setActiveSheet($sheetnumber) {
        $this->objPHPExcel->setActiveSheetIndex($sheetnumber);
    }

then call that function like this :

$phpExcel->setActiveSheet(0);



回答3:


You shouldn't need ByName. Try just setActiveSheetIndex(2);.




回答4:


If you are directly manipulating the xml in workbook.xml

$replace = '#activeTab="\d"#i';
$with = 'activeTab="0"';
$newxmlfile=preg_replace($replace,$with,$newxmlfile); // force activetab to sheet 1


来源:https://stackoverflow.com/questions/18271943/how-to-set-active-sheet-without-loading-an-xlsx-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!