title: .xlsx Created and Saved Using Template with EPPlus is Unreadable/Corrupt
body: <h1>The Problem</h1>
  
  <p>I'm trying to create a small program that uses an <em>Excel template</em> to create an <em>Excel document</em> and then writes to several cells using <a href="https://epplus.codeplex.com/" rel="noreferrer"><em>EPPlus</em></a>. Unfortunately, the files appear to be corrupt no matter what I try.</p>
  
  <h2>My code:</h2>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using OfficeOpenXml;
  using System.IO;
  
  namespace ConsoleApplication9
  {
      public sealed class ExcelSerialize
      {
          private readonly List&lt;Tuple&lt;string, string&gt;&gt; Results;
          private readonly string Directory;
          private ExcelPackage package;
  
          public ExcelSerialize(List&lt;Tuple&lt;string, string&gt;&gt; Results, string Directory)
          {
              this.Results = Results;
              this.Directory = Directory;
          }
  
          public bool WriteResults()
          {
              FileInfo template = new FileInfo(Directory);
  
              using (package = new ExcelPackage(template, true))
              {
                  ExcelWorksheet worksheet = package.Workbook.Worksheets[1];
  
                  //foreach (Tuple&lt;string, string&gt; Result in Results)
                  //{
                  //    worksheet.Cells[Result.Item1].Value = Result.Item2;
                  //}
  
                  string file = string.Format(System.AppDomain.CurrentDomain.BaseDirectory.ToString() + @"results\results" + System.DateTime.Now.ToString().Replace(" ", "").Replace("/", "_").Replace(":", "-") + ".xlsx");
  
                  Byte[] bin = package.GetAsByteArray();
                  File.WriteAllBytes(file, bin);
  
                  return true;
              }
          }
      }
  }
  </code></pre>
  
  <h2>Things I've tried:</h2>
  
  <ul>
  <li>Changing the values of various cells in the template.</li>
  <li>Saving an <em>Excel</em> document created from the template without writing any new data to it.</li>
  <li>Creating a basic template with the cells A1, A2, and A3 containing "TEST" and no other edits instead of the more complicated template I intend to use.</li>
  <li>Saving using <code>Package.SaveAs()</code>.</li>
  <li>Saving using the Byte array seen in the example code.</li>
  <li>Compiling EPPlus from the latest source provided on Codeplex.</li>
  </ul>
  
  <h2>Things that work:</h2>
  
  <ul>
  <li>Creating a new file using the following code:</li>
  </ul>
  
  <p></p>
  
  <pre><code>using (package = new ExcelPackage(string.Format(System.AppDomain.CurrentDomain.BaseDirectory.ToString() + @"results\results" + System.DateTime.Now.ToString().Replace(" ", "").Replace("/", "_").Replace(":", "-") + ".xlsx"))
  {
      ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("Test");
      worksheet.Cells[A1].Value = "Test";
      package.Save();
  }
  </code></pre>
  
  <h2>Notes:</h2>
  
  <p>For whatever reason, the files saved still appear corrupt and can't be recovered. I'm currently using <em>Microsoft Office 2010</em>. The file formats I'm using are <code>.xltx</code> and <code>.xlsx</code>.</p>