title: php fwrite to xls file blank cells not showing up
body: <p>I'm trying to write data that I've gotten from an SQL query to a tab-delimited text file. But there is a problem that if there is a blank cell it doesn't register. and just writes the next text in the blank cell.</p>
  
  <p>database data:</p>
  
  <pre><code>id    text    2ndText
  1     1t1     2t1
  2             2t2
  3     1t3     2t3
  </code></pre>
  
  <p>excel output:</p>
  
  <pre><code>id    text            2ndText
  1     1t1             2t1
  2     2t2
  3     1t3             2t3
  </code></pre>
  
  <p>the code we're using is this:</p>
  
  <pre><code>while ($row=mysql_fetch_row($Recordset1)) 
  { 
      $line=''; 
      $fc=0;
      for ($i=0;$i&lt;$fields;$i++)
      {
          if (substr(strtoupper(mysql_field_name($Recordset1,$i)),0,7)!="DBSTACK")
          {
              if (strtoupper($_POST['checkbox'.$fc])==strtoupper(mysql_field_name($Recordset1,$i)))
              {   
                  $value=$row[$i];
                  if ((!isset($value)) | ($value == "")) 
                  { 
                      $value = "\t"; 
                  } else 
                  { 
                      $value=str_replace('"','""',$value); 
                      $value='"'.$value.'"'."\t"; 
                  } 
                  $line.=$value;
              }
              $fc++;
          }
      } 
      $data.=trim($line)."\n"; 
  } 
  $data=str_replace("\r","",$data);
  $TargetFileName="temp/".session_id().time().".xls";
  $target=fopen($TargetFileName,"wb");
  $XLData=$header."\n".$data;
  fwrite($target,$XLData);
  fclose($target);
  </code></pre>
  
  <p>where <code>$Recordset1</code> is the result of the query</p>
  
  <p>In my search for a solution <a href="https://pear.php.net/manual/en/package.fileformats.spreadsheet-excel-writer.spreadsheet-excel-writer-worksheet.writeblank.php" rel="nofollow noreferrer">this</a> came out as a possible solution, but this is a frequently used piece of code so i don't want it to change too much. So is there a simpler solution?</p>
  
  <p>Changing the value to na\t if the cell is empty, could be a solution but that's kind of dirty.</p>
  
  <p>Any ideas?</p>