title: How to read excel file in asp.net
body: <p>I am using Epplus library in order to upload data from excel file.The code i am using is perfectly works for excel file which has standard form.ie if first row is column and rest all data corresponds to column.But now a days i am getting regularly , excel files which has different structure and i am not able to read
  excel file like as shown below</p>
  
  <p>what i want is on third row i wan only Region and Location Id and its values.Then 7th row is columns and 8th to 15 are its values.Finally 17th row is columns for 18th to 20th .How to load all these datas to seperate datatables
  code i used is as shown below
  I created an extension method</p>
  
  <pre><code> public static DataSet Exceltotable(this string path)
          {
              DataSet ds = null;
              using (var pck = new OfficeOpenXml.ExcelPackage())
              {
                  try
                  {
                      using (var stream = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                      {
                          pck.Load(stream);
                      }
                      ds = new DataSet();
                      var wss = pck.Workbook.Worksheets;
                      ////////////////////////////////////
                      //Application app = new Application();
                      //app.Visible = true;
                      //app.Workbooks.Add("");
                      //app.Workbooks.Add(@"c:\MyWork\WorkBook1.xls");
                      //app.Workbooks.Add(@"c:\MyWork\WorkBook2.xls");
                      //for (int i = 2; i &lt;= app.Workbooks.Count; i++)
                      //{
                      //    for (int j = 1; j &lt;= app.Workbooks[i].Worksheets.Count; j++)
                      //    {
                      //        Worksheet ws = app.Workbooks[i].Worksheets[j];
                      //        ws.Copy(app.Workbooks[1].Worksheets[1]);
                      //    }
                      //}
  
                      ///////////////////////////////////////////////////
  
                      //for(int s=0;s&lt;5;s++)
                      //{
                      foreach (var ws in wss)
                      {
                          System.Data.DataTable tbl = new System.Data.DataTable();
                          bool hasHeader = true; // adjust it accordingly( i've mentioned that this is a simple approach)
                          string ErrorMessage = string.Empty;
                          foreach (var firstRowCell in ws.Cells[1, 1, 1, ws.Dimension.End.Column])
                          {
  
                              tbl.Columns.Add(hasHeader ? firstRowCell.Text : string.Format("Column {0}", firstRowCell.Start.Column));
  
                          }
                          var startRow = hasHeader ? 2 : 1;
                          for (var rowNum = startRow; rowNum &lt;= ws.Dimension.End.Row; rowNum++)
                          {
                              var wsRow = ws.Cells[rowNum, 1, rowNum, ws.Dimension.End.Column];
                              var row = tbl.NewRow();
                              foreach (var cell in wsRow)
                              {
                                  //modifed by faras
                                  if (cell.Text != null)
                                  {
                                      row[cell.Start.Column - 1] = cell.Text;
                                  }
                              }
                              tbl.Rows.Add(row);
                              tbl.TableName = ws.Name;
                          }
                          DataTable dt = RemoveEmptyRows(tbl);
                          ds.Tables.Add(dt);
                      }
                  }
                  catch (Exception exp)
                  {
  
  
                  }
                  return ds;
              }
          }
  </code></pre>