title: Problem downloading data to Excel using PHP "pack" function
body: <p>I have a PHP application that downloads MySQL table data to a spreadsheet on the client machine. I found some code in numerous places on the web that works fine for Open Office Scalc on a machine running Redhat. The problem I ran into was when I tried to download to an MS Excel on a Windows PC rather than to an Open Office spreadsheet. The problem seems to be associated with the length of the string inserted into a cell. If it is too long, Excel thinks the file is corrupt and won't load it. "Too long" seems to be about 255 characters, even though the MS Excel specs say 32,000 is the maximum length. </p>
  
  <p>To investigate the problem further, I tried downloading the same data as a tab-separated values file and let Excel convert it to a spreadsheet. Using that method, there was no problem loading very long strings into the spreadsheet cells. So strings much longer than 255 characters can in fact be inserted into Excel cells, but just not with the code I am using, even though that code works with Open Office. Using tsv files would not solve our problem, because the long strings have carriage returns that we want to retain, and carriage returns are interpreted as row separators when a tsv file is loaded into a spreadsheet.</p>
  
  <p>The PHP function that writes a string to a spreadsheet cell is:</p>
  
  <pre><code> function xlsWriteLabel($Row, $Col, $Value ) {
       $L = strlen($Value);
       echo pack("ssssss", 0x204, 8 + $L, $Row, $Col, 0x0, $L);
       echo $Value;
       return;
   }
  </code></pre>
  
  <p>The other necessary code for transfers to spreadsheets in addition to the above function  can be found at:</p>
  
  <p><a href="http://www.appservnetwork.com/modules.php?name=News&amp;file=article&amp;sid=8" rel="nofollow">http://www.appservnetwork.com/modules.php?name=News&amp;file=article&amp;sid=8</a></p>
  
  <p>I haven't found any explanations as to the meanings of the various arguments passed to the "pack" function in the above code, and I'm wondering if changing one the arguments in the function above could solve the problem. </p>
  
  <p>So, if anyone has a solution to this problem, I'd be interested in hearing it.</p>