title: create multiple excels with multiple sheet using ExcelLibrary in C#
body: <p><a href="https://i.stack.imgur.com/iyu44.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/iyu44.png" alt="enter image description here"></a></p>
  
  <p>I want to export the data in multiple sheets with multiple excel files. As you can see in my image I want to generate sheets as <code>ID</code> changes and when <code>ModalityId</code> changes I want to create new excel.</p>
  
  <p>I have made code like this for this concern:</p>
  
  <pre><code>List&lt;string&gt; lstFile = new List&lt;string&gt;();
  if (dtAltTag != null &amp;&amp; dtAltTag.Rows.Count &gt; 0)
  {
      string filePath = string.Empty;
      string fileName = "";
  
      Excel.Workbook workBook = new Excel.Workbook();
      var workSheet = new Excel.Worksheet(fileName);
  
      if (dtAltTag.Rows[0]["OldFormCode"] != null &amp;&amp; dtAltTag.Rows[0]["OldFormCode"].ToString() != "")
      {
          fileName = dtAltTag.Rows[0]["OldFormCode"].ToString();
      }
      else
      {
          fileName = dtAltTag.Rows[0]["Code"].ToString();
      }
      workSheet.Name = fileName;
      AddValue(0, workSheet, dtAltTag); // function is used to add the value in the sheet
      workBook.Worksheets.Add(workSheet);
  
      //data working
      for (int i = 0; i &lt; dtAltTag.Rows.Count; i++)
      {
          //for modality changes and first entery 
          //if (i == 0 || dtAltTag.Rows[i]["ModalityId"] != dtAltTag.Rows[i - 1]["ModalityId"])
          //{
          //if form changes then it should be in other sheet
          if (i != 0 &amp;&amp; dtAltTag.Rows[i]["ID"].ToString() != dtAltTag.Rows[i - 1]["ID"].ToString())
          {
              if (dtAltTag.Rows[i]["OldFormCode"] != null &amp;&amp; dtAltTag.Rows[i]["OldFormCode"].ToString() != "")
              {
                  fileName = dtAltTag.Rows[i]["OldFormCode"].ToString();
              }
              else
              {
                  fileName = dtAltTag.Rows[i]["Code"].ToString();
              }
              var workSheet1 = new Excel.Worksheet(fileName);
  
              AddValue(i, workSheet1, dtAltTag);
          }
          else
          {
  
          }
      }
  
      filePath = HostingEnvironment.MapPath(ConfigurationManager.AppSettings["ExcelFilesFilePath"]) + "Allitem";
      if (!File.Exists(filePath))
          Directory.CreateDirectory(filePath);
      filePath = filePath + "\\" + fileName + "_" + DateTime.Now.ToString("MM_dd_yyy_HH_mm_ss") + ".xls";
      workBook.Save(filePath);
      lstFile.Add(filePath);
  }
  return lstFile;
  </code></pre>
  
  <p>When the id is changed I append new header but after that, I want to continue export data till the id change can't detect how to do this?
  How can I get the current sheet where I continue adding the value in else case?</p>
  
  <p>I hope you are cleared of what I am trying to do!</p>
  
  <p>Thanks in advance!</p>