2013-02-26 9 views
5

Już sprawdziłem to Merging cells in Excel by rows and columns together using PHPExcel, ale pokazano tylko dla dodania wiersza, jednak zastosowałem kilka testów dla kolumny, ale żadna nie działa.Jak scalić kolumnę w Excelu za pomocą PHPExcel

Ten kod jest tworzony pomyślnie, ale dane wyjściowe w programie Excel są wyświetlane z błędem.

EDIT

jednak to działa, jeśli usunąć LLOP i umieścić to w prostych słowach -

$objPHPExcel->setActiveSheetIndex(0)->mergeCells('A1:B1'); 
$objPHPExcel->getActiveSheet()->setCellValue('A1', 'Swapnesh'); 

kodu głównego dodawania danych -

// Add some data 
echo date('H:i:s') , " Add some data" , EOL; 
$i = "A"; 
$j ="B"; 
for($num =1; $num <= 5; $num++) 
{ 
    $concat = "{$i}1:{$j}1"; 
    $objPHPExcel->setActiveSheetIndex(0)->mergeCells($concat); 
    $i++;$j++; 
} 
$objPHPExcel->getActiveSheet()->setCellValue('A1', 'Swapnesh'); 

My cały kod -

<?php 
error_reporting(E_ALL); 
ini_set('display_errors', TRUE); 
ini_set('display_startup_errors', TRUE); 

define('EOL',(PHP_SAPI == 'cli') ? PHP_EOL : '<br />'); 

date_default_timezone_set('Asia/Calcutta'); 

/** Include PHPExcel */ 
require_once 'Classes/PHPExcel.php'; 


// Create new PHPExcel object 
echo date('H:i:s') , " Create new PHPExcel object" , EOL; 
$objPHPExcel = new PHPExcel(); 

// Set document properties 
echo date('H:i:s') , " Set document properties" , EOL; 
$objPHPExcel-> 
getProperties()->setCreator("Swapnesh Sinha") 
          ->setLastModifiedBy("Swapnesh") 
          ->setTitle("Office 2007 XLSX Test Document") 
          ->setSubject("Office 2007 XLSX Test Document") 
          ->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.") 
          ->setKeywords("office 2007 openxml php") 
          ->setCategory("Test result file"); 


// Add some data 
echo date('H:i:s') , " Add some data" , EOL; 
$i = "A"; 
$j ="B"; 
for($num =1; $num <= 5; $num++) 
{ 
    $concat = "{$i}1:{$j}1"; 
    $objPHPExcel->setActiveSheetIndex(0)->mergeCells($concat); 
    $i++;$j++; 
} 
$objPHPExcel->getActiveSheet()->setCellValue('A1', 'Swapnesh'); 

// Rename worksheet 
echo date('H:i:s') , " Rename worksheet" , EOL; 
$objPHPExcel->getActiveSheet()->setTitle('Student Data'); 


// Set document security 
echo date('H:i:s') , " Set document security" , EOL; 
$objPHPExcel->getSecurity()->setLockWindows(true); 
$objPHPExcel->getSecurity()->setLockStructure(true); 
$objPHPExcel->getSecurity()->setWorkbookPassword("PHPExcel"); 


// Set sheet security 
echo date('H:i:s') , " Set sheet security" , EOL; 
$objPHPExcel->getActiveSheet()->getProtection()->setPassword('PHPExcel'); 
$objPHPExcel->getActiveSheet()->getProtection()->setSheet(true); // This should be enabled in order to enable any of the following! 
$objPHPExcel->getActiveSheet()->getProtection()->setSort(true); 
$objPHPExcel->getActiveSheet()->getProtection()->setInsertRows(true); 
$objPHPExcel->getActiveSheet()->getProtection()->setFormatCells(true); 


// Set active sheet index to the first sheet, so Excel opens this as the first sheet 
$objPHPExcel->setActiveSheetIndex(0); 


// Save Excel 2007 file 
echo date('H:i:s') , " Write to Excel2007 format" , EOL; 
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007'); 
//$objWriter->save(str_replace('.php', '.xlsx', __FILE__)); 
$filename = "Student-data-sheet".".xlsx"; 
$objWriter->save($filename); 
echo date('H:i:s') , " File written to " , str_replace('.php', '.xlsx', pathinfo(__FILE__, PATHINFO_BASENAME)) , EOL; 


// Echo memory peak usage 
echo date('H:i:s') , " Peak memory usage: " , (memory_get_peak_usage(true)/1024/1024) , " MB" , EOL; 

// Echo done 
echo date('H:i:s') , " Done writing file" , EOL; 
echo 'File has been created in ' , getcwd() , EOL; 

Odpowiedz

10

Rozwiązuję samodzielnie -

Właściwie w przypadku scalania kolumn faktycznie nie musimy iterować. powiedzmy, że chcę scalić z kolumny A1 do E1.

Więc zamiast iść pętla przez pętli to można bardzo łatwo zrobić przez -

$objPHPExcel->setActiveSheetIndex(0)->mergeCells('A1:E1'); 

Więc w moim przypadku najpierw trzeba znaleźć ostatni element i pierwszy, a potem po prostu umieścić w mergeCells() metody aby działało.

5

Ten problem występuje, gdy próbujesz scalić dwie komórki w pętli. Powód nie jest pętlą, którą powtarzasz, ale jej nazwą kolumną, którą przekazujesz. jeśli podajesz błędne nazwy kolumn, program Excel nie będzie generował prawidłowych wyników.

Oto prosty program do zdobycia następnej kolumny programu Excel, abyśmy nie musieli stawić czoła takiemu problemowi.

/* manage excel index */ 
function getColumn($prevColumn){ 
    if(strlen($prevColumn)==1&&$prevColumn!="Z") 
    { $prevColumn++;return $prevColumn; } 

    $colum = ''; 
    $list = str_split($prevColumn); 

    if(count($list)==1&&$list[0]=='Z') 
    { 
     $list[0] = "A"; 
     $list[1] = "A"; 
    } 
    else if(count($list)==2&&$list[1]!='Z') 
    { 
     $list[1]++; 
} 
    else if(count($list)==2&&$list[1]=='Z') 
    { 
     $list[0] = "B"; 
     $list[1] = "A"; 
} 
return implode('',$list); 
} 

## us this like below 

$column = "A"; 
$row = 1; 
foreach($array_to_print as $details) 
{ 

$column = getColumn($column); // product next column B 
$Excelobj->getActiveSheet()->setCellValue($column.$row, $details); 
$column = getColumn($column);// product next column C 
$Excelobj->setActiveSheetIndex(0)->mergeCells($from_cell.':'.$to_cell);// now this will work till A-ZZ column in excel 
} 
+0

thx za program – swapnesh

Powiązane problemy