Here I have created Sample that will help you out.
HTML
<div>
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="btnImport" runat="server" Text="Import" OnClick="ImportExcel" />
<hr />
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
</div>
C#
static string ComponentId = string.Empty;
protected void ImportExcel(object sender, EventArgs e)
{
string filePath = Server.MapPath("~/Files/") + Path.GetFileName(FileUpload1.PostedFile.FileName);
FileUpload1.SaveAs(filePath);
using (SpreadsheetDocument doc = SpreadsheetDocument.Open(filePath, false))
{
Sheet sheet = doc.WorkbookPart.Workbook.Sheets.GetFirstChild<Sheet>();
Worksheet worksheet = (doc.WorkbookPart.GetPartById(sheet.Id.Value) as WorksheetPart).Worksheet;
IEnumerable<Row> rows = worksheet.GetFirstChild<SheetData>().Descendants<Row>();
DataTable dt = new DataTable();
List<Component> components = new List<Component>();
foreach (Row row in rows)
{
if (row.RowIndex.Value == 1)
{
foreach (Cell cell in row.Descendants<Cell>())
{
dt.Columns.Add(GetValue(doc, cell));
}
}
else
{
dt.Rows.Add();
int i = 0;
string xml = row.InnerXml;
if (!xml.Contains("A"))
{
dt.Rows[dt.Rows.Count - 1][i] = string.Empty;
i++;
}
foreach (Cell cell in row.Descendants<Cell>())
{
if (GetColumnName(cell.CellReference) == "A")
{
ComponentId = GetValue(doc, cell);
}
dt.Rows[dt.Rows.Count - 1][i] = GetValue(doc, cell);
i++;
}
components.Add(new Component
{
ComponentId = dt.Rows[dt.Rows.Count - 1]["ComponentId"].ToString(),
ComponentFunctionality = dt.Rows[dt.Rows.Count - 1]["ComponentFunctionality"].ToString(),
DerivedResult = dt.Rows[dt.Rows.Count - 1]["DerivedResult"].ToString(),
ExpectedResult = dt.Rows[dt.Rows.Count - 1]["ExpectedResult"].ToString(),
ParentComponentId = ComponentId
});
}
}
var grpComp = from comp in components
group comp by comp.ParentComponentId into eGroup
select new
{
Key = eGroup.Key,
Components = eGroup
};
List<Component> resultComponent = new List<Component>();
foreach (var c in grpComp)
{
Component co = c.Components.Where(x => x.ComponentId == c.Key).FirstOrDefault();
co.ChildComponent = c.Components.Where(comp => comp.ParentComponentId == c.Key).Where(cm => cm.ComponentId == string.Empty).ToList<Component>();
resultComponent.Add(co);
}
// To Save into Database
InsertComponent(resultComponent);
//I have simply shown into Grid if you want you can bind to nested Grid
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
private void InsertComponent(List<Component> components)
{
foreach (Component comp in components)
{
//Write code to Insert into Parent Table
foreach (Component childComp in comp.ChildComponent)
{
//Write code to Insert into Child Table
}
}
}
public static string GetColumnName(string cellReference)
{
Regex regex = new Regex("[A-Za-z]+");
Match match = regex.Match(cellReference);
return match.Value;
}
private string GetValue(SpreadsheetDocument doc, Cell cell)
{
string value = cell.CellValue.InnerText;
if (cell.DataType != null && cell.DataType.Value == CellValues.SharedString)
{
return doc.WorkbookPart.SharedStringTablePart.SharedStringTable.ChildElements.GetItem(int.Parse(value)).InnerText;
}
return value;
}
Component Class
public class Component
{
public Component()
{
ChildComponent = new List<Component>();
}
public string ParentComponentId { get; set; }
public string ComponentId { get; set; }
public string ComponentFunctionality { get; set; }
public string ExpectedResult { get; set; }
public string DerivedResult { get; set; }
public List<Component> ChildComponent { get; set; }
}
Screenshot
1)

2)
