douwen3500
2014-11-13 00:49
采纳率: 0%
浏览 124
已采纳

在PHPExcel中获取命名范围

Using PHPExcel, I would like to loop through all the named ranges in a workbook and then create a new tab for each one.

I am currently doing this in excel with a macro, like so:

Dim sheetName As String
sheetName = ActiveSheet.Name

Dim nName As Name

For Each nName In Names
    If InStr(1, nName.RefersTo, sheetName) > 0 Then
        Application.Goto Reference:=nName.Name
        Selection.Copy
        Sheets.Add After:=Sheets(Sheets.Count)
        ActiveSheet.Name = nName.Name
        Selection.PasteSpecial Paste:=xlPasteValues
    End If
Next nName
mystring = Sheets(1).Name
ActiveWorkbook.SaveCopyAs Filename:="P:\DP\CWBI\" & mystring & "_parsed.xls"

I can loop through active sheets in PHPExcel and dump them into an array for processing, like this:

$objReader = PHPExcel_IOFactory::createReader('Excel2007');
$sheets = $objReader->listWorksheetNames($file);

print_r ($sheets);

Does anyone know if it's possible to loop through all the named ranges?

Edit:

I'm trying the first suggestion below, but not getting any output. Here's the code:

$file="span_test.xlsx";
$objPHPExcel = PHPExcel_IOFactory::load($file);

foreach($objPHPExcel->getNamedRanges() as $name => $namedRange) {
   echo $namedRange . "</br>";   
}

Got it working now, my mistake in trying to echo an object. should be

print_r($name)

图片转代码服务由CSDN问答提供 功能建议

使用PHPExcel,我想循环遍历工作簿中的所有命名范围,然后为每个范围创建一个新选项卡 一个。

我目前正在使用宏执行此操作,如下所示:

  Dim sheetName As String 
sheetName = ActiveSheet。 名称
 
目标名称
 
对于名称中的每个nName 
如果InStr(1,nName.RefersTo,sheetName)&gt;  0然后
 Application.Goto参考:= nName.Name 
 Selection.Copy 
 Sheets.Add After:= Sheets(Sheets.Count)
 ActiveSheet.Name = nName.Name 
 Selection.PasteSpecial Paste:= xlPasteValues  
结束如果
下一个nName 
mystring = Sheets(1).Name 
ActiveWorkbook.SaveCopyAs Filename:=“P:\ DP \ CWBI \”&amp; 神秘&amp;  “_parsed.xls”
   
 
 

我可以遍历PHPExcel中的活动工作表并将它们转储到数组中进行处理,如下所示:

  $ objReader = PHPExcel_IOFactory :: createReader('Excel2007'); 
 $ sheets = $ objReader-&gt; listWorksheetNames($ file); 
 
print_r($ sheets); 
    
 
 

有谁知道是否可以遍历所有命名的范围?

编辑:

I 我正在尝试下面的第一个建议,但没有得到任何输出。 这是代码:

  $ file =“span_test.xlsx”; 
 $ objPHPExcel = PHPExcel_IOFactory :: load($ file); 
 
foreach($ objPHPExcel-&gt  ; getNamedRanges()as $ name =&gt; $ namedRange){
 echo $ namedRange。  “&LT; / BR&gt;” 中;  
} 
   
 
 

现在正常工作,我在尝试回显对象时犯了错误。 应该是

  print_r($ name)
   
 
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • doushu5805 2014-11-13 00:58
    已采纳

    You have to actually load the workbook to access the named ranges

    foreach($objPHPExcel->getNamedRanges() as $name => $namedRange) { ... }
    

    which returns a PHPExcel_NamedRange object for each named range

    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题