0

I'm exporting data from a MySQL DB to Excel using PHPSpreadsheet. The spreadsheet is working fine, the data is exporting correctly, however, the numbers are being stored as text. I can get PHPSpreadsheet to convert the cell to a number, but the number is still stored as text.

    use PhpOffice\PhpSpreadsheet\Spreadsheet;
    use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
    use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
    
    $spreadsheet = new Spreadsheet();
    $sheet = $spreadsheet->getActiveSheet(); 
    
    
    $sheet->setCellValue('A1', 'Reference'); 
    $sheet->setCellValue('B1', 'Project Name'); 
    $sheet->setCellValue('C1', 'Value'); 
    $sheet->setCellValue('D1', 'Sales Rep');
    $sheet->setCellValue('E1', 'Stage'); 

    if ($totalrows_lpr > 0) {
        $i =1;
        while ($row_lpr = mysqli_fetch_assoc($result_lpr)) {
            $i++;
            $projectID = $row_lpr['projectID'];
            $projectRef = $row_lpr['projectRef'];
            $projectName = $row_lpr['projectName'];
            $value = projectvalue2($projectID);
            $staffName = $row_lpr['firstName'] . " " . $row_lpr['surname'];
            $stageName = $row_lpr['stageName'];
            $sheet->setCellValue('A'.$i , $projectRef);
            $sheet->setCellValue('B'.$i , $projectName);
            $sheet->setCellValue('C'.$i , $value);
            $sheet->getStyle('C'.$i)->getNumberFormat()->setFormatCode('#,##0.00');
            $sheet->setCellValue('D'.$i , $staffName);
            $sheet->setCellValue('E'.$i , $stageName);
        }
    }

The code for the function (projectvalue2):

$sql_pv = "SELECT MAX(productTotal)
                    FROM
                    ( SELECT SUM(productTotal) AS productTotal 
                        FROM quote_items
                        LEFT JOIN quote_details
                        ON quote_items.quoteID = quote_details.quoteID
                        WHERE quote_items.projectID = $projectID AND quote_details.visible = 1
                        GROUP BY quote_items.quoteID
                    )
                    quote_items
                    ";

The number is formatted correctly with thousand separators and 2 decimal points. If the cell has a zero value as in no data from the database then it is stored as a number in the cell.

Rayza
  • 123
  • 1
  • 10

1 Answers1

0

I received some help from Mark Baker on the GitHub/PHPSpreadsheet forums. I changed my data type in the database from a decimal(13,2) to a float and now the cells display numbers instead of text.

Rayza
  • 123
  • 1
  • 10