Hi indradeo,
Refer below code.
HTML
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="btnImport" runat="server" Text="Import" OnClick="ImportCSV" />
<hr />
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
Code
C#
protected void ImportCSV(object sender, EventArgs e)
{
string csvPath = Server.MapPath("~/Files/") + Path.GetFileName(FileUpload1.PostedFile.FileName);
FileUpload1.SaveAs(csvPath);
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[]
{
new DataColumn("Col1", typeof(string)),
new DataColumn("Col2", typeof(string)),
new DataColumn("Col3", typeof(string)),
new DataColumn("Col4", typeof(string)),
new DataColumn("Col5", typeof(string)),
new DataColumn("Col6", typeof(string)),
new DataColumn("Col7", typeof(string)),
new DataColumn("Col8", typeof(string)),
new DataColumn("Col9", typeof(string)),
new DataColumn("Col10", typeof(string)),
new DataColumn("Col11", typeof(string)),
new DataColumn("Col12", typeof(string)),
new DataColumn("Col13", typeof(string)),
new DataColumn("Col14", typeof(string))
});
string csvData = File.ReadAllText(csvPath);
foreach (string row in csvData.Split('\n'))
{
if (!string.IsNullOrEmpty(row))
{
dt.Rows.Add();
int i = 0;
foreach (string cell in row.Split(';'))
{
dt.Rows[dt.Rows.Count - 1][i] = cell;
i++;
}
}
}
GridView1.DataSource = dt;
GridView1.DataBind();
}
VB.Net
Protected Sub ImportCSV(ByVal sender As Object, ByVal e As EventArgs)
Dim csvPath As String = Server.MapPath("~/Files/") + Path.GetFileName(FileUpload1.PostedFile.FileName)
FileUpload1.SaveAs(csvPath)
Dim dt As DataTable = New DataTable()
dt.Columns.AddRange(New DataColumn() {New DataColumn("Col1", GetType(String)), New DataColumn("Col2", GetType(String)), New DataColumn("Col3", GetType(String)), New DataColumn("Col4", GetType(String)), New DataColumn("Col5", GetType(String)), New DataColumn("Col6", GetType(String)), New DataColumn("Col7", GetType(String)), New DataColumn("Col8", GetType(String)), New DataColumn("Col9", GetType(String)), New DataColumn("Col10", GetType(String)), New DataColumn("Col11", GetType(String)), New DataColumn("Col12", GetType(String)), New DataColumn("Col13", GetType(String)), New DataColumn("Col14", GetType(String))})
Dim csvData As String = File.ReadAllText(csvPath)
For Each row As String In csvData.Split(vbLf)
If Not String.IsNullOrEmpty(row) Then
dt.Rows.Add()
Dim i As Integer = 0
For Each cell As String In row.Split(";"c)
dt.Rows(dt.Rows.Count - 1)(i) = cell
i += 1
Next
End If
Next
GridView1.DataSource = dt
GridView1.DataBind()
End Sub