Hi beniwalsan,
Refer the below sample as per your provided data.
Excel Data
ID |
SeoReportKeyWord |
SeoReportPostion |
1 |
airbrush makeup parlour in gurgaon |
1 |
2 |
airbrush makeup parlor in gurgaon |
1 |
3 |
engagement makeup parlour in gurgaon |
1 |
4 |
airbrush makeup gurgaon |
1 |
5 |
makeup parlour in gurgaon |
1 |
6 |
airbrush makeup in gurgaon |
1 |
7 |
wedding makeup studio in gurgaon |
1 |
8 |
airbrush bridal makeup in gurgaon |
2 |
9 |
engagement beauty studio in gurgaon |
1 |
10 |
pre-bridal makeup parlour in gurgaon |
1 |
11 |
makeup and beauty parlour in gurgaon |
1 |
12 |
bridal makeup parlour in gurgaon |
1 |
13 |
bridal makeup and parlour in gurgaon |
1 |
14 |
best bridal makeup parlour in gurgaon |
1 |
15 |
makeup and beauty salon in gurgaon |
1 |
16 |
best makeup and beauty parlour in gurgaon |
1 |
17 |
airbrush makeup studio in gurgaon |
2 |
18 |
wedding beauty parlour in gurgaon |
2 |
19 |
best makeup parlour in gurgaon |
2 |
20 |
hair colour salon in gurgaon |
2 |
21 |
bridal makeup salon in gurgaon |
3 |
22 |
bridal makeup studio in gurgaon |
3 |
23 |
engagement makuep in gurgaon |
3 |
HTML
<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>
C#
protected void ImportExcel(object sender, EventArgs e)
{
//Save the uploaded Excel file.
string filePath = Server.MapPath("~/Files/")+ Path.GetFileName(FileUpload1.PostedFile.FileName);
FileUpload1.SaveAs(filePath);
//Open the Excel file in Read Mode using OpenXml.
using (SpreadsheetDocument doc = SpreadsheetDocument.Open(filePath, false))
{
//Read the first Sheets from Excel file.
Sheet sheet = doc.WorkbookPart.Workbook.Sheets.GetFirstChild<Sheet>();
//Get the Worksheet instance.
Worksheet worksheet = (doc.WorkbookPart.GetPartById(sheet.Id.Value) as WorksheetPart).Worksheet;
//Fetch all the rows present in the Worksheet.
IEnumerable<Row> rows = worksheet.GetFirstChild<SheetData>().Descendants<Row>();
//Create a new DataTable.
DataTable dt = new DataTable();
//Loop through the Worksheet rows.
foreach (Row row in rows)
{
//Use the first row to add columns to DataTable
if (row.RowIndex.Value == 1)
{
foreach (Cell cell in row.Descendants<Cell>())
{
dt.Columns.Add(GetValue(doc, cell));
}
}
else
{
//Add rows to DataTable.
dt.Rows.Add();
int i = 0;
foreach (Cell cell in row.Descendants<Cell>())
{
dt.Rows[dt.Rows.Count - 1][i] = GetValue(doc, cell);
i++;
}
}
}
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
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;
}
VB.Net
Protected Sub ImportExcel(sender As Object, e As EventArgs)
'Save the uploaded Excel file.
Dim filePath As String = Server.MapPath("~/Files/") + Path.GetFileName(FileUpload1.PostedFile.FileName)
FileUpload1.SaveAs(filePath)
'Open the Excel file in Read Mode using OpenXml.
Using doc As SpreadsheetDocument = SpreadsheetDocument.Open(filePath, False)
'Read the first Sheets from Excel file.
Dim sheet As Sheet = doc.WorkbookPart.Workbook.Sheets.GetFirstChild(Of Sheet)()
'Get the Worksheet instance.
Dim worksheet As Worksheet = TryCast(doc.WorkbookPart.GetPartById(sheet.Id.Value), WorksheetPart).Worksheet
'Fetch all the rows present in the Worksheet.
Dim rows As IEnumerable(Of Row) = worksheet.GetFirstChild(Of SheetData)().Descendants(Of Row)()
'Create a new DataTable.
Dim dt As New DataTable()
'Loop through the Worksheet rows.
For Each row As Row In rows
'Use the first row to add columns to DataTable
If row.RowIndex.Value = 1 Then
For Each cell As Cell In row.Descendants(Of Cell)()
dt.Columns.Add(GetValue(doc, cell))
Next
Else
'Add rows to DataTable.
dt.Rows.Add()
Dim i As Integer = 0
For Each cell As Cell In row.Descendants(Of Cell)()
dt.Rows(dt.Rows.Count - 1)(i) = GetValue(doc, cell)
i += 1
Next
End If
Next
GridView1.DataSource = dt
GridView1.DataBind()
End Using
End Sub
Private Function GetValue(doc As SpreadsheetDocument, cell As Cell) As String
Dim value As String = cell.CellValue.InnerText
If cell.DataType IsNot Nothing AndAlso cell.DataType.Value = CellValues.SharedString Then
Return doc.WorkbookPart.SharedStringTablePart.SharedStringTable.ChildElements.GetItem(Integer.Parse(value)).InnerText
End If
Return value
End Function
Screenshot