先贴代码:
using System.Data;
using System.Data.OleDb;
static void Main(string[] args) { var strpath = System.Environment.CurrentDirectory + "\"; var filename = "ss.csv"; DataSet dataset = GetCsvData(strpath, filename); if (dataset.Tables.Count > 0) { foreach (DataTable itemTable in dataset.Tables) { string table_sheetName=itemTable.TableName;// DataTable datNew = itemTable.DefaultView.ToTable(false, new string[] {//选择列 "列名","ProjectName" }); } } } static DataSet GetCsvData(string fileNameUrl, string fileName) { OleDbConnection OleCon = new OleDbConnection(); OleDbCommand OleCmd = new OleDbCommand(); OleDbDataAdapter OleDa = new OleDbDataAdapter(); DataSet csvData = new DataSet(); OleCon.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileNameUrl + ";Extended Properties='Text;FMT=Delimited;HDR=YES;'"; OleCon.Open(); OleCmd.Connection = OleCon; OleCmd.CommandText = "select * From " + fileName; OleDa.SelectCommand = OleCmd; try { OleDa.Fill(csvData, "CsvFileVCQData"); return csvData; } catch { return csvData; } finally { OleCon.Close(); OleCmd.Dispose(); OleDa.Dispose(); OleCon.Dispose(); } }
---------------------------
2018.4.19更新
在csv中的数据如果有"(双引号会出错)这是由于在读的时候会将数据读为“data”这样 而如果其中含有双引号的话“da""ta” 很明显会出错,解决这个问题可以将双引号改为两个单引号''