Я экспортирую данные в файл CSV, но есть значение " 11e5 ", которое в Excel интерпретируется как число. Вместо текста в таблице Excel отображается 1.10E + 06 .
Я пытался добавить префикс к значению апострофом, но затем он отображается как 1100000 в Excel.
Вот код, который я использую для тестирования, живой код получает данные из MySQL.
function array2csv(array &$array)
{
if (count($array) == 0) {
return null;
}
ob_start();
$df = fopen("php://output", 'w');
fputcsv($df, array_keys(reset($array)));
foreach ($array as $row) {
fputcsv($df, $row);
}
fclose($df);
return ob_get_clean();
}
function download_send_headers($filename){
$now = gmdate("D, d M Y H:i:s");
header("Expires: Tue, 03 Jul 2001 06:00:00 GMT");
header("Cache-Control: max-age=0, no-cache, must-revalidate, proxy-revalidate");
header("Last-Modified: {$now} GMT");
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header("Content-Disposition: attachment;filename={$filename}");
header("Content-Transfer-Encoding: binary");
}
$row1 = array ('Assignment name', 'tet4' );
$row2 = array ('Class name','11e5' );
$row3 = array ('Name', 'Target', 'Start grade', 'End Grade', 'Avarage grade', 'May', 'June', 'July', 'August','06th Aug', '13th Aug', '20th Aug', '27th Aug', '03rd Sep', 'Last 7 days', 'Last 24 hours', 'Total points' );
$row4 = array ('Maan Roy', 2, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, 0, 32 );
$row5 = array ( 'mac mac', 0, 1, 2, 1.5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 98, 0, 98 );
$response = array();
array_push($response, $row1);
array_push($response, $row2);
array_push($response, $row3);
array_push($response, $row4);
array_push($response, $row5);
download_send_headers("data_export_" . date("Y-m-d") . ".csv");
echo array2csv($response);