Upload and Read Csv File in Net Core
Introduction
In this commodity, I'll show you, with an case, a way to upload, read/browse and show CSV file (Text File) information in ASP.NET GridView using C# and VB.NET.
CSV file is a computer file that contains Comma Separated (Comma Delimited) Values. The information from a CSV file is browsed and and then once the values are separated, a DataTable is created which is able to populate the ASP.NET GridView control. If yous want to export datatable to CSV file you tin can read this article, i non re-posting the same article on c# corner to avoid duplicate content.
HTML Markup
The following HTML Markup consists of an acquaintance ASP.Net FileUpload control, a Push, and a GridView.
<%@ Page Linguistic communication="C#" AutoEventWireup="true" CodeFile="CS.aspx.cs" Inherits="CS" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://world wide web.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <caput runat="server"> <title></title> <style blazon="text/css"> body { font-family: Arial; font-size: 10pt; } table { border: 1px solid #ccc; border-collapse: collapse; background-color: #fff; } table th { background-colour: #ff7f00; colour: #fff; font-weight: assuming; } table thursday, tabular array td { padding: 5px; border: 1px solid #ccc; } table, table table td { border: 0px solid #ccc; } .push button { background-color: #0094ff; /* Blueish */ border: none; colour: white; padding: 15px 32px; text-align: center; text-decoration: none; display: inline-block; font-size: 16px; } </manner> </head> <torso> <form id="form1" runat="server"> <asp:FileUpload ID="FileUpload1" CssClass="button" runat="server" /> <asp:Push button ID="btnImport" CssClass="button" runat="server" Text="Import" OnClick="ImportCSV" /> <hr /> <asp:GridView ID="GridView1" runat="server"> </asp:GridView> </form> </trunk> </html> Namespaces
For reading the text from CSV file, you need to import the following namespace.
C#
using Organisation.IO; using System.Information; VB.Internet
Imports Organisation.IO Imports System.Data Upload, Read/scan and evidence CSV file (Text file) information in ASP.Internet GridView
When the Import button is clicked, the CSV file is kickoff uploaded and so saved within a folder named Files. The CSV file information is browsed into a Cord variable using the File class ReadAllText method.
A DataTable is formed with columns same as that of the destination database table so the CSV file information is split using the new line (\due north) and Comma (,) characters. Employing a loop, the information is saved into the DataTable.
Finally, the DataTable is certain to the GridView command.
C#
using System; using System.Collections.Generic; using System.Linq; using Organization.Spider web; using Organization.Web.UI; using System.Web.UI.WebControls; using System.IO; using System.Data; public partial grade CS : Organization.Web.UI.Page { protected void ImportCSV(object sender, EventArgs e) { //Upload and save the file string csvPath = Server.MapPath("~/Files/") + Path.GetFileName(FileUpload1.PostedFile.FileName); FileUpload1.SaveAs(csvPath); //Create a DataTable. DataTable dt = new DataTable(); dt.Columns.AddRange(new DataColumn[5] { new DataColumn("Id", typeof(int)), new DataColumn("Name", typeof(cord)), new DataColumn("Engineering", typeof(cord)), new DataColumn("Company", typeof(string)), new DataColumn("Country",typeof(string)) }); //Read the contents of CSV file. string csvData = File.ReadAllText(csvPath); //Execute a loop over the rows. foreach (cord row in csvData.Split('\north')) { if (!cord.IsNullOrEmpty(row)) { dt.Rows.Add(); int i = 0; //Execute a loop over the columns. foreach (string cell in row.Split(',')) { dt.Rows[dt.Rows.Count - 1][i] = cell; i++; } } } //Demark the DataTable. GridView1.DataSource = dt; GridView1.DataBind(); } } VB.Net
Imports Arrangement.IO Imports Organization.Data Partial Class VB Inherits Organisation.Spider web.UI.Folio Protected Sub ImportCSV(sender As Object, e As EventArgs) 'Upload and salvage the file Dim csvPath As Cord = Server.MapPath("~/Files/") + Path.GetFileName(FileUpload1.PostedFile.FileName) FileUpload1.SaveAs(csvPath) 'Create a DataTable. Dim dt Equally New DataTable() dt.Columns.AddRange(New DataColumn(iv) {New DataColumn("Id", GetType(Integer)), New DataColumn("Name", GetType(Cord)), New DataColumn("Applied science", GetType(String)), New DataColumn("Company", GetType(Cord)), New DataColumn("Country", GetType(Cord))}) 'Read the contents of CSV file. Dim csvData Equally Cord = File.ReadAllText(csvPath) 'Execute a loop over the rows. For Each row As String In csvData.Split up(ControlChars.Lf) If Not String.IsNullOrEmpty(row) Then dt.Rows.Add together() Dim i As Integer = 0 'Execute a loop over the columns. For Each cell As String In row.Split(","c) dt.Rows(dt.Rows.Count - one)(i) = jail cell i += ane Next Stop If Adjacent 'Bind the DataTable. GridView1.DataSource = dt GridView1.DataBind() Stop Sub End Grade Screenshots
CSV File
Output
Summary
CSV file is a estimator file that contains Comma Separated (Comma Delimited) values. The information from a CSV file is scanned and then, separating the values, a DataTable is created which is able to be accepted populate the ASP.Internet GridView control.
Source: https://www.c-sharpcorner.com/article/how-to-upload-browse-and-show-csv-file-information-in-asp-net-gridview/
0 Response to "Upload and Read Csv File in Net Core"
Post a Comment