Hi
I have .csv file and want to upload its data in datatable. I am trying below code but it is giving Invalid File Signature
public static DataTable ReadCSVFile(string fileName)
{
using (var stream = File.Open(fileName, FileMode.Open, FileAccess.Read))
{
// Auto-detect format, supports:
// - Binary Excel files (2.0-2003 format; *.xls)
// - OpenXml Excel files (2007 format; *.xlsx, *.xlsb)
using (var reader = ExcelReaderFactory.CreateReader(stream))
{
do
{
while (reader.Read())
{
// reader.GetDouble(0);
}
} while (reader.NextResult());
DataSet result = reader.AsDataSet();
return result.Tables[0];
}
}
}
Thanks
Amit MohantyPosted Apr 13, 2023, 9:54 AM
With Tab delimeter.
Tuhin PaulPosted Apr 15, 2023, 11:01 AM
A filePath variable is declared that contains the path to the CSV file to be read. A new DataTable object is created to hold the CSV data. The StreamReader class is used to open the CSV file located at filePath and read its contents. The first line of the CSV file is read using ReadLine(), which contains the column headers of the table. The column headers are split into an array of strings using the Split() method with a comma as the delimiter. A foreach loop is used to iterate over each header in the array of headers, and a new DataColumn is created with the header name and added to the DataTable using the Columns.Add() method. The remaining lines of the CSV file are read using a while loop, which continues until EndOfStream is reached. Each row is read using ReadLine(), which returns a string containing comma-separated values. The row string is split into an array of strings using the Split() method with a comma as the delimiter. A new DataRow object is created using NewRow(), and each value in the row array is assigned to the corresponding column using the Item[] property of the DataRow object. The DataRow object is added to the DataTable using the Rows.Add() method. After all rows have been read and added to the DataTable, the resulting DataTable object is returned, containing all of the data from the CSV file in a tabular format.
Tuhin PaulPosted Apr 15, 2023, 10:55 AM
Ramco RamcoPosted Apr 13, 2023, 10:01 AM
Hi AMit
I have written below . Is it o.k
Thanks
Ramco RamcoPosted Apr 13, 2023, 9:43 AM
Hi all
I think separator is tab delimited. I am downloading file from facebook.
it is showing as id\tcreated_time and so on
Thanks
Ramco RamcoPosted Apr 13, 2023, 9:22 AM
Hi Amit
same issue which i just mentioned
I have written below line. Is hould show only id
var colname = dt.Columns[0].ColumnName;
and it shows like this
Thanks
Amit MohantyPosted Apr 13, 2023, 9:11 AM
Naimish MakwanaPosted Apr 13, 2023, 9:01 AM
It seems like you are using the
ExcelReaderFactoryto read a CSV file. However, as the name suggests,ExcelReaderFactoryis used to read Excel files, not CSV files.To read a CSV file and load its data into a DataTable, you can use the
TextFieldParserclass which is available in theMicrosoft.VisualBasic.FileIOnamespace. Here's an example of how you can modify your code to useTextFieldParser:Thanks