Modify cell = style erased

Topics: Developer Forum, User Forum
Jun 13, 2012 at 1:31 PM
Edited Jun 13, 2012 at 3:15 PM

Hi,

When I modify a value of cellule, my style (borders, alignments...) is erased.

So I have to remake the style with phpExcel but there isn't another solution ?

Thanks

Coordinator
Jun 13, 2012 at 8:08 PM

This shouldn't occur, and I have been unable to replicate it. Can you please generate a simple script that will demonstrate this problem so that I can investigate. Thanks.

Jun 14, 2012 at 1:06 PM
Edited Jun 14, 2012 at 1:10 PM

There are borders (for example) around cell k9 but after modifications, all styles are removed (font size, font style, borders...).

This my script simplified :

<?php

// Création de l'objet Reader pour un fichier Excel 2007
$objReader = new PHPExcel_Reader_Excel5();

// Lecture du fichier.
$objPHPExcel = $objReader->load("../originaux/original_notice.xls");

$objPHPExcel->getDefaultStyle()->applyFromArray(array(
'font'=>array(
'name'      =>  'Times News Roman',
'size'      =>  12,
'bold'      => false)));

// Modification

$objPHPExcel->getActiveSheet()->setCellValue('k9', 'test');

$objWriter = new PHPExcel_Writer_Excel5($objPHPExcel);
$objWriter->save('../files/'.$donnees_info['IDpersonne'].'_notice.xls');

?>
Coordinator
Jun 14, 2012 at 2:32 PM

I still can't replicate this unless I set readDataOnly to true... when I would expect styling to be lost because I'm telling the reader to ignore styling.

Unfortunately, if I can't replicate a problem, it's almost impossible to fix because it becomes a case of guessing.

Jun 14, 2012 at 3:15 PM
Edited Jun 14, 2012 at 3:15 PM

Ok..

So, to reset my borders i write :

$objPHPExcel->getActiveSheet()->getStyle('k15')->getBorders()->getBottom()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
$objPHPExcel->getActiveSheet()->getStyle('k15')->getBorders()->getRight()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
$objPHPExcel->getActiveSheet()->getStyle('k15')->getBorders()->getLeft()->setBorderStyle(PHPExcel_Style_Border::BORDER_HAIR);
$objPHPExcel->getActiveSheet()->getStyle('k15')->getBorders()->getTop()->setBorderStyle(PHPExcel_Style_Border::BORDER_HAIR);


Is there another faster method ?

Jun 14, 2012 at 6:00 PM

I've not worked with formatting Excel 5 files but don't quite understand why your styles are being lost. If you remove the lines writing data are your styles preserved? What about removing the lines to set the default style. Anyway, something like:

$objPHPExcel->getActiveSheet()->getStyle('k15')->
applyFromArray(
  array(
    'borders' => array(
      'bottom' => array('style' => PHPExcel_Style_Border::BORDER_THIN),
      'left' => array('style' => PHPExcel_Style_Border::BORDER_THIN),
      'right' => array('style' => PHPExcel_Style_Border::BORDER_HAIR),
      'top' => array('style' => PHPExcel_Style_Border::BORDER_HAIR),
    ),
  )
);

should be be a faster way to set styles. Faster still if you operate on the range of cells that you want to reset instead of individually (there are also 'vertical' and 'horizontal' border types to make use of for the internal borders)

Jun 14, 2012 at 8:25 PM

If I remove the lines writing data, my styles are preserved... I don't understand :(