Cheerio
Cheerio

Reputation: 1240

change one field with PHPExcel

I want to change just one field before set the values with PHPExcel:
For example to set 001 before the phone number:


....

if ($result = $DB->execute($sql) or die(mysql_error())) {
    $objPHPExcel = new PHPExcel();
    $objPHPExcel->getActiveSheet()->setTitle('List');
    $rowNumber = 1;
    while ($row = mysql_fetch_row($result)) {
        $col = 'A';
        foreach ($row as $cell) {
            $objPHPExcel->getActiveSheet()->setCellValue($col.$rowNumber, .'001 '.$row['phone']);
            $objPHPExcel->getActiveSheet()->setCellValue($col . $rowNumber, $cell);
            $col++;
        }
        $rowNumber++;
    }
    $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
    header('Content-Type: application/vnd.ms-excel');
    header('Content-Disposition: attachment;filename="myFile.xls"');
    header('Cache-Control: max-age=0');
    $objWriter->save('php://output');
    exit();
}



Upvotes: 0

Views: 1236

Answers (1)

Mark Baker
Mark Baker

Reputation: 212522

$objPHPExcel->getActiveSheet()
            ->setCellValueExplicit($col.$rowNumber, .'001 '.$row['phone'], PHPExcel_Cell_DataType::TYPE_STRING);

or set the number format mask to use the requisite number of digits, which will then give leading zeroes if the number is shorter

Upvotes: 1

Related Questions