php 如何在不加载 xlsx 文件的情况下设置活动工作表?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18271943/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set active sheet without loading an xlsx file?
提问by Simone
I am using PHPExcel
to generate an xl using php. I am not loading an xl sheet but creating new sheets using
我正在PHPExcel
使用 php 生成一个 xl。我没有加载 xl 工作表,而是使用创建新工作表
$phpExcel = new PHPExcel();
$phpExcel->getActiveSheet()->setTitle("My Sheet");
I want to set active sheet using phpExcel using $phpExcel->setActiveSheetIndexByName("2");
我想使用 phpExcel 设置活动工作表 $phpExcel->setActiveSheetIndexByName("2");
but im getting an error setActiveSheetIndexByName not defined function.
但我收到一个错误 setActiveSheetIndexByName not defined function.
Please help
请帮忙
回答by Mark Baker
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.
当然,您确实需要创建/添加额外的工作表才能更改活动工作表:使用 new PHPExcel() 只会创建一个包含单个工作表的工作簿。
You can set the active sheet using either the sheet index (sheets are indexed from 0);
您可以使用工作表索引设置活动工作表(工作表从 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.
使用 createSheet() 或 addSheet() 方法添加新工作表将自动将该新工作表设置为活动工作表。默认情况下,任何新工作表都将被赋予一个包含单词“工作表”和一个数字的名称,直到您使用 setTitle() 对其进行更改。
回答by heena
Add below function into Excel.php class file:
将以下函数添加到 Excel.php 类文件中:
function setActiveSheet($sheetnumber) {
$this->objPHPExcel->setActiveSheetIndex($sheetnumber);
}
then call that function like this :
然后像这样调用该函数:
$phpExcel->setActiveSheet(0);
回答by zzapper
If you are directly manipulating the xml in workbook.xml
如果直接操作workbook.xml中的xml
$replace = '#activeTab="\d"#i';
$with = 'activeTab="0"';
$newxmlfile=preg_replace($replace,$with,$newxmlfile); // force activetab to sheet 1
回答by DevlshOne
You shouldn't need ByName
. Try just setActiveSheetIndex(2);
.
你不应该需要ByName
. 试试吧setActiveSheetIndex(2);
。