Actually i have an excel file consists of 60 columns and 20 rows from that i want to fetch only 6 columns so i have written below code(Input array is

  • Thread starter Thread starter srilekha bolamoni
  • Start date Start date
S

srilekha bolamoni

Guest
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.VisualBasic.FileIO;
namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string csv_file_path = @"C: \Users\sbolamoni\Desktop\Importer_file\ocho.csv";
DataTable csvData = GetDataTabletFromCSVFile(csv_file_path);
Console.WriteLine("Rows count:" + csvData.Rows.Count);
Console.ReadLine();
}
private static DataTable GetDataTabletFromCSVFile(string csv_file_path)
{
DataTable csvData = new DataTable();
try
{


using (TextFieldParser csvReader = new TextFieldParser(csv_file_path))
{
csvReader.SetDelimiters(new string[] { "," });
csvReader.HasFieldsEnclosedInQuotes = true;
string[] colFields = csvReader.ReadFields();
foreach (string column in colFields)
{
if (column == "DealerID" || column == "StockNo" || column == "VIN"||column=="Make"||column=="Model"||column=="DealerName")
{
DataColumn datecolumn = new DataColumn(column);
datecolumn.AllowDBNull = true;
csvData.Columns.Add(datecolumn);
}
while (!csvReader.EndOfData)
{
string[] fieldData = csvReader.ReadFields();
for (int i = 0; i < fieldData.Length; i++)
{
if (fieldData == "")
{
fieldData = null;
}
}
csvData.Rows.Add(fieldData);

}
}
}

}
catch (Exception)
{
throw;
}
return csvData;
}
}
}
  1. please can anyone help me out
  2. thanks in advance

Continue reading...
 

Similar threads

Back
Top