Asked  7 Months ago    Answers:  5   Viewed   182 times

I am using PHPExcel to extract data from my database onto an organized Excel sheet. Everything is working great except for one thing. My database entries can sometimes contain HTML markups like <strong></strong>, <BR>, <p></p> etc ... So I managed to get this PHP line working, this is working great to replace my <BR> markups to a space.

$data = str_replace("<br />", "n", $data);         
$data = str_replace("<br/>", "n", $data);          
$data = str_replace("<br>", "n", $data);

However when I try doing the following it does nothing. I was expecting that it would bold the text.

$data = str_replace("<strong>", '&B', $data);

I read on these forums that its best to use preg_replace and setup an array for all the HTML markups I need replaced. For the life of me I can't understand how to use preg_replace. Would someone please give me some advise what is the best way to replace markups such as <strong> and </strong> to bold when it exports, this would be very much appreciated.

 Answers

40

'&B' will only set bold text for printed page headers and footers, as described in the section of the documentation entitled Setting the print header and footer of a worksheet

Formatting the content of a cell is described in the section of the documentation entitled Formatting cells, something like:

$objPHPExcel->getActiveSheet()->getCell('A1')->setValue('bold');
$objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setBold(true);

or (if your content contains actual markup) using Rich Text objects if you only want to set part of the text to bold.

$objRichText = new PHPExcel_RichText();
$objRichText->createText('This text is ');

$objBoldTextRun = $objRichText->createTextRun('bold');
$objBoldTextRun->getFont()->setBold(true);
$objPHPExcel->getActiveSheet()->getCell('B1')->setValue($objRichText);

but you will need to parse your markup to convert it to a Rich Text object

Saturday, May 29, 2021
 
BrunoRamalho
answered 7 Months ago
77

Eventually solved it with the help of TiMESPLiNTER (give him the credit!), didn't know $1 didn't get brought along and that you have to call a variable $matches in the callback function and get $matches[1] instead of $1.

$counter = 1;

function parse(){
    function cb($matches){
        global $counter;

        return '<span class="b">'. $this->counter .'. ' . $matches[1] . '</span>';
    }

    $string = preg_replace_callback("_(.*?)_si", 'cb', $string);
}
Saturday, May 29, 2021
 
mario
answered 7 Months ago
33

EDIT I added a pull request with a fix: https://github.com/PHPOffice/PHPExcel/pull/251

This happens when you try to duplicate cell's style to the same cell; Take a look at this:

$phpe = new PHPExcel();
$sheet = $phpe->createSheet();

$sheet->setCellValue('A1', 'hi there') ;
$sheet->setCellValue('A2', 'hi again') ;

$sheet->duplicateStyle($sheet->getStyle('A1'), 'A2');

$writer = new PHPExcel_Writer_Excel2007($phpe);
$writer->save('./test.xlsx');

It will work just fine. BUT if I add another line like this:

$sheet->duplicateStyle($sheet->getStyle('A1'), 'A1');

then bang, infinite recursion starts after calling the save method

To fix your code, you should modify this part:

$sheet->duplicateStyle((($level == 0) ?  $this->styles['category'] : $this->styles['subcategory']), Cell::NUMBER . $row);

To something along the lines of:

$style = ($level == 0) ?  $this->styles['category'] : $this->styles['subcategory'];
$targetCoords = Cell::NUMBER . $row;
if($style->getActiveCell() != $targetCoords) {
    $sheet->duplicateStyle($style, $targetCoords);
}
Saturday, May 29, 2021
 
rblarsen
answered 7 Months ago
27

It's mentioned in the documentation.


    $objRichText = new PHPExcel_RichText();
    $objRichText->createText('This invoice is ');

    $objPayable = $objRichText->createTextRun('payable within thirty days after the end of the month');
    $objPayable->getFont()->setBold(true);
    $objPayable->getFont()->setItalic(true);
    $objPayable->getFont()->setColor( new PHPExcel_Style_Color( PHPExcel_Style_Color::COLOR_DARKGREEN ) );

    $objRichText->createText(', unless specified otherwise on the invoice.');

    $objPHPExcel->getActiveSheet()->getCell('A18')->setValue($objRichText);

Saturday, May 29, 2021
 
toesslab
answered 7 Months ago
43

Android >= Honeycomb (self-explaining)

view.setX(x);
view.setY(y);

Android < Honeycomb Instead of directly setting it to the position you will tell "set it x pixels away from the left/top/right/bottom border".

RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
lp.setMargins(x, y, 0, 0);

view.setLayoutParams(lp);
Saturday, October 30, 2021
 
Manish Das
answered 1 Month ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 
Share