问题
I'm trying to input some data into a csv file and it works nicely, but if I try to add the header of the table of data Excel won't let me open the file, because "the file format and extension of file.csv don't match. the file could be corrupted or unsafe".
Here's the code:
//crate headers
$headers[] = "ID";
$headers[] = "Name";
$headers[] = "Ref";
$headers[] = "Quantity";
// crete and open file
$csvName = "file.csv";
$fileHandle = fopen($csvName, 'w') or die('Can\'t create .csv file, try again later.');
//Add the headers, if I take this line out the excel allows me to open the file
fputcsv($fileHandle,$headers,";");
//Add the data
for($i = 0; $i < count($info); ++$i) {
fputcsv($fileHandle,$info[$i],";");
}
//close file
fclose($fileHandle);
EDIT:
Here are the first lines of my csv open with notepad:
ID;Name;Ref;Quantity
2;"Blouse - Color : White, Size : M";demo_2;6
3;"Printed Dress - Color : Orange, Size : S";demo_3;4
回答1:
If you're intending to use this to create a CSV file that can be opened normally with Excel, the headers shouldn't need to be wrapped in double quotes (because they don't contain any separator characters), and you should use commas rather than semicolons for the separators. However, if you make those changes, you'll still get the same error message when you try to open the resulting file with Excel. Surprisingly, this is because your headers start with 'ID'.
If you can use a different name for that first column header, it could simplify things a bit.
$headers = ["ItemID", "Name", "Ref", "Quantity"];
$csvName = "file.csv";
$fileHandle = fopen($csvName, 'w') or die('Can\'t create .csv file, try again later.');
//Add the headers
fputcsv($fileHandle, $headers);
//Add the data
foreach ($info as $item) {
fputcsv($fileHandle, $item);
}
//close file
fclose($fileHandle);
This should create a .csv file that will open in Excel with no errors.
回答2:
Try to change your headers to this:
$headers[] = '"ID"';
$headers[] = '"Name"';
$headers[] = '"Ref"';
$headers[] = '"Quantity"';
This will wrap the strings in double quotes, which should fix the syntax issues you are experiencing.
回答3:
Excel is picky. PHP by default uses commas, which is actually what you want to use for Excel. Semicolons will often work but are not the default - remember, CSV stands for Comma Separated Values. The second problem is specific to Excel. Even though it handles strings without quotes just fine in the data section, for some reason it doesn't work well on the header line. I don't see an option in fputcsv to force quotes, so you need to hardcode them. Something like:
// create and open file
$csvName = "file.csv";
$fileHandle = fopen($csvName, 'w') or die("Can't create .csv file, try again later.");
//Add the headers - note that \r\n or \n is OS dependent
fwrite($fileHandle,'"ID","Name","Ref","Quantity"' . "\r\n");
//Add the data
for ($i = 0; $i < count($info); ++$i) {
fputcsv($fileHandle,$info[$i]);
}
//close file
fclose($fileHandle);
来源:https://stackoverflow.com/questions/45489088/add-heading-on-csv-using-fputcsv-php