2010-08-11 10 views
21

PHPExcel $ सेल> getColumn() रिटर्न 'ए', 'बी', 'सी', ...PHPExcel कैसे सेल से स्तंभ अनुक्रमणिका पाने के लिए

जो पूर्णांक प्राप्त करने के लिए सबसे अच्छा तरीका है (0 , 1, 2, ...) सेल से।

यह फ़ंक्शन मौजूद नहीं है।

$colIndex = $cell->getColumnIndex(); 

तो chr परिवर्तित ASCII को withoput विकल्प क्या है?

+0

ध्यान रखें कि getColumn() कॉलम> 26 के लिए 'एए' (और इसी तरह) लौट सकते हैं ... – AlexV

उत्तर

44
$colIndex = PHPExcel_Cell::columnIndexFromString($cell->getColumn()); 
+6

ठीक ठीक काम करता है, मैं ध्यान दें कि दिए गए मान स्तंभ एक के लिए 1 है, लेकिन $ cell = "वर्कशीट-> getCellByColumnAndRow ($ col, $ row); कॉलम ए के लिए 0 का उपयोग करता है –

+2

Yup, quirky ... कुछ मैंने विरासत में लिया जब मैंने PHPExcel के विकास को संभाला, लेकिन इसे बदलना पिछड़ा तोड़ देगा बहुत से लोगों के लिए संगतता –

+3

PHPExcel_Cell :: stringFromColumnIndex भी उन लोगों के लिए है जिन्हें दूसरी तरफ जाने की आवश्यकता है। – cgTag

5

आप फिर से कॉलम इंडेक्स प्राप्त कर सकते हैं।

$xls = PHPExcel_IOFactory::load($fn); 
$xls->setActiveSheetIndex(0); 
$sheet = $xls->getActiveSheet(); 

foreach($sheet->getRowIterator() as $row) 
{ 
    foreach($row->getCellIterator() as $key => $cell) 
    { 
     echo $key; // 0, 1, 2... 
     echo $cell->getCalculatedValue(); // Value here 
    } 
} 
+0

तोड़ दिया जाएगा और कैसे प्रत्येक पंक्ति के दूसरे कक्ष के लिए केवल मूल्य प्राप्त होता है? –

1
If you want to get decrement cell address using this function, you have to use another function with this function as follows. 

<?php 
echo columnLetter("AB"); 

function columnLetter($c){ 


$letter=""; 
    $c = intval(columnNumber($c)); 
    if ($c<=0) return ''; 

    while($c != 0){ 
     $p = ($c - 1) % 26; 
     $c = intval(($c - $p)/26); 
     $letter = chr(65 + $p) . $letter; 
    } 

    return $letter; 

} 

function columnNumber($col){ 

    $col = str_pad($col,2,'0',STR_PAD_LEFT); 
    $i = ($col{0} == '0') ? 0 : (ord($col{0}) - 64) * 26; 
    $i += ord($col{1}) - 64; 

    return $i-1; 

} 
?> 
संबंधित मुद्दे