0

I have a mySQL database. I am attempting to download one of the tables to Excel.

I am using the basic download PHP:

header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=file.xls");

I am then using echo for each field in each row to generate the Excel spreadsheet.

The issue I encountered is that some fields were typed incorrectly, they have their first character as a double quote, with no closing double quote.

This is causing a failure in my code:

echo $row['type'] . "\t";

As the Excel spreadsheet now merges this cell into all the next--I assume because it sees the opening quote as a beginning quote and doesn't properly end the echo statement. If I escape the quote, the slash shows in Excel. What's the fix here?

If the double quote is anywhere else but the first character, I don't have this issue (even a single closing quote).

1 Answers1

0

You could just trim it in php:

echo trim($row['type'], '"') . "\t";

It will delete any leading and trailing " off the string.

Francisco Presencia
  • 8,732
  • 6
  • 46
  • 90