In CSV file the first row contains the Headers and other rows contains rest of the data.
Let’s start
- Add a web page ReadCSV.aspx after creating new website.
- Add a File upload control to choose file.
- Add a Submit button read the selected file.
- Add a grid view to see the CSV file data.
ReadCSV.aspx
- <%@ Page Title="" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="ReadCSV.aspx.cs" Inherits="ReadCSV" %>
-
- <asp:Content ID="Content1" ContentPlaceHolderID="MainContent" Runat="Server">
- <asp:Panel ID="PanelMain" runat="server">
- <br />
- <table style="width: 44%;">
- <tr>
- <td style="width: 84px">Select File:</td>
- <td style="width: 231px">
- <asp:FileUpload ID="FileUpload1" runat="server" />
- </td>
- <td>
- <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="* required" ControlToValidate="FileUpload1" ForeColor="Red" ValidationGroup="validation">
- </asp:RequiredFieldValidator>
- </td>
- </tr>
- <tr>
- <td style="width: 84px"> </td>
- <td style="width: 231px"> </td>
- </tr>
- <tr>
- <td colspan="2">
- <asp:Button ID="btnUpload" runat="server" Text="Upload" ValidationGroup="validation" OnClick="btnUpload_Click" />
- </td>
- </tr>
- </table>
- <br />
- <asp:GridView ID="GridView1" runat="server"></asp:GridView>
- <br />
- <asp:Label ID="lblerror" runat="server" />
- </asp:Panel>
- </asp:Content>
ReadCSV.aspx.cs
- using System;
- using System.Collections.Generic;
- using System.Data;
- using System.IO;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
-
- public partial class ReadCSV: System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
-
- }
-
- protected void btnUpload_Click(object sender, EventArgs e)
- {
- try
- {
- DataTable dt = new DataTable();
- dt = ReadCsvFile();
- GridView1.DataSource = dt;
- GridView1.DataBind();
- }
- catch (Exception ex)
- {
- lblerror.Text = ex.Message;
- }
- }
- public DataTable ReadCsvFile()
- {
-
- DataTable dtCsv = new DataTable();
- string Fulltext;
- if (FileUpload1.HasFile)
- {
- string FileSaveWithPath = Server.MapPath("\\Files\\Import" + System.DateTime.Now.ToString("ddMMyyyy_hhmmss") + ".csv");
- FileUpload1.SaveAs(FileSaveWithPath);
- using(StreamReader sr = new StreamReader(FileSaveWithPath))
- {
- while (!sr.EndOfStream)
- {
- Fulltext = sr.ReadToEnd().ToString();
- string[] rows = Fulltext.Split('\n');
- for (int i = 0; i < rows.Count() - 1; i++)
- {
- string[] rowValues = rows[i].Split(',');
- {
- if (i == 0)
- {
- for (int j = 0; j < rowValues.Count(); j++)
- {
- dtCsv.Columns.Add(rowValues[j]);
- }
- }
- else
- {
- DataRow dr = dtCsv.NewRow();
- for (int k = 0; k < rowValues.Count(); k++)
- {
- dr[k] = rowValues[k].ToString();
- }
- dtCsv.Rows.Add(dr);
- }
- }
- }
- }
- }
- }
- return dtCsv;
- }
- }
Let’s discuss the ReadCsvFile() method.
First of all set the location to save the file that is selected by file upload control. I have created a folder named Files to save files.
Using the stream reader read the whole file into a string FullText.
Now split the FullText with ‘\n’ to get the rows (row wise data).
Now split the rows with comma (,) to get the individual values.
After getting individual values add these values into data table by using loops.
Now assign the data table to grid view to see the data.