public static void ReadSV(TextReader reader, params string[] separators) { using (Microsoft.VisualBasic.FileIO.TextFieldParser MyReader = new Microsoft.VisualBasic.FileIO.TextFieldParser(reader)) { MyReader.TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.Delimited; MyReader.SetDelimiters(","); MyReader.HasFieldsEnclosedInQuotes = true; string[] currentRow = null; while (!MyReader.EndOfData) { try { currentRow = MyReader.ReadFields(); string currentField = null; foreach (string currentField_loopVariable in currentRow) { currentField = currentField_loopVariable; } } catch (Microsoft.VisualBasic.FileIO.MalformedLineException ex) { } } } }
static IEnumerable<string[]> ReadFields(string data,bool trimWS) { using (var tfp = new Microsoft.VisualBasic.FileIO.TextFieldParser(new StringReader(data))) { tfp.Delimiters = new[] { "," }; tfp.TrimWhiteSpace = trimWS; while (!tfp.EndOfData) { yield return tfp.ReadFields(); } } }
/// <summary> /// parses a CSV file (with headers) into a DataTable, and returns it /// </summary> /// <param name="csvFilePath"></param> /// <returns></returns> private DataTable parseCsvIntoDataTable(string csvFilePath) { using (Microsoft.VisualBasic.FileIO.TextFieldParser parser = new Microsoft.VisualBasic.FileIO.TextFieldParser(csvFilePath)) { parser.TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.Delimited; parser.SetDelimiters(","); parser.HasFieldsEnclosedInQuotes = true; using (DataTable fileData = new DataTable()) { while (!parser.EndOfData) { if (parser.LineNumber == 1) { foreach (string field in parser.ReadFields()) { fileData.Columns.Add(field); } } else { fileData.Rows.Add(parser.ReadFields()); } } return(fileData); } } }
private static List <Ingredient> GetIngredients(string filename, bool ignoreFirstLine = true) { List <Ingredient> ingredients = new List <Ingredient>(); using (Microsoft.VisualBasic.FileIO.TextFieldParser parser = new Microsoft.VisualBasic.FileIO.TextFieldParser(filename)) { parser.TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.Delimited; parser.SetDelimiters(","); if (ignoreFirstLine) { parser.ReadFields(); } while (!parser.EndOfData) { string[] fields = parser.ReadFields(); if (fields.Length == 8) { Ingredient i = new Ingredient(fields[0], fields[1], fields[2], fields[3], fields[4], fields[5], fields[6], fields[7]); ingredients.Add(i); } } } return(ingredients); }
public static IEnumerable <string[]> Records(this Microsoft.VisualBasic.FileIO.TextFieldParser csv, bool ignoreInvalidRecords) { if (csv.Delimiters == null || csv.Delimiters.Length == 0) { csv.SetDelimiters(","); } string[] record = null; while (!csv.EndOfData) { record = null; if (ignoreInvalidRecords) { try { record = csv.ReadFields(); } catch { } } else { record = csv.ReadFields(); } if (record != null) { yield return(record); } } }
static List <Effect> GetEffects(string filename, bool ignoreFirstLine = true) { List <Effect> effects = new List <Effect>(); using (Microsoft.VisualBasic.FileIO.TextFieldParser parser = new Microsoft.VisualBasic.FileIO.TextFieldParser(filename)) { parser.TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.Delimited; parser.SetDelimiters(","); if (ignoreFirstLine) { parser.ReadFields(); } while (!parser.EndOfData) { string[] fields = parser.ReadFields(); if (fields.Length == 10) { Effect e = new Effect(fields[0], fields[1], fields[2], fields[3], fields[4], fields[5], fields[6], fields[7], fields[8], fields[9]); effects.Add(e); } } } return(effects); }
static List <EffectException> GetExceptions(string filename, bool ignoreFirstLine = true) { List <EffectException> exceptions = new List <EffectException>(); using (Microsoft.VisualBasic.FileIO.TextFieldParser parser = new Microsoft.VisualBasic.FileIO.TextFieldParser(filename)) { parser.TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.Delimited; parser.SetDelimiters(","); if (ignoreFirstLine) { parser.ReadFields(); } while (!parser.EndOfData) { string[] fields = parser.ReadFields(); if (fields.Length == 5) { EffectException e = new EffectException(fields[0], fields[1], fields[2], fields[3], fields[4]); exceptions.Add(e); } } } return(exceptions); }
public static DataTable GetCsvFile(string csv_file_path) { var csvData = new DataTable(); if (csv_file_path.EndsWith(".csv")) { using (var csvReader = new Microsoft.VisualBasic.FileIO.TextFieldParser(csv_file_path, Encoding.GetEncoding(1252))) { csvReader.SetDelimiters(new string[] { ";" }); csvReader.HasFieldsEnclosedInQuotes = true; //read column string[] colFields = csvReader.ReadFields(); foreach (string column in colFields) { var 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[i] == "") { fieldData[i] = null; } } csvData.Rows.Add(fieldData); } } } return(csvData); }
static List <Ingredient> GetIngredients(string filename, bool ignoreFirstLine = true) { List <Ingredient> ingredients = new List <Ingredient>(); using (Microsoft.VisualBasic.FileIO.TextFieldParser parser = new Microsoft.VisualBasic.FileIO.TextFieldParser(filename)) { parser.TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.Delimited; parser.SetDelimiters(","); if (ignoreFirstLine) { parser.ReadFields(); } while (!parser.EndOfData) { string[] fields = parser.ReadFields(); if (fields.Length == 20) { Effect effect1 = new Effect(fields[1], fields[2], fields[3], fields[4]); Effect effect2 = new Effect(fields[5], fields[6], fields[7], fields[8]); Effect effect3 = new Effect(fields[9], fields[10], fields[11], fields[12]); Effect effect4 = new Effect(fields[13], fields[14], fields[15], fields[16]); Ingredient e = new Ingredient(fields[0] //name , effect1, effect2, effect3, effect4 , Convert.ToSingle(fields[17]) //weight , Convert.ToInt32(fields[18]) //value , fields[19] //obtained ); ingredients.Add(e); } } } return(ingredients); }
IEnumerable <CodeImport> ParseCSV(string filename) { List <CodeImport> codes = new List <CodeImport>(); string path = Path.Combine(ResourcesFolder, filename); using (var parser = new Microsoft.VisualBasic.FileIO.TextFieldParser(path)) { parser.TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.Delimited; parser.SetDelimiters(","); parser.TrimWhiteSpace = true; //get the headers string[] headers = parser.ReadFields(); int lineNumber = 1; while (parser.EndOfData == false) { CodeImport code = new CodeImport(); string[] line = parser.ReadFields(); for (int i = 0; i < line.Length; i++) { switch (headers[i].ToUpperInvariant()) { case "CRITERIAINDEX": try { code.CriteriaIndex = Convert.ToInt32(line[i]); }catch { throw new FormatException($"Unable to convert Criteria Index to integer on line { lineNumber }. Value: \"{ line[i] }\""); } break; case "CODETYPE": code.CodeType = line[i]; break; case "CODE": code.Code = line[i]; break; case "EXACTMATCH": code.SearchMethodType = string.Equals("1", line[i], StringComparison.OrdinalIgnoreCase) ? DTO.Enums.TextSearchMethodType.ExactMatch : DTO.Enums.TextSearchMethodType.StartsWith; break; } } if (code.IsEmpty() == false) { codes.Add(code); } lineNumber++; } } return(codes.OrderBy(c => c.CriteriaIndex).ThenBy(c => c.CodeType).ThenBy(c => c.SearchMethodType)); }
private void LoadTable(DataTable table, Stream stream) { var timespanExpression = new Regex("([0-9]+):([0-9]+):([0-9]+)"); var parseDictionary = new Dictionary<Type, Delegate>(); //parseDictionary.Add(typeof(Boolean), new Func<String, Boolean>(fieldValue => Convert.ToBoolean(Int32.Parse(fieldValue)))); parseDictionary.Add(typeof(DateTime), new Func<String, Object>(fieldValue => { if(fieldValue.Length.Equals(8)) return new DateTime(Int32.Parse(fieldValue.Substring(0, 4)), Int32.Parse(fieldValue.Substring(4, 2)), Int32.Parse(fieldValue.Substring(6, 2))); else return DBNull.Value; })); //parseDictionary.Add(typeof(Decimal), new Func<String, Decimal>(fieldValue => Decimal.Parse(fieldValue))); parseDictionary.Add(typeof(Int32), new Func<String, Object>(fieldValue => { Int32 test; if (Int32.TryParse(fieldValue, out test)) return test; else return DBNull.Value; })); //parseDictionary.Add(typeof(TimeSpan), new Func<String, Object>(fieldValue => { try { var timeSpanPart = fieldValue.Split(':'); return new TimeSpan(Int32.Parse(timeSpanPart[0]), Int32.Parse(timeSpanPart[1]), Int32.Parse(timeSpanPart[2])); } catch { return DBNull.Value; } })); var textFieldParser = new Microsoft.VisualBasic.FileIO.TextFieldParser(stream); textFieldParser.TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.Delimited; textFieldParser.SetDelimiters(","); textFieldParser.HasFieldsEnclosedInQuotes = false; var fieldReferences = textFieldParser.ReadFields().Where(item=>!String.IsNullOrEmpty(item)).ToArray(); while (!textFieldParser.EndOfData) { var fields = textFieldParser.ReadFields(); var newRow = table.NewRow(); for (var index = 0; index < fieldReferences.Length; index++) { var fieldReference = fieldReferences[index]; var fieldValue = fields[index]; if (table.Columns.Contains(fieldReference)) { if (parseDictionary.ContainsKey(table.Columns[fieldReference].DataType)) newRow[fieldReference] = parseDictionary[table.Columns[fieldReference].DataType].DynamicInvoke(fieldValue); else newRow[fieldReference] = fieldValue; } } table.Rows.Add(newRow); //Console.WriteLine(String.Format("{0} {1}",table.TableName, table.Rows.Count)); } }
/// <summary> /// 读取CSV文件到DataTable /// </summary> /// <param name="fpath"></param> /// <param name="header">第一行数据是否为表头</param> /// <returns></returns> public static DataTable CSVToDataTable(string fpath, bool header = true) { DataTable dt = new DataTable(); if (File.Exists(fpath)) { DataRow row = null; string[] rowArr = null; using (Microsoft.VisualBasic.FileIO.TextFieldParser parser = new Microsoft.VisualBasic.FileIO.TextFieldParser(fpath, Encoding.GetEncoding("GB2312"))) { parser.Delimiters = new string[] { "," }; parser.TrimWhiteSpace = true; while (!parser.EndOfData) { if (parser.LineNumber == 1) { rowArr = parser.ReadFields(); if (!header) { for (int i = 0; i < rowArr.Length; i++) { dt.Columns.Add(i.ToString()); } row = dt.NewRow(); for (int i = 0; i < dt.Columns.Count; i++) { row[i] = rowArr[i]; } dt.Rows.Add(row); } else { foreach (string col in rowArr) { dt.Columns.Add(col); } } } else { rowArr = parser.ReadFields(); row = dt.NewRow(); for (int i = 0; i < dt.Columns.Count; i++) { row[i] = rowArr[i]; } dt.Rows.Add(row); } } parser.Close(); } } else { throw new FileNotFoundException(); } return(dt); }
//[Benchmark] public void vb() { using var reader = OpenTextReader(); var parser = new Microsoft.VisualBasic.FileIO.TextFieldParser(reader) { Delimiters = new[] { "," }, TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.Delimited, TrimWhiteSpace = false, HasFieldsEnclosedInQuotes = true, }; foreach (var rec in Read().Take(Max)) { EnsureFieldCount(rec.Length); } IEnumerable <string[]> Read() { while (true) { var record = parser.ReadFields(); if (record == null) { yield break; } yield return(record); } } }
public static void SeedGenres(IGenreRepository genrerepo) { using (FileStream fs = File.Open("DataInit\\genres.csv", FileMode.Open, FileAccess.Read, FileShare.None)) { var parser = new Microsoft.VisualBasic.FileIO.TextFieldParser(fs); parser.TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.Delimited; parser.SetDelimiters(new string[] { "," }); //genrerepo.Clear(); genrerepo.SetIdentityInsert(true); bool first = true; while (!parser.EndOfData) { // 0: genreId, 1: genre string[] row = parser.ReadFields(); if (first) { first = false; continue; } var genre = new Genre { //GenreId = Int32.Parse(row[0]), Name = row[1] }; genrerepo.Create(genre); } genrerepo.SetIdentityInsert(false); } }
/// <summary> /// Adapted from: /// https://stackoverflow.com/questions/1405038/reading-a-csv-file-in-net /// </summary> /// <param name="csvFilePath"></param> /// <returns></returns> public static List <CsvReadData> ReadCsvTemplatesFile(string csvFilePath) { List <CsvReadData> wiList = new(); var parser = new Microsoft.VisualBasic.FileIO.TextFieldParser(csvFilePath) { TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.Delimited }; parser.SetDelimiters(new string[] { "," }); // File must start with field names on line 1; int rowNumber = 0; while (!parser.EndOfData) { string[] row = parser.ReadFields(); if (rowNumber > 0) { // We only need the workitem's id for AzDo ! wiList.Add(new CsvReadData() { Id = row[0], Title = row[2] }); } rowNumber += 1; } return(wiList); }
/// <summary> /// Read data from SWAT output file using TextFieldParser /// </summary> /// <remarks> /// 1. Need to know the number and width of columns /// 2. Doesn't support select partial data /// </remarks> static void TestExtractFromText_TextFieldParser(string filePah) { Console.WriteLine("TextFieldParser"); DateTime before = DateTime.Now; using (Microsoft.VisualBasic.FileIO.TextFieldParser reader = new Microsoft.VisualBasic.FileIO.TextFieldParser(filePah)) { reader.TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.FixedWidth; reader.SetFieldWidths(6, 4, 10, 4, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 11, 10, 10, 10, 6); string[] currentRow = null; int i = 0; //ignore the first 9 lines while (!reader.EndOfData && i < 9) { reader.ReadLine(); i++; } while (!reader.EndOfData) { currentRow = reader.ReadFields(); } } DateTime after = DateTime.Now; Console.WriteLine(string.Format("******\nTime Used: {0} seconds\n******", after.Subtract(before).TotalSeconds)); }
//Добавить загрузку котировок со всей папки static string[,] Parser() { //Массив котировок int i = 0; char splitchar = ' '; string[,] arrCot = new string[300000, 4]; string[] strArr = null; // Парсим CSV файл в массив using (Microsoft.VisualBasic.FileIO.TextFieldParser parser = new Microsoft.VisualBasic.FileIO.TextFieldParser(@"C:\Users\P.mihalchenko\source\repos\WindowsFormsApp1\CSV\SPFB.RTS_180625_180626.csv")) { parser.TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.Delimited; parser.SetDelimiters(","); while (!parser.EndOfData) { string[] fields = parser.ReadFields(); foreach (string field in fields) { //Загоняем данные из файла CSV в массив splitchar = ';'; strArr = field.Split(splitchar); for (int j = 0; j <= 3; j++) { arrCot[i, j] = strArr[j]; } i++; } } } return(arrCot); }
public static IEnumerable <RoomInfo> ReadFromCsv(string filePath) { var parser = new Microsoft.VisualBasic.FileIO.TextFieldParser(filePath) { TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.Delimited, Delimiters = new[] { "," }, HasFieldsEnclosedInQuotes = true, CommentTokens = new[] { "id", "listing_url" }, }; while (!parser.EndOfData) { string[] row = parser.ReadFields(); RoomInfo parsedRoomInfo; try { parsedRoomInfo = FromCsvRow(row); } catch (FormatException) { // Source data contains some corrupt rows, so just skip it. //Console.WriteLine($"Corrupt row: {string.Join(",", row)}"); continue; } yield return(parsedRoomInfo); } }
public static ArrayList getCSV(string FilePath, string strEncoding) { ArrayList csvData = new ArrayList(); if (strEncoding == "") { strEncoding = "Shift_JIS"; } Encoding encode = Encoding.GetEncoding(strEncoding); Microsoft.VisualBasic.FileIO.TextFieldParser tfp = new Microsoft.VisualBasic.FileIO.TextFieldParser(FilePath, encode); //区切り文字を,とする tfp.Delimiters = new string[] { "," }; //フィールドを"で囲み、改行文字、区切り文字を含めることができるか //デフォルトでtrueなので、必要なし tfp.HasFieldsEnclosedInQuotes = true; //フィールドの前後からスペースを削除する //デフォルトでtrueなので、必要なし tfp.TrimWhiteSpace = true; while (!tfp.EndOfData) { string[] fields = tfp.ReadFields(); csvData.Add(fields); } tfp.Close(); return(csvData); }
static void Main(string[] args) { string filename = @"C:\Users\Evan\Downloads\Dataset\Dataset\EasyClinic_tc_cc\oracle.csv"; string outfile = @"C:\Users\Evan\Downloads\Dataset\Dataset\EasyClinic_tc_cc\oracle.txt"; TextReader reader = new StreamReader(filename); int numSources = reader.ReadLine().Split(new char[] { ',' }, StringSplitOptions.None).Count(); reader.Close(); Microsoft.VisualBasic.FileIO.TextFieldParser parser = new Microsoft.VisualBasic.FileIO.TextFieldParser(filename); parser.TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.Delimited; parser.SetDelimiters(","); List<List<string>> data = new List<List<string>>(); for (int i = 0; i < numSources; i++) data.Add(new List<string>()); int curID = 0; while (!parser.EndOfData) { string[] currentRow = parser.ReadFields(); foreach (string currentField in currentRow) { data[curID].Add(currentField); curID++; } curID = 0; } TextWriter of = new StreamWriter(outfile); foreach (List<string> row in data) { of.WriteLine(String.Join(" ", row)); } of.Flush(); of.Close(); }
public static void ReadSV(TextReader reader, params string[] separators) { using (Microsoft.VisualBasic.FileIO.TextFieldParser MyReader = new Microsoft.VisualBasic.FileIO.TextFieldParser(reader)) { MyReader.TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.Delimited; MyReader.SetDelimiters(","); MyReader.HasFieldsEnclosedInQuotes = true; string[] currentRow = null; while (!MyReader.EndOfData) { try { currentRow = MyReader.ReadFields(); string currentField = null; foreach (string currentField_loopVariable in currentRow) { currentField = currentField_loopVariable; } } catch (Microsoft.VisualBasic.FileIO.MalformedLineException ex) { } } } }
private static void loadHeroResource() { try { heroList = new List <HeroData>(); Microsoft.VisualBasic.FileIO.TextFieldParser parser = new Microsoft.VisualBasic.FileIO.TextFieldParser( HERO_CSV_PATH, System.Text.Encoding.GetEncoding(932)); parser.SetDelimiters(new string[] { "," }); //,区切り parser.HasFieldsEnclosedInQuotes = true; //" escape parser.TrimWhiteSpace = true; //trim on while (!parser.EndOfData) { //フィールドを読み込む string[] fields = parser.ReadFields(); HeroData data = HeroData.fromCSVValue(fields); if (data != null) { heroList.Add(data); } } } catch (Exception e) { Console.WriteLine(e); Console.Error.WriteLine("CSV parse error"); } }
static List <InvoiceTotal> ReadTotalsFile(string totalsFile) { List <InvoiceTotal> totalsList = new List <InvoiceTotal>(); try { using (Microsoft.VisualBasic.FileIO.TextFieldParser parser = new Microsoft.VisualBasic.FileIO.TextFieldParser(totalsFile)) { parser.TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.Delimited; parser.SetDelimiters(","); while (!parser.EndOfData) { string[] words = parser.ReadFields(); if (!words[0].Any(Char.IsLetter)) { totalsList.Add(new InvoiceTotal(int.Parse(words[0]), decimal.Parse(words[1]))); } } parser.Close(); } } catch (Exception ex) { Console.WriteLine(ex.ToString()); } return(totalsList); }
public IEnumerable <string[]> ReadRows() { stream.Seek(0, SeekOrigin.Begin); List <string[]> rows = new List <string[]>(); using (var parser = new Microsoft.VisualBasic.FileIO.TextFieldParser(stream)) { parser.TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.Delimited; parser.SetDelimiters(","); while (!parser.EndOfData) { string[] fields = new string[0]; try { fields = parser.ReadFields(); } catch (Exception) { continue; } rows.Add(fields); } } return(rows.ToArray()); }
private IEnumerable <Task> Parse(AirportInfoDelegate airportInfoCallback) { var parser = new Microsoft.VisualBasic.FileIO.TextFieldParser(this.csvFile); parser.TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.Delimited; parser.SetDelimiters(","); while (!parser.EndOfData) { //Process row string[] fields = parser.ReadFields(); if (7 != fields.Count()) { continue; } var identifier = fields[0]; var city = fields[1]; var state = fields[2]; var name = fields[3]; var chart = fields[4]; var region = fields[5]; var afdlink = fields[6]; yield return(airportInfoCallback.Invoke(identifier, city, state, name, chart, region, afdlink)); } parser.Close(); }
private List <structDWG> ReadCSV(string filePathName, bool hasHeaders) { List <structDWG> dwgList = new List <structDWG>(); using (Microsoft.VisualBasic.FileIO.TextFieldParser MyReader = new Microsoft.VisualBasic.FileIO.TextFieldParser(filePathName)) { MyReader.TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.Delimited; MyReader.Delimiters = new string[] { "," }; string[] currentRow = null; //if CSV file has headers then discard first line if (hasHeaders == true) { MyReader.ReadLine().Skip(1); } while (!MyReader.EndOfData) { try { currentRow = MyReader.ReadFields(); //create temp sheet structDWG curFile = new structDWG(); curFile.fileName = currentRow[0]; curFile.linkView = currentRow[1]; //add sheet to list dwgList.Add(curFile); } catch (Microsoft.VisualBasic.FileIO.MalformedLineException ex) { Debug.Print("Line " + ex.Message + " is invalid. Skipping"); } } return(dwgList); } }
public async Task<IEnumerable<DistributorSalesmanImport>> Import(string path) { return await Task.Factory.StartNew(() => { var imports = new List<DistributorSalesmanImport>(); var tempFolder = Path.Combine(FileUtility.GetApplicationTempFolder(), Path.GetFileName(path)); if (File.Exists(tempFolder)) File.Delete(tempFolder); File.Copy(path, tempFolder); using (var parser = new Microsoft.VisualBasic.FileIO.TextFieldParser(tempFolder)) { parser.SetDelimiters("\t"); string[] currentRow = null; while (!parser.EndOfData) { currentRow = parser.ReadFields(); if (currentRow != null && currentRow.Length > 0) { imports.Add(MapSalesmanImport(currentRow)); } } } File.Delete(tempFolder); return imports; }); }
/// <summary> /// 固定時間リストをインポートして DB に登録します。 /// </summary> /// <param name="fileName"></param> private void ImportFixedTimeListCore(string fileName) { // カーソルを待機状態にする Cursor.Current = Cursors.WaitCursor; try { List <FixedTimeModel> importList = new List <FixedTimeModel>(); // csvファイルをインポート(固定時間,名称,固定番号のデータを前提とする) using (var paser = new Microsoft.VisualBasic.FileIO.TextFieldParser(fileName, Encoding.GetEncoding("Shift_JIS"))) { paser.TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.Delimited; paser.SetDelimiters(","); while (!paser.EndOfData) { try { var fields = paser.ReadFields(); if (fields.Length != 3) { continue; } var item = new FixedTimeModel() { FixedTimeValue = int.Parse(fields[0]), // 固定時間 Name = fields[1], // 名称 FixedNumber = int.Parse(fields[2]) // 固定番号 }; importList.Add(item); } catch (Exception ex) { var method = MethodBase.GetCurrentMethod(); base.WriteLog(method, ex.Message); } } } // DBへ登録 DataServiceFacade.Instance.AddFixedTimeData(importList); MessageBox.Show(String.Format("{0}件インポートしました", importList.Count()), "DB操作", MessageBoxButtons.OK, MessageBoxIcon.Information); // 表示の更新 this.GetFixedTimeAll(); } catch (Exception ex) { var method = MethodBase.GetCurrentMethod(); Console.WriteLine("{0},{1}(),{2}", method.DeclaringType.FullName, method.Name, ex.Message); MessageBox.Show(ex.Message, "DBエラー", MessageBoxButtons.OK, MessageBoxIcon.Error); } finally { // カーソルを元に戻す Cursor.Current = Cursors.Default; } }
public static System.Data.DataTable GetInputDatatable(Activity dnActivity, IEnumerable <LinkedService> linkedServices, IEnumerable <Dataset> datasets) { //SQL or Azure Blob CSV only var inLS = LinkedServiceHelper.GetInputLinkedService(dnActivity, linkedServices, datasets); System.Data.DataTable dsRtn = GetInputDatatableShell(dnActivity, linkedServices, datasets); //Figure out which Type switch (inLS.Properties.Type) { case "AzureStorage": CloudStorageAccount inputStorageAccount = CloudStorageAccount.Parse(((AzureStorageLinkedService)inLS.Properties.TypeProperties).ConnectionString); CloudBlobClient inputClient = inputStorageAccount.CreateCloudBlobClient(); AzureBlobDataset abdInput = datasets.Single(d => d.Name == dnActivity.Inputs.First().Name).Properties.TypeProperties as AzureBlobDataset; CloudBlockBlob cbbInputFile = new CloudBlockBlob(new Uri(inputStorageAccount.BlobEndpoint.AbsoluteUri + abdInput.FolderPath + "/" + abdInput.FileName)); System.IO.MemoryStream ms = new System.IO.MemoryStream(); cbbInputFile.DownloadToStream(ms); ms.Position = 0; using (Microsoft.VisualBasic.FileIO.TextFieldParser tfp = new Microsoft.VisualBasic.FileIO.TextFieldParser(ms)) { tfp.TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.Delimited; tfp.SetDelimiters(","); while (!tfp.EndOfData) { string[] fields = tfp.ReadFields(); dsRtn.LoadDataRow(fields, true); } } break; case "AzureSqlDatabase": AzureSqlTableDataset astInput = datasets.Single(d => d.Name == dnActivity.Inputs.First().Name).Properties.TypeProperties as AzureSqlTableDataset; System.Data.SqlClient.SqlConnection scInput = new System.Data.SqlClient.SqlConnection(((AzureSqlDatabaseLinkedService)inLS.Properties.TypeProperties).ConnectionString); System.Data.SqlClient.SqlCommand commInput = new System.Data.SqlClient.SqlCommand(); commInput.Connection = scInput; commInput.CommandType = System.Data.CommandType.Text; commInput.CommandText = string.Format("SELECT * FROM [{0}]", astInput.TableName); System.Data.SqlClient.SqlDataAdapter sdaInput = new System.Data.SqlClient.SqlDataAdapter(commInput); sdaInput.Fill(dsRtn); break; default: throw new NotImplementedException(); } return(dsRtn); }
public static BillingDetailLineItem Parse(string text) { var reader = new StringReader(text); var fieldParser = new Microsoft.VisualBasic.FileIO.TextFieldParser(reader); fieldParser.TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.Delimited; fieldParser.SetDelimiters(","); var items = fieldParser.ReadFields(); var b = new BillingDetailLineItem(); b.AccountOwnerId = items[0].FormatBillingLineItem(); b.AccountName = items[1].FormatBillingLineItem(); b.ServiceAdministratorId = items[2].FormatBillingLineItem(); b.SubscriptionId = items[3].FormatBillingLineItem(); b.SubscriptionGuid = items[4].FormatBillingLineItem(); b.SubscriptionName = items[5].FormatBillingLineItem(); b.Date = items[6].FormatBillingLineItem().ToDateTime(); b.Month = items[7].FormatBillingLineItem().ToInt(); b.Day = items[8].FormatBillingLineItem().ToInt(); b.Year = items[9].FormatBillingLineItem().ToInt(); b.Product = items[10].FormatBillingLineItem(); b.ResourceGUID = items[11].FormatBillingLineItem(); b.Service = items[12].FormatBillingLineItem(); b.ServiceType = items[13].FormatBillingLineItem(); b.ServiceRegion = items[14].FormatBillingLineItem(); b.ServiceResource = items[15].FormatBillingLineItem(); b.ResourceQtyConsumed = items[16].FormatBillingLineItem(); b.ResourceRate = items[17].FormatBillingLineItem(); b.ExtendedCost = items[18].FormatBillingLineItem(); b.ServiceSubRegion = items[19].FormatBillingLineItem(); b.ServiceInfo = items[20].FormatBillingLineItem(); b.Component = items[21].FormatBillingLineItem(); b.ServiceInfo1 = items[22].FormatBillingLineItem(); b.ServiceInfo2 = items[23].FormatBillingLineItem(); b.AdditionalInfo = items[24].FormatBillingLineItem(); b.Tags = items[25].FormatBillingLineItem(); if (items.Length > 26) { b.StoreServiceIdentifier = items[26].FormatBillingLineItem(); } if (items.Length > 27) { b.DepartmentName = items[27].FormatBillingLineItem(); } if (items.Length > 28) { b.CostCenter = items[28].FormatBillingLineItem(); } return(b); }
/////////////////////////////// //メソッド名:fncMessageImport() //引 数 :なし //戻り値 :なし //機 能 :メッセージテーブルを確認しデータが // :存在していなければインポート /////////////////////////////// private void fncMessageImport() { try { var context = new SalesContext(); //DBのM_Messageテーブルデータ有無チェック //データが存在していなけばデータをインポート int cntMsg = context.Database.SqlQuery <int>("SELECT count(*) FROM [dbo].[M_Message]").First(); context.Dispose(); if (cntMsg > 0) { return; } //インポートするCSVファイルの指定 string csvpth = System.Environment.CurrentDirectory + "\\Message.csv"; //データテーブルの設定 DataTable dt = new DataTable(); dt.TableName = "M_Message"; //csvファイルの内容をDataTableへ //csvファイル及び、デリミタの指定 var parser = new Microsoft.VisualBasic.FileIO.TextFieldParser(csvpth, Encoding.GetEncoding("Shift-JIS")) { TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.Delimited, Delimiters = new string[] { "," } }; // 全行読み込み var rows = new List <string[]>(); while (!parser.EndOfData) { rows.Add(parser.ReadFields()); } // 列設定 dt.Columns.AddRange(rows.First().Select(s => new DataColumn(s)).ToArray()); // 行追加 foreach (var row in rows.Skip(1)) { dt.Rows.Add(row); } //DB接続情報の取得 var dbpth = System.Configuration.ConfigurationManager.ConnectionStrings["SalesContext"].ConnectionString; //DataTableの内容をDBへ追加 using (var bulkCopy = new SqlBulkCopy(dbpth)) { bulkCopy.DestinationTableName = dt.TableName; bulkCopy.WriteToServer(dt); } } catch (Exception ex) { MessageBox.Show(ex.Message); } }
public static DataTable PopulateDTFromCSVFile(string csv_file_path) { DataTable csvData = new DataTable(); try { if (csv_file_path.EndsWith(".csv")) { using (Microsoft.VisualBasic.FileIO.TextFieldParser csvReader = new Microsoft.VisualBasic.FileIO.TextFieldParser(csv_file_path)) { csvReader.SetDelimiters(new string[] { "," }); csvReader.HasFieldsEnclosedInQuotes = true; //read column string[] colFields = csvReader.ReadFields(); foreach (string column in colFields) { 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[i] == "") { fieldData[i] = null; } } csvData.Rows.Add(fieldData); } } } } catch (Exception ex) { MessageBox.Show("Exce " + ex); } return(csvData); }
public static DataTable GetDataTabletFromCSVFile(string csv_file_path, string sheetname) { DataTable csvData = new DataTable(); try { string con = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + csv_file_path + @"Extended Properties='Excel 8.0;HDR=Yes;'"; if (csv_file_path.EndsWith(".xlsx")) { using (Microsoft.VisualBasic.FileIO.TextFieldParser csvReader = new Microsoft.VisualBasic.FileIO.TextFieldParser(csv_file_path)) { csvReader.SetDelimiters(new string[] { "," }); csvReader.HasFieldsEnclosedInQuotes = true; //read column string[] colFields = csvReader.ReadFields(); foreach (string column in colFields) { 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[i] == "") { fieldData[i] = null; } } csvData.Rows.Add(fieldData); } } } } catch (Exception ex) { MessageBox.Show("Exce " + ex); } return(csvData); }
List <CsvContact> GetContacts(string contactsData) { var ms = new MemoryStream(); var stringBytes = System.Text.Encoding.UTF8.GetBytes(contactsData); ms.Write(stringBytes, 0, stringBytes.Length); ms.Seek(0, SeekOrigin.Begin); var parser = new Microsoft.VisualBasic.FileIO.TextFieldParser(ms); parser.TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.Delimited; parser.SetDelimiters(new string[] { "," }); string[] cols = parser.ReadFields(); List <CsvContact> contacts = new List <CsvContact>(); while (!parser.EndOfData) { string[] row = parser.ReadFields(); try { CsvContact contact = new CsvContact(cols, row); var existingEntry = contacts.FirstOrDefault(c => c.ID == contact.ID); if (existingEntry != null) { // merge instead of adding existingEntry.AddPhones(contact.Phones); } else { contacts.Add(contact); } } catch (InvalidDataException) { // Contact doesn't have a good enough name } } return(contacts); }
/// <summary> /// Parses delimited text like CSV (command-separated values) and /// returns each row of data as a sequence of items. /// </summary> public static IEnumerator <TResult> ParseXsv <THeader, TResult>( this TextReader reader, string delimiter, bool quoted, Func <long, string[], THeader> headerSelector, Func <long, THeader, string[], TResult> resultSelector) { if (reader == null) { throw new ArgumentNullException("reader"); } if (headerSelector == null) { throw new ArgumentNullException("headerSelector"); } if (resultSelector == null) { throw new ArgumentNullException("resultSelector"); } using (var parser = new TextFieldParser(reader) { TextFieldType = FieldType.Delimited, Delimiters = new[] { delimiter }, HasFieldsEnclosedInQuotes = quoted, TrimWhiteSpace = false, }) { var headerInitialzed = false; var header = default(THeader); while (!parser.EndOfData) { if (!headerInitialzed) { header = headerSelector(parser.LineNumber, parser.ReadFields()); headerInitialzed = true; } else { yield return(resultSelector(parser.LineNumber, header, parser.ReadFields())); } } } }
public static DataTable ConvertCSVtoDataTable(string path, bool hasHeader) { DataTable dt = new DataTable(); using (var MyReader = new Microsoft.VisualBasic.FileIO.TextFieldParser(path)) { MyReader.TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.Delimited; MyReader.Delimiters = new String[] { "," }; string[] currentRow; //'Loop through all of the fields in the file. //'If any lines are corrupt, report an error and continue parsing. bool firstRow = true; while (!MyReader.EndOfData) { try { currentRow = MyReader.ReadFields(); //Add the header columns if (hasHeader && firstRow) { foreach (string c in currentRow) { dt.Columns.Add(c, typeof(string)); } firstRow = false; continue; } //Create a new row DataRow dr = dt.NewRow(); dt.Rows.Add(dr); //Loop thru the current line and fill the data out for (int c = 0; c < currentRow.Count(); c++) { dr[c] = currentRow[c]; } } catch (Microsoft.VisualBasic.FileIO.MalformedLineException ex) { //Handle the exception here } } } return dt; }
public IEnumerable<ProductDiscountGroupImport> Import(string path) { try { var productImports = new List<ProductDiscountGroupImport>(); var tempFolder = Path.Combine(FileUtility.GetApplicationTempFolder(), Path.GetFileName(path)); if (File.Exists(tempFolder)) File.Delete(tempFolder); File.Copy(path, tempFolder); using (var parser = new Microsoft.VisualBasic.FileIO.TextFieldParser(tempFolder)) { parser.SetDelimiters(","); string[] currentRow = null; while (!parser.EndOfData) { currentRow = parser.ReadFields(); if (currentRow != null && currentRow.Length > 0) { productImports.Add(MapImport(currentRow)); } } } File.Delete(tempFolder); return productImports; } catch (FileNotFoundException ex) { throw ex; } catch (FieldAccessException ex) { throw ex; } catch (Exception ex) { MessageBox.Show(ex.Message, "Importer Error", MessageBoxButton.OK, MessageBoxImage.Error); return null; } }
/// <summary> /// Splits a string depending on a delimiter and in respecting strings enclosed in quotes. /// </summary> /// <param name="s">The string to split.</param> /// <param name="delimiters">The delimiters to use.</param> /// <returns>The split string as an <see cref="IList{String}"/></returns> internal static IList<string> SplitEnclosedInQuotes(this string s, string[] delimiters) { var allLineFields = new List<string>(); using (var reader = new System.IO.StringReader(s)) { using (var parser = new Microsoft.VisualBasic.FileIO.TextFieldParser(reader)) { parser.Delimiters = delimiters; parser.HasFieldsEnclosedInQuotes = true; string[] fields; while ((fields = parser.ReadFields()) != null) { allLineFields = fields.ToList(); } } } return allLineFields; }
public static DataTable LoadCSV(string filePath) { var parser = new Microsoft.VisualBasic.FileIO.TextFieldParser(filePath); parser.TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.Delimited; parser.SetDelimiters(new string[] { ";" }); Regex regex = new Regex("\"(.*?)\""); DataTable table = new DataTable(); bool isFirstRow = true; while (!parser.EndOfData) { string[] row = parser.ReadFields(); if (row.Length == 0 || row[0].Length == 0) continue; string line = row[0]; string sub = regex.Match(line).Value; if (!string.IsNullOrEmpty(sub)) { line = line.Replace(sub, sub.Replace(", ", ",").Replace(",", "@")); } if(isFirstRow) { foreach (var c in line.Split(',')) { table.Columns.Add(c, typeof(string)); } isFirstRow = false; } else { table.Rows.Add(line.Split(',')); } } return table; }
public async Task<IActionResult> LoadHistory(System.Web.HttpPostedFileBase file) { if (file != null && file.ContentLength > 0) { var reader = new Microsoft.VisualBasic.FileIO.TextFieldParser(file.InputStream); reader.SetDelimiters(","); while (!reader.EndOfData) { var line = reader.ReadFields(); var stock = new Stock(line[0]); var model = new StockTransaction(DateTime.Parse(line[1]), Decimal.Parse(line[2]), int.Parse(line[3])); stockHistory.addTransaction(_applicationDbContext, await GetCurrentUserAsync(), stock, model); } } return Redirect("/Stocks/History"); }
/// <summary> /// List of invoice items. /// This is used for free-form invoices. /// </summary> /// <returns>The list of invoices.</returns> public IList<InvoiceItem> ListLineItems() { if (_listLineItems == null) { _listLineItems = new List<InvoiceItem>(); // Null check if (string.IsNullOrEmpty(CsvLineItems)) return _listLineItems; // Get memory stream of data System.IO.MemoryStream stream = new System.IO.MemoryStream(System.Text.Encoding.UTF8.GetBytes(CsvLineItems)); // Use the inbuildt .NET CSV parser Microsoft.VisualBasic.FileIO.TextFieldParser parser = new Microsoft.VisualBasic.FileIO.TextFieldParser(stream); parser.TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.Delimited; parser.SetDelimiters(","); List<string> headers = new List<string>(); int lineindex = 0; while (!parser.EndOfData) { string[] fields = parser.ReadFields(); int fieldindex = 0; InvoiceItem invocieitem = null; foreach (string field in fields) { // Header, or content? if (lineindex == 0) { headers.Add(field); } else { if (invocieitem == null) invocieitem = new InvoiceItem(); // Parse by header name switch (headers[fieldindex]) { case "kind": invocieitem.Kind = field; break; case "description": invocieitem.Description = field; break; case "quantity": invocieitem.Quantity = decimal.Parse(field, System.Globalization.CultureInfo.InvariantCulture); break; case "unit_price": invocieitem.UnitPrice = decimal.Parse(field, System.Globalization.CultureInfo.InvariantCulture); break; case "amount": invocieitem.Amount = decimal.Parse(field, System.Globalization.CultureInfo.InvariantCulture); break; case "taxed": invocieitem.Taxed = bool.Parse(field); break; case "taxed2": invocieitem.Taxed2 = bool.Parse(field); break; case "project_id": invocieitem.ProjectId = string.IsNullOrEmpty(field) ? 0 : long.Parse(field); break; } } fieldindex++; } lineindex++; if (invocieitem != null) _listLineItems.Add(invocieitem); } parser.Close(); } return _listLineItems; }
// ******************************************************************************************************* // FUNCTION TO PERFORM THE ACTUAL IMPORT // ******************************************************************************************************* public static void runImport(string CSVpath, string authtoken, string IDpath, Windows.Forms.RichTextBox miniConsole) { // // Check CSV file // if (CSVpath == "") { updateConsole(miniConsole, true, "Need to supply a file!"); return; } if (!CSVpath.EndsWith(".csv")) { updateConsole(miniConsole, true, "Supplied file needs to be in .CSV format!"); return; } else if (!IO.File.Exists(CSVpath)) { updateConsole(miniConsole, true, "CSV file '" + CSVpath + "' does not exist."); return; } updateConsole(miniConsole, true, "File '" + CSVpath + "' exists!"); // // Check Authtoken // if (authtoken == "") { string msg = "No AUTHTOKEN supplied."; updateConsole(miniConsole, true, "\n" + msg); return; } else if (authtoken.Length != 32) { updateConsole(miniConsole, true, "AUTHTOKEN should have 32 characters. You supplied a " + authtoken.Length + "-character string."); return; } updateConsole(miniConsole, true, "\nUsing AUTHTOKEN " + authtoken); // // Read & use CSV file // Text.Encoding enc = Text.Encoding.UTF7; Microsoft.VisualBasic.FileIO.TextFieldParser CSVparser = new Microsoft.VisualBasic.FileIO.TextFieldParser(@CSVpath, enc); CSVparser.TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.Delimited; CSVparser.SetDelimiters(","); string[] headings = CSVparser.ReadFields(); int[] zohofields = { 0, 0, 0, 0, 0, 0, 0, 0, 0 }; int colsfound = 0; // match columns by heading (more versatile than assuming consistent column order) updateConsole(miniConsole, true, "\nChecking and matching column headings:"); for(int i = 0 ; i < headings.Length ; i++){ string heading = headings[i]; updateConsole(miniConsole, false, (" " + (i+1) + ":").PadRight(5) + heading.PadRight(35)); if (heading == "Account (SSO)") { updateConsole(miniConsole, true, "--> Matched to Account Name"); colsfound++; zohofields[0] = i+1; } else if (heading == "Product (VGPM)") { updateConsole(miniConsole, true, "--> Matched to Product Name"); colsfound++; zohofields[1] = i+1; } else if (heading == "Offering Type") { updateConsole(miniConsole, true, "--> Matched to Subject"); colsfound++; zohofields[2] = i+1; } else if (heading == "Quantity") { updateConsole(miniConsole, true, "--> Matched to Quantity"); colsfound++; zohofields[3] = i+1; } else if (heading == "Total Sales Value(TSV) (USD)") { updateConsole(miniConsole, true, "--> Matched to Grand Total"); colsfound++; zohofields[4] = i+1; } else if (heading == "Contract Start Date (SSO)") { updateConsole(miniConsole, true, "--> Matched to Contract Start Date"); colsfound++; zohofields[5] = i+1; } else if (heading == "Contract End Date (SSO)") { updateConsole(miniConsole, true, "--> Matched to Contract End Date"); colsfound++; zohofields[6] = i+1; } else if (heading == "Owner (SSO)") { updateConsole(miniConsole, true, "--> Matched to Salesperson"); colsfound++; zohofields[7] = i+1; } else if (heading == "Offering Description") { updateConsole(miniConsole, true, "--> Matched to Description" + i); colsfound++; zohofields[8] = i+1; } else { updateConsole(miniConsole, true, " Column ignored."); } } if (colsfound == 9) { updateConsole(miniConsole, true, "\nMatched all 9 required Zoho fields!"); } else if (colsfound == 0) { string msg = "Could not read .csv file"; updateConsole(miniConsole, true, "\n" + msg); return; } else { string msg = "WARNING: Failed to match all 9 required Zoho fields."; updateConsole(miniConsole, true, "\n" + msg); ErrorMessages.Add(msg); } // // Check Product ID list: // Dictionary<string, string> ids = new Dictionary<string, string>(); if (IDpath == "") { // use default ID list ids = ID_default.getIDs(); } // check if user-submitted ID list exists else if (!IO.File.Exists(IDpath)) { updateConsole(miniConsole, true, "Product ID list '" + IDpath + "' does not exist."); return; } // // Use Product ID list // else { // read product IDs Microsoft.VisualBasic.FileIO.TextFieldParser IDparser = new Microsoft.VisualBasic.FileIO.TextFieldParser(@IDpath); IDparser.TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.Delimited; IDparser.SetDelimiters(","); updateConsole(miniConsole, true, "\nProcessing product IDs..."); while (!IDparser.EndOfData) { string[] IDfields = IDparser.ReadFields(); string prodID = IDfields[0]; string prodName = IDfields[1].Replace(";", ""); try { ids.Add(prodName, prodID); } catch (ArgumentException) { ErrorMessages.Add("Product ID duplicate entry found: " + prodID + ", " + prodName + ". Entry ignored. All sales will be imported under the first product."); } } } // // Start sending data // (This starts off sending batches of 5 records. If a batch returns an error, // it tries them again 1 by 1 to isolate the problem record(s) while uploading // the others which well-behaved). // updateConsole(miniConsole, true, "\nProcessing records from CSV file..."); int failCount = 0; int rowNumber = 1; int fullSize = 5; int batchSize = fullSize; // // Read CSV file // ReadBatch: string URLtosend = "https://crm.zoho.com/crm/private/xml/SalesOrders/insertRecords?authtoken="+authtoken+"&scope=crmapi&xmlData=<SalesOrders>"; int batchCount = 0; while (!CSVparser.EndOfData && batchCount < batchSize) { try { // put a blank value at the beginning for non-matched fields string[] CSVfieldsORIG = CSVparser.ReadFields(); string[] CSVfields = new string[CSVfieldsORIG.Length + 1]; string[] CSVblank = { "" }; CSVblank.CopyTo(CSVfields, 0); CSVfieldsORIG.CopyTo(CSVfields, 1); // collect all field values into strings string zoho_accname = CSVfields[zohofields[0]]; if (zoho_accname == "") { goto SkipRecord; } string zoho_prodname = zohoCodeFormat(CSVfields[zohofields[1]]); if (zoho_prodname == "") { goto SkipRecord; } string zoho_prodID = ""; try { zoho_prodID = ids[zoho_prodname]; } catch (Exception e) { ErrorMessages.Add("Error getting ID for " + zoho_prodname + ", so product name for this record set to 'unrecognized':" + e.Message); zoho_prodID = "1312357000000156015"; } string zoho_subject = CSVfields[zohofields[2]]; if (zoho_subject == "") { zoho_subject = getSubject(zoho_prodname); } string zoho_prodquan = CSVfields[zohofields[3]]; string zoho_total = CSVfields[zohofields[4]].Replace(",", ""); string zoho_start = USdate(CSVfields[zohofields[5]]); string zoho_end = USdate(CSVfields[zohofields[6]]); string zoho_salper = CSVfields[zohofields[7]]; string zoho_descrip = CSVfields[zohofields[8]]; // make this record into an XML string string thisXMLstring = XMLstring(zoho_accname, zoho_prodname, zoho_prodID, zoho_subject, zoho_prodquan, zoho_total, zoho_start, zoho_end, zoho_salper, zoho_descrip, rowNumber); URLtosend += thisXMLstring; updateConsole(miniConsole, true, " #"+(rowNumber.ToString()+":").PadRight(4)+zoho_prodname.PadRight(25)+zoho_accname); } catch (Exception e) { failCount += 1; ErrorMessages.Add("Error message:" + e.Message); updateConsole(miniConsole, true, e.Message); } SkipRecord: rowNumber++; batchCount++; if (batchSize == 1 && batchCount == fullSize) { batchSize = fullSize; } } // // Finish building XML string-based URL & send data to Zoho // URLtosend += "</SalesOrders>"; updateConsole(miniConsole, true, "Sending to Zoho..."); System.Net.WebClient webc = new System.Net.WebClient(); String ZohoFeedback = ""; try { ZohoFeedback = webc.DownloadString(URLtosend); // THIS LINE ACTUALLY SENDS THE DATA URLtosend = ""; } catch (Net.WebException we) { if (batchSize == 1) { updateConsole(miniConsole, true, "Got WebException for this record, so skipped it: " + we.Message); ErrorMessages.Add(we.Message); } else { updateConsole(miniConsole, true, "Got WebException for this batch: " + we.Message); updateConsole(miniConsole, true, "Retrying last " + batchSize + " records one by one..."); rowNumber -= batchSize; batchSize = 1; goto ReadBatch; } } // // Extract message from Zoho server // Regex r = new Regex(Regex.Escape("<message>") + "(.*?)" + Regex.Escape("</message>")); MatchCollection matches = taggedString("message", ZohoFeedback); // print message to mini console if(ZohoFeedback != ""){ updateConsole(miniConsole, false, "Message from Zoho server: "); } foreach (Match match in matches){ string msg = match.Groups[1].Value; updateConsole(miniConsole, true, msg+"\n"); if (msg != "Record(s) added successfully" && batchSize > 1) { rowNumber -= batchSize; batchSize = 1; goto ReadBatch; } else if (msg != "Record(s) added successfully" && batchSize == 1) { failCount += 1; updateConsole(miniConsole, true, msg); ErrorMessages.Add("Zoho server: "+msg); batchSize = fullSize; } } // // Loop back and keep sending batches until all records have been sent // if (!CSVparser.EndOfData) { goto ReadBatch; } // // Finish up and report any errors // updateConsole(miniConsole, true, "----------------------------------------------------------------------------------------"); updateConsole(miniConsole, true, "IMPORT COMPLETED"); updateConsole(miniConsole, true, (rowNumber - 1).ToString() + " records processed."); updateConsole(miniConsole, false, (rowNumber - 1 - failCount).ToString() + " successful, " + failCount.ToString() + " unsuccessful.\n"); if(ErrorMessages.Count > 0){ updateConsole(miniConsole, true, "Error messages:"); foreach(string errmsg in ErrorMessages){ updateConsole(miniConsole, true, errmsg); } } updateConsole(miniConsole, true, "----------------------------------------------------------------------------------------"); }
private static string[][] ReadFile(string fileName) { outputFields = new List<int>(); List<string[]> outputStream = new List<string[]>(); try { using (VB.FileIO.TextFieldParser parser = new VB.FileIO.TextFieldParser(@fileName)) { parser.Delimiters = new string[] { "," }; string[] template = parser.ReadFields(); //dump template text //mark output fields for (int i = 0; i < template.Length; i++) { if(template[i].ToLower().Contains("output")) { outputFields.Add(i); } } //store rest of the data in output stream while (!parser.EndOfData) { outputStream.Add(parser.ReadFields()); } } } catch (IOException e) { Console.WriteLine("The file could not be read"); } return outputStream.ToArray(); }
private void openECitationCSVToolStripMenuItem_Click(object sender, EventArgs e) { openFileDialog1.Filter = "CSV Files|*.txt;*.csv"; try { if (openFileDialog1.ShowDialog() == DialogResult.OK) { string[] data = System.IO.File.ReadAllLines(openFileDialog1.FileName); char splitChar = '\t'; char[] splitChars = { ' ', '\t', ';', ',' }; foreach (char c in splitChars) { if (data[0].Split(c)[0] == "TITLE") splitChar = c; } var parser = new Microsoft.VisualBasic.FileIO.TextFieldParser(openFileDialog1.FileName); parser.TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.Delimited; parser.SetDelimiters(new string[] { splitChar.ToString() }); int i = 0; while (!parser.EndOfData) { string[] fields = parser.ReadFields(); if (i>0) dataGridView1.Rows.Add(fields); i++; } } } catch (Exception ex) { MessageBox.Show(string.Format("Error reading file: {0}", ex.Message)); } }
/// <summary>Read a csv file into a datatable </summary> /// <param name="csvPath">the path to the csv-file</param> /// <param name="separator">the character separating the values, can be "COMMA", "PUNTCOMMA", "SPATIE" or "TAB", /// for any sepator string the the input is used</param> /// <returns>a datable containing the values form the file</returns> public static DataTable loadCSV2datatable(string csvPath, string separator, int maxRows = 500, System.Text.Encoding codex = null) { FileInfo csv = new FileInfo(csvPath); string sep; DataTable tbl = new DataTable(); System.Text.Encoding textEncoding = System.Text.Encoding.Default; if (codex != null) textEncoding = codex; if (!csv.Exists) throw new Exception("Deze csv-file bestaat niet: " + csv.Name); if( separator == "" || separator == null ) throw new Exception("Deze separator is niet toegelaten"); switch (separator) { case "Comma": sep = ","; break; case "Puntcomma": sep = ";"; break; case "Spatie": sep = " "; break; case "Tab": sep = "/t"; break; default: sep = separator; break; } using (Microsoft.VisualBasic.FileIO.TextFieldParser csvParser = new Microsoft.VisualBasic.FileIO.TextFieldParser(csv.FullName, textEncoding, true)) { csvParser.Delimiters = new string[] { sep }; csvParser.TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.Delimited; csvParser.TrimWhiteSpace = !(separator == "TAB" || separator == "SPATIE") ; string[] colNames = csvParser.ReadFields(); string[] row; int counter = 0; foreach (string colName in colNames) { tbl.Columns.Add(colName); } while (!csvParser.EndOfData) { try { if (counter >= maxRows) { return tbl; } counter++; row = csvParser.ReadFields(); if (tbl.Columns.Count != row.Count()) { throw new Exception("Niet alle rijen hebben hetzelfde aantal kolommen, op eerste lijn: " + tbl.Rows.Count.ToString() + " gevonden: " + row.Count() + " op lijn: " + string.Join(sep, row)); } tbl.Rows.Add(row); } catch (Microsoft.VisualBasic.FileIO.MalformedLineException ex) { throw new Exception("CSV is kan niet worden gelezen, het heeft niet de correcte vorm: " + csvParser.ErrorLine, ex); } } } return tbl; }
public static System.Collections.Generic.List<System.Collections.ArrayList> readCSV(string filename) { Microsoft.VisualBasic.FileIO.TextFieldParser tfp = new Microsoft.VisualBasic.FileIO.TextFieldParser(filename); tfp.Delimiters = new string[] { "," }; tfp.HasFieldsEnclosedInQuotes = false; List<System.Collections.ArrayList> a = new List<System.Collections.ArrayList>(); try { while (!tfp.EndOfData) { string[] s = tfp.ReadFields(); a.Add(new System.Collections.ArrayList(s)); } } catch (Exception err) { } finally { tfp.Close(); } return a; }
static void Main(string[] args) { while (true) { Console.WriteLine("Press any key."); Console.WriteLine("[r]- ReadXsvAsync"); Console.WriteLine("[o]- ReadXsvObservable"); Console.WriteLine("[a]- TextFieldParser"); Console.WriteLine("[p]- Perfomance"); Console.WriteLine("[q]- Perfomance2"); Console.WriteLine("[e]- Exit"); var key = Console.ReadKey(false); switch (key.KeyChar) { case 'o': using (var reader = new XsvReader(new StreamReader(@".\ModelShips.txt"))) //using (var reader = new XsvReader(new StringReader(Properties.Resources.ModelShips))) { var id = System.Threading.Thread.CurrentThread.ManagedThreadId; Console.WriteLine("start:" + id); var disposable = ReadXsvObservable(reader); Console.ReadKey(false); Console.WriteLine("current:" + id); disposable.Dispose(); Console.WriteLine("Disposed"); } break; case 'r': //using (var reader = new XsvReader(new StreamReader(@".\ModelShips.txt"))) using (var reader = new XsvReader(new StringReader(Properties.Resources.ModelShips))) { ReadXsvAsync(reader).Wait(); } break; case 'a': var data = "No., Name, Price, Comment" + Environment.NewLine + "001, りんご, 98円, 青森産" + Environment.NewLine + "002, バナナ, 120円, \" とっても!\r\nお,い,し,い,よ!\"" + Environment.NewLine + "" + Environment.NewLine + "004, \"うまい棒\"\"めんたい\"\"\", 10円," + Environment.NewLine + "005, バナメイ海老, 800円, \"300g\r\n\r\nエビチリに\""; Console.WriteLine("<< Source >>"); Console.WriteLine(data); Console.WriteLine(""); Console.WriteLine("<< XsvReader.Parse() >>"); foreach (var row in Parse(data)) { Console.WriteLine(row.ConcatWith(";") + "<n>"); } Console.WriteLine(""); Console.WriteLine("<< TextFieldParser (TrimWhiteSpace=true) >>"); foreach (var row in ReadFields(data,trimWS:true)) { Console.WriteLine(row.ConcatWith(";") + "<n>"); } Console.WriteLine(""); Console.WriteLine("<< TextFieldParser (TrimWhiteSpace=false) >>"); foreach (var row in ReadFields(data,trimWS:false)) { Console.WriteLine(row.ConcatWith(";") + "<n>"); } break; case 'p': Console.WriteLine(""); var delimiters = new[] { "," }; var sw = System.Diagnostics.Stopwatch.StartNew(); for (int i = 0; i < 1000; i++) { using (var parser = new Microsoft.VisualBasic.FileIO.TextFieldParser(new StringReader(Properties.Resources.ModelShips))) { parser.Delimiters = delimiters; parser.TrimWhiteSpace = true; while (!parser.EndOfData) { parser.ReadFields(); } } } sw.Stop(); Console.WriteLine(string.Format("TextFieldParser: {0} ms", sw.ElapsedMilliseconds)); sw.Reset(); sw.Start(); for (int i = 0; i < 1000; i++) { using (var reader = new XsvReader(new StringReader(Properties.Resources.ModelShips))) { while (!reader.EndOfData) { reader.ReadXsvLine(delimiters).ToArray(); } } } sw.Stop(); Console.WriteLine(string.Format("XsvReader: {0} ms", sw.ElapsedMilliseconds)); break; #if net45 case 'q': ReadAsyncTest().Wait(); break; #endif case 'e': return; } Console.WriteLine("***-***-***-***"); } }
private bool InitializeData() { try { using (System.IO.StreamReader sr = new System.IO.StreamReader( Application.StartupPath + "/shared/quirk/quirk_library.json")) { quirk_data = Newtonsoft.Json.JsonConvert.DeserializeObject<JsonQuirkData>(sr.ReadToEnd()); quirks = quirk_data.quirks.ToList(); sr.Close(); } using (System.IO.FileStream fs = System.IO.File.OpenRead( Application.StartupPath + "/curios/curio_type_library.csv")) using (Microsoft.VisualBasic.FileIO.TextFieldParser parser = new Microsoft.VisualBasic.FileIO.TextFieldParser(fs)) { List<string> curiotypes = new List<string>(); int i = 0; parser.Delimiters = new[] { "," }; while (!parser.EndOfData) { if (parser.ReadFields()[1] != "") { for (i = 0; i < 7; i++) { if (!parser.EndOfData) { parser.ReadLine(); } else break; } curiotypes.Add(parser.ReadFields()[2]); } } cbx_curiotags.Items.Add("(none)"); cbx_curiotags.Items.AddRange( curiotypes.Select(a => a).Distinct().OrderBy(b => b).ToArray()); parser.Close(); } using (System.IO.StreamReader sr = new System.IO.StreamReader( Application.StartupPath + "/shared/buffs/buff_library.json")) { buff_data = Newtonsoft.Json.JsonConvert.DeserializeObject<JsonBuffData>(sr.ReadToEnd()); lbx_buffroster.Items.AddRange( buff_data.buffs.Select( a => a.id) .Distinct() .OrderBy(b => b) .ToArray()); sr.Close(); } descriptions = XDocument.Load( Application.StartupPath + "/localization/quirks.string_table.xml"); dialogue = XDocument.Load( Application.StartupPath + "/localization/dialogue.string_table.xml"); return true; } catch //(Exception ex) { MessageBox.Show("Move this application to your game's installation folder\nor varify your game cache.", "Error"); //System.Diagnostics.Debug.WriteLine(ex); } return false; }
public List<ImportItemModel> Import(string directoryPath) { string tempFolder = string.Empty; var dataModels = new List<ImportItemModel>(); try { tempFolder = Path.Combine(FileUtility.GetApplicationTempFolder(), Path.GetFileName(directoryPath)); if (File.Exists(tempFolder)) File.Delete(tempFolder); File.Copy(directoryPath, tempFolder); using (var parser =new Microsoft.VisualBasic.FileIO.TextFieldParser(tempFolder)) { parser.SetDelimiters(","); while (!parser.EndOfData) { string[] currentRow = parser.ReadFields(); if (currentRow != null && currentRow.Length > 1) { dataModels.Add( new ImportItemModel { ProductCode = currentRow.ElementAtOrDefault(0), Quantity = currentRow.ElementAtOrDefault(1), SalesmanCode = (currentRow.ElementAtOrDefault(2) != null && !string.IsNullOrEmpty(currentRow[2])) ? currentRow.ElementAtOrDefault(2) : "default", DocumentRef = currentRow.ElementAtOrDefault(3) }); } } } return dataModels; } catch (IOException ex) { MessageBox.Show("File Load Error\n" + ex.Message); return null; } catch (Exception ex) { MessageBox.Show("Error\n" + ex.Message); return null; } finally { if (File.Exists(tempFolder)) File.Delete(tempFolder); } }
/// <summary> /// Read data from SWAT output file using TextFieldParser /// </summary> /// <remarks> /// 1. Need to know the number and width of columns /// 2. Doesn't support select partial data /// </remarks> static void TestExtractFromText_TextFieldParser(string filePah) { Console.WriteLine("TextFieldParser"); DateTime before = DateTime.Now; using (Microsoft.VisualBasic.FileIO.TextFieldParser reader = new Microsoft.VisualBasic.FileIO.TextFieldParser(filePah)) { reader.TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.FixedWidth; reader.SetFieldWidths(6,4,10,4, 10,10,10,10,10,10,10,10,10,10, 10,10,10,10,10,10,10,10,10,11, 10,10,10,6); string[] currentRow = null; int i = 0; //ignore the first 9 lines while (!reader.EndOfData && i<9) { reader.ReadLine(); i++; } while(!reader.EndOfData) { currentRow = reader.ReadFields(); } } DateTime after = DateTime.Now; Console.WriteLine(string.Format("******\nTime Used: {0} seconds\n******", after.Subtract(before).TotalSeconds)); }