標籤:url def pen cat text name hdr return pos
先貼代碼:
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(); } }
C# 讀取CSV格式資料 讀取為DataTable類型