title: NodeJS generate excel file from binary data, can not be opened in Excel
body: <p>I am lost in this solution.
  1. Excel file is saved in server;
  2. Using Java API to read the excel file as binary data. Here is the function:</p>
  
  <pre><code>@Override
  public byte[] retrieveExportedFileBinaryContent(String recordId, String userId) throws IOException, NoExportRecordException, InValidDownloadPermissionException{
      CaseExportRecord caseExportRecord = caseExportRecordRepository.findById(recordId);
      if( caseExportRecord!=null ){
  
          CaseExportConfig caseExportConfig = caseExportConfigRepository.findByCaseExportRecords(caseExportRecord);
  
          if(caseExportConfig.getUserId().equalsIgnoreCase(userId)){
              String fileName = generateExportedFileNameWithPath(caseExportRecord);
              Path path = Paths.get(fileName);
               if( path!=null ){
                   return Files.readAllBytes(path);
               }else{
                   throw new NoExportRecordException("Case Export Record File Does Not Exist");
               }
          } else {
              throw new InValidDownloadPermissionException("You do not have permission to download this file.");
          }
  
  
      }else{
          throw new NoExportRecordException("Case Export Record Id Not Exist");
      }
  }
  </code></pre>
  
  <ol start="3">
  <li><p>using NodeJS to obtain the binary data and create an excel file:</p>
  
  <pre><code>var fs = require('fs');
      var wstream = fs.createWriteStream('my.xls');
      wstream.write(fileData);
      wstream.end();
  </code></pre></li>
  </ol>
  
  <p>Here fileData is the binary data returned by Java API.</p>
  
  <p>But now, when I use Excel to open this xls file, it is no longer the good excel and only raw binary data is displayed in excel.</p>
  
  <p>How can I create a good excel file from binary data?</p>
  
  <p>Thanks</p>