IO24107 2012 11 AB WI 100033104410W500 337 516.78 17456.3 0 17456 N 0 控制: HtmlInputFile fileOilFile = fileOilSubmission为HtmlInputFile; if(fileOilFile!= null) strOilFileName = fileOilFile.Value; DataTable: DataTable csvData = new DataTable(); csvData.Columns。添加(RoyaltyEntityID,typeof(string)); csvData.Columns.Add(ProductionYear,typeof(int)); csvData.Columns.Add( ProductionMonth,typeof(int)); csvD ata.Columns.Add(ProductionEntityID,typeof(string)); csvData.Columns.Add(ProductionVolume,typeof(double)); csvData。 Columns.Add(SalePrice,typeof(double)); csvData.Columns.Add(GrossRoyaltyAmount,typeof(double)); csvData.Columns。添加(TruckingRate,typeof(double)); csvData.Columns.Add(TotalNetRoyalty,typeof(double)); csvData.Columns.Add( ConfidentialWell,typeof(bool)); csvData.Columns.Add(HoursProductionAmount,typeof(double)); 使用(StreamReader sr = File.OpenText(strOilFileName)) { string s = String。清空; while((s = sr.ReadLine())!= null) {//我们只测试读取速度 foreach(strOilFileName中的var行) { csvData.Rows.Add(line.spli) t(',')[0]); csvData.Rows.Add(line.split(',')[1]); csvData.Rows。添加(line.split(',')[2]); csvData.Rows.Add(line.split(',')[3]); } } } 和Split不起作用I have csv file uploaded by UploadFile control which has no header row. When I try to read it to datatable it gives the error because the first row has same values in different columns. How to insert the header row to this file or read data from csv to datatable with predefined columnsData in csv:IO23968 2012 11 AB WI 100162804410W500 0 516.78 0 0 0 N 0 IO24190 2012 11 AB WI 100140604510W500 302 516.78 15617.9 0 15617 N 0 IO24107 2012 11 AB WI 100033104410W500 337 516.78 17456.3 0 17456 N 0 Control:HtmlInputFile fileOilFile = fileOilSubmission as HtmlInputFile; if (fileOilFile != null) strOilFileName = fileOilFile.Value;DataTable:DataTable csvData = new DataTable(); csvData.Columns.Add("RoyaltyEntityID", typeof(string)); csvData.Columns.Add("ProductionYear", typeof(int)); csvData.Columns.Add("ProductionMonth", typeof(int)); csvData.Columns.Add("ProductionEntityID", typeof(string)); csvData.Columns.Add("ProductionVolume", typeof(double)); csvData.Columns.Add("SalePrice", typeof(double)); csvData.Columns.Add("GrossRoyaltyAmount", typeof(double)); csvData.Columns.Add("TruckingRate", typeof(double)); csvData.Columns.Add("TotalNetRoyalty", typeof(double)); csvData.Columns.Add("ConfidentialWell", typeof(bool)); csvData.Columns.Add("HoursProductionAmount", typeof(double));using (StreamReader sr = File.OpenText(strOilFileName)) { string s = String.Empty; while ((s = sr.ReadLine()) != null) { //we're just testing read speeds foreach (var line in strOilFileName) { csvData.Rows.Add(line.split(',')[0]); csvData.Rows.Add(line.split(',')[1]); csvData.Rows.Add(line.split(',')[2]); csvData.Rows.Add(line.split(',')[3]); } } } And Split does not work推荐答案 使用 OleDb [ ^ ] with schema.ini [ ^ ]文件;) 更多:定义文字格式(文本文件驱动程序) [ ^ ] Use OleDb[^] with schema.ini[^] file ;)More: Defining Text Format (Text File Driver)[^] 尝试这个.. Try this..DataRow dr = csvData.NewRow();using (StreamReader sr = new StreamReader("/file.csv")){ string line = string.Empty; while ((line = sr.ReadLine()) != null) { string[] strRow = line.Split(','); dr["RoyaltyEntityID"] = strRow[0]; dr["ProductionYear"] = strRow[1]; dr["ProductionMonth"] = strRow[2]; ... csvData.Rows.Add(dr); }} 这篇关于将没有标题行文件的CSV导入数据表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-20 08:23