php PHPExcel 检查工作表是否存在

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15365599/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 09:06:45  来源:igfitidea点击:

PHPExcel Check if sheet exists

phpphpexcel

提问by Get Off My Lawn

I am using phpExcel, and I can't find anything to check if a sheet exists. What I would like to accomplish is something like this:

我正在使用 phpExcel,我找不到任何东西来检查工作表是否存在。我想完成的是这样的:

if(!$excel->sheetExists(1)){
    $excel->createSheet(1);
    $sheet = $excel->setSheet(1);
}
// Do some stuff with the sheet

So. My question: How can I check if a sheet exists?

所以。我的问题:如何检查工作表是否存在?

Edit

编辑

Would this work?

这行得通吗?

try{
    $sheet = $this->excel->setActiveSheetIndex(1);
}catch(Exception $e){
    $excel->createSheet(1);
    $sheet = $excel->setActiveSheetIndex(1);
}

回答by Mark Baker

If you simply want to know whether a sheetexists at index 1, then

如果您只想知道索引 1 处是否存在工作表,则

$sheetCount = $excel->getSheetCount();

will return a count of the worksheets. As sheets are indexed incrementally from 0, then a sheet at index 1 will only exist if the count is 2 or more.

将返回工作表的计数。由于工作表是从 0 开始递增索引的,因此只有在计数为 2 或更多时才会存在索引为 1 的工作表。

If you want to know whether a named sheet exists, then

如果你想知道一个命名表是否存在,那么

$sheetNames = $excel->getSheetNames();

will return an array of sheet names (indexed by their index position), and you can then test using in_array();

将返回一个工作表名称数组(由它们的索引位置索引),然后您可以使用 in_array() 进行测试;

The

$excel->getSheet()

method will throw an exception if the requested sheet (by index) doesn't exist, so wrap it in a try/catch block would be another approach

如果请求的工作表(按索引)不存在,方法将抛出异常,因此将其包装在 try/catch 块中将是另一种方法

$excel->getSheetByName()

returns a NULL value if the named worksheet doesn't exist

如果指定的工作表不存在,则返回 NULL 值

回答by Chris L

You can check if a sheet exists by name with the method sheetNameExists($pSheetName).

您可以使用sheetNameExists($pSheetName)方法按名称检查工作表是否存在。

回答by mkaatman

getSheet($sheetNumber)is how you check if a sheet exists.

getSheet($sheetNumber)是如何检查工作表是否存在。

回答by Biswadeep Sarkar

Yes your code will also work:

是的,您的代码也可以使用:

try {
        $objWorksheet =  $objPHPExcel->setActiveSheetIndex(1); 
}
catch (Exception $e) {
    echo 'Sheet is not exists!';
}

回答by ashraf mohammed

$sheet=$excel->getSheet(1);
// or you can get sheet by name $sheet=$excel->getSheetByName("Sheet1");
if(!empty($sheet)&&is_object($sheet)){
      //sheet already exist
}else{
      //sheet does not exist. Write your code here!
}

回答by pala dattadri

"TO KNOW THE PRESENT ACTIVE SHEET NUMBER(INDEX)" $this->activeSheet->getActiveSheetIndex()

“了解当前活动表编号(索引)” $this->activeSheet->getActiveSheetIndex()

NOTE: $loadExcel=PHPExcel_IOFactory::load("excelSheetName.xlsx"); $this->activeSheet=$loadExcel;

注意:$loadExcel=PHPExcel_IOFactory::load("excelSheetName.xlsx"); $this->activeSheet=$loadExcel;