title: Regarding Formatting the Excel sheet using EPPlus in C#
body: <p>I have been working on Excel Sheet Formatting using EPPlus. I am able to create group when the Excel Sheet is in a ordered one .But if the input comes unordered and I have to group then I am facing issue.</p>
  
  <p>The Excel input looks like in the problematic sceario:</p>
  
  <p>Field1           Field2    Field3</p>
  
  <p>1                 XYZ       ABC</p>
  
  <p>3.1               PRQ       SDE</p>
  
  <p>1.1               AB        ST</p>
  
  <p>1.2               MN        RT</p>
  
  <p>like wise and all 1 related row should be at one group like in ascending order.</p>
  
  <p>I have written the below which if sorted works fine but not able to figure out if the input is jumbled up/ not sorted.</p>
  
  <p>Here is my code. </p>
  
  <p>Thanks for your help.</p>
  
  <pre><code>namespace EPPlusTestDemo {
      class Program
      {
          static void Main(string[] args)
          {
              // Set the File name and get the output directory
  
              var fileName = "Example-File-" + DateTime.Now.ToString("yyyy-MM-dd--hh-mm-ss") + ".xlsx";
              var outputDir = @"C:\Users\asder\Desktop\outpath\" + fileName;
  
              // Create the file using the FileInfo object
              var file = new FileInfo(outputDir);
  
              using (var package = new ExcelPackage(file))
              {
                  // Adding a new worksheet to the empty workbook
                  ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("Example list - " + DateTime.Now.ToShortDateString());
  
                  // --------- Data and styling goes here -------------- //
  
                  // Header part
  
                  worksheet.Cells["A1"].Value = "Field1";
                  worksheet.Cells["B1"].Value = "Field2";
                  worksheet.Cells["C1"].Value = "Field3";
                  worksheet.Cells["D1"].Value = "Field4";
                  worksheet.Cells["E1"].Value = "Field5";
  
                  worksheet.Cells["A1:E1"].Style.Font.Bold = true;
  
                  // The row number 
                  int initialRowNum = 2;
                  int rowNumber = initialRowNum;
  
                  #region input_toExcel
  
                  // Filling the data from the 2nd row of the excel sheet
  
                  worksheet.Cells[rowNumber, 1].Value = "1";
                  worksheet.Cells[rowNumber, 2].Value = "GRP";
                  worksheet.Cells[rowNumber, 3].Value = "ABC";
                  worksheet.Cells[rowNumber, 4].Value = "CDE";
                  worksheet.Cells[rowNumber, 5].Value = "ABCD";
  
                  rowNumber++;
  
                  worksheet.Cells[rowNumber, 1].Value = "1.1";
                  worksheet.Cells[rowNumber, 2].Value = "Item";
                  worksheet.Cells[rowNumber, 3].Value = "ABC";
                  worksheet.Cells[rowNumber, 4].Value = "CDE";
                  worksheet.Cells[rowNumber, 5].Value = "ABCD";
  
  
                  rowNumber++;
  
                  worksheet.Cells[rowNumber, 1].Value = "1.2";
                  worksheet.Cells[rowNumber, 2].Value = "Item";
                  worksheet.Cells[rowNumber, 3].Value = "ABC";
                  worksheet.Cells[rowNumber, 4].Value = "CDE";
                  worksheet.Cells[rowNumber, 5].Value = "ABCD";
  
                  rowNumber++;
  
                  worksheet.Cells[rowNumber, 1].Value = "1.3";
                  worksheet.Cells[rowNumber, 2].Value = "Item";
                  worksheet.Cells[rowNumber, 3].Value = "ABC";
                  worksheet.Cells[rowNumber, 4].Value = "CDE";
                  worksheet.Cells[rowNumber, 5].Value = "ABCD";
  
  
                  rowNumber++;
  
                  worksheet.Cells[rowNumber, 1].Value = "1.4";
                  worksheet.Cells[rowNumber, 2].Value = "Item";
                  worksheet.Cells[rowNumber, 3].Value = "ABC";
                  worksheet.Cells[rowNumber, 4].Value = "CDE";
                  worksheet.Cells[rowNumber, 5].Value = "ABCD";
  
  
                  rowNumber++;
  
                  worksheet.Cells[rowNumber, 1].Value = "2";
                  worksheet.Cells[rowNumber, 2].Value = "GRP";
                  worksheet.Cells[rowNumber, 3].Value = "ABC";
                  worksheet.Cells[rowNumber, 4].Value = "CDE";
                  worksheet.Cells[rowNumber, 5].Value = "ABCD";
  
                  rowNumber++;
  
                  worksheet.Cells[rowNumber, 1].Value = "2.1";
                  worksheet.Cells[rowNumber, 2].Value = "Item";
                  worksheet.Cells[rowNumber, 3].Value = "ABC";
                  worksheet.Cells[rowNumber, 4].Value = "CDE";
                  worksheet.Cells[rowNumber, 5].Value = "ABCD";
  
                  rowNumber++;
  
                  worksheet.Cells[rowNumber, 1].Value = "2.2";
                  worksheet.Cells[rowNumber, 2].Value = "Item";
                  worksheet.Cells[rowNumber, 3].Value = "ABC";
                  worksheet.Cells[rowNumber, 4].Value = "CDE";
                  worksheet.Cells[rowNumber, 5].Value = "ABCD";
  
  
                  rowNumber++;
  
                  worksheet.Cells[rowNumber, 1].Value = "2.3";
                  worksheet.Cells[rowNumber, 2].Value = "Item";
                  worksheet.Cells[rowNumber, 3].Value = "ABC";
                  worksheet.Cells[rowNumber, 4].Value = "CDE";
                  worksheet.Cells[rowNumber, 5].Value = "ABCD";
  
  
                  rowNumber++;
  
                  worksheet.Cells[rowNumber, 1].Value = "2.4";
                  worksheet.Cells[rowNumber, 2].Value = "Item";
                  worksheet.Cells[rowNumber, 3].Value = "ABC";
                  worksheet.Cells[rowNumber, 4].Value = "CDE";
                  worksheet.Cells[rowNumber, 5].Value = "ABCD";
  
                  #endregion
  
  
                  // Grouping after comparing the Cell Value 
                  for ( var rowIndex = initialRowNum; worksheet.Row(rowIndex) != null; rowIndex++)
                  {
                      // Checking for NULL in the Excel Sheet
                      var comapreable = worksheet.Cells[rowIndex, 1].GetValue&lt;string&gt;();
  
                      // Group Counter 
                      int count = 0;
  
                      if (comapreable != null)
                      {
                          Regex regexIntegral = new Regex(@"\d");
                          Match matchIntegral = regexIntegral.Match(comapreable);
  
                          Regex regexDouble = new Regex(@"([1-9]+)\.([1-9]+)");
                          Match matchDouble = regexDouble.Match(comapreable);
  
  
                          // Checking if the Cell of the Excel sheet contains any integral value
                          if ( matchIntegral.Success == true )
                          {
                              count++; // Incrementing the Group Level Counter
  
                              // Checking if the Cell contains the decimal values
  
                              if ( matchDouble.Success == true )
                              {
                                  worksheet.Row(rowIndex).OutlineLevel = count;
                                  worksheet.Row(rowIndex).Collapsed = true;
                              }
                          }
                      }
                      else
                      {
                          break;
                      }
                  }
                  // Saving the File
                  package.Save();
              }
              // Console.ReadKey();
          }
      } }
  </code></pre>