/// <summary> /// Do main logic e.g. command line swiches were ok /// </summary> /// <param name="opts">Options</param> private static void RunOptionsAndReturnExitCode(Models.CmdOptions opts, ILogger logger) { if (opts is null) { throw new ArgumentNullException(nameof(opts)); } if (string.IsNullOrWhiteSpace(opts.ExportFileName) && string.IsNullOrWhiteSpace(opts.ImportFilename)) { throw new ArgumentException("Either -i or -o must be specified"); } var csv = new CsvUtility(new CsvOptions(), logger); if (!string.IsNullOrWhiteSpace(opts.ExportFileName)) { dt = CreateData(opts); csv.WriteCsv(opts.ExportFileName, HandleCsvLineWrite); } if (!string.IsNullOrWhiteSpace(opts.ImportFilename)) { csv.ReadCsv(opts.ImportFilename, HandleCsvLineRead); } logger.LogInformation("Exiting..."); }
public void ToCsv() { var table = new DataTable(); for (int i = 0; i < 4; i++) { table.Columns.Add(new DataColumn { ColumnName = ((char)('a' + i)).ToString() }); } for (int r = 0; r < 2; r++) { DataRow row = table.NewRow(); row[0] = "0"; row[1] = "1"; row[2] = " 2"; row[3] = "\"3\""; table.Rows.Add(row); } string csv = CsvUtility.ToCsv(table); string expected = Resources.Load <TextAsset>("table0").text; Assert.NotNull(csv); Assert.AreEqual(expected, csv); }
public void TryResetTest() { using var csv = CsvUtility.ToStream( "name,age\n" + "Homer,35\n" + "Marge,28\n"); using var reader = new CsvReader(new StreamReader(csv)); // Force to consume all the records foreach (var _ in reader.ReadAll()) { } Assert.IsTrue(reader.IsDone); Assert.IsTrue(reader.TryReset()); Assert.IsFalse(reader.IsDone); var enumerator = reader.ReadAll().GetEnumerator(); Assert.IsTrue(enumerator.MoveNext()); Assert.AreEqual("Homer,35", enumerator.Current.ToString()); Assert.IsTrue(enumerator.MoveNext()); Assert.AreEqual("Marge,28", enumerator.Current.ToString()); Assert.IsFalse(enumerator.MoveNext()); Assert.IsTrue(reader.IsDone); }
public ProcessingContextLoader(FolderChooser folderChooser, LocatioSettingsLoader configLoader, ConfigChooser configChooser, CsvUtility csvUtility) { _folderChooser = folderChooser; _configLoader = configLoader; _configChooser = configChooser; _csvUtility = csvUtility; }
// Use this for initialization void Awake() { // event --- //fileLoader.eventWWWFileLoadComplete += (WWW www) => { // string txt = www.text; // Encoding encoder = Encoding.GetEncoding("Shift_JIS"); // string str = encoder.GetString(www.bytes); // CsvItem[] list = CsvUtility.FromCsv<CsvItem>(str); // Debug.Log (">>> "+list.Length); // items = list; //}; //fileLoader.LoadWWW (Application.streamingAssetsPath, "excelTest.csv"); // callback --- fileLoader.LoadWWW(Application.streamingAssetsPath, "excelTest.csv", (WWW www) => { // callback string txt = www.text; Encoding encoder = Encoding.GetEncoding("Shift_JIS"); string str = encoder.GetString(www.bytes); CsvItem[] list = CsvUtility.FromCsv <CsvItem>(str); items = list; }); // stream --- //Encoding encoder = Encoding.GetEncoding("Shift_JIS"); //string loadStr = fileLoader.LoadStream(Application.streamingAssetsPath, "excelTest.csv", encoder); //CsvItem[] list = CsvUtility.FromCsv<CsvItem>(loadStr); }
public void CsvUtilityCreateCsvTest() { var objects = new List <TestObject>(); objects.Add(new TestObject() { StringField = "ABC\"\n,'ABC", IntField = -1, DateField = null, BooleanField = false }); for (var i = 0; i < 500; i++) { var o = new TestObject() { StringField = Replicate('A', i), IntField = i, DateField = DateTime.Now, BooleanField = true }; objects.Add(o); } CsvUtility.CreateCsv(TestingFolder, "CsvObjects" + DateTime.Now.ToFileTime() + "", objects); }
private static void TestWrites(string tableContents, string readerContents) { tableContents.ShouldBe(readerContents); using (StringReader reader = new(readerContents)) { IList <string>?values; int rowIndex = 0; int numColumns = TestData.GetLength(1); while ((values = CsvUtility.ReadLine(reader)) != null) { values.Count.ShouldBe(numColumns); for (int columnIndex = 0; columnIndex < numColumns; columnIndex++) { string value = values[columnIndex]; string?testValue = TestData[rowIndex, columnIndex].ToString(); value.ShouldBe(testValue); } rowIndex++; } rowIndex.ShouldBe(TestData.GetLength(0)); } }
public void DownloadCsv() { var newFileName = ApplicationController.GetSaveFileName(string.Format("{0}", RecordService.GetCollectionName(RecordType)), "csv"); ApplicationController.DoOnAsyncThread(() => { try { LoadingViewModel.IsLoading = true; if (!string.IsNullOrWhiteSpace(newFileName)) { var folder = Path.GetDirectoryName(newFileName); var fileName = Path.GetFileName(newFileName); var fields = FieldMetadata.Select(rf => rf.FieldName); CsvUtility.CreateCsv(folder, fileName, GetGridRecords(true).Records, fields, (f) => RecordService.GetFieldLabel(f, RecordType), (r, f) => { return(RecordService.GetFieldAsDisplayString((IRecord)r, f)); }); ApplicationController.StartProcess("explorer.exe", "/select, \"" + Path.Combine(folder, fileName) + "\""); } } catch (Exception ex) { ApplicationController.UserMessage("Error Downloading CSV: " + ex.DisplayString()); } finally { LoadingViewModel.IsLoading = false; } }); }
public void ReadBlackTest1() { using var csv = CsvUtility.ToStream(" "); using var reader = new CsvReader(new StreamReader(csv), hasHeader: false); Assert.IsNull(reader.Read()); }
public void DownloadCsv(string folder) { ApplicationController.DoOnAsyncThread(() => { CsvForExtraction = false; CsvExtracting = true; CsvExtractingProgress = new ProgressControlViewModel(ApplicationController); CsvExtractingProgress.UpdateProgress(0, 1, "Extracting To CSV"); try { var fileName = string.Format("{0} CSV Download - {1}.csv", ApplicationController.ApplicationName, DateTime.Now.ToFileTime()); CsvUtility.CreateCsv(folder, fileName , Items, new LogController(CsvExtractingProgress)); CsvExtracting = false; CsvExtracted = true; OpenCsvButton = new XrmButtonViewModel("Open CSV", () => OpenFile(folder, fileName), ApplicationController); OpenCsvFolderButton = new XrmButtonViewModel("Open CSV Folder", () => OpenFolder(folder), ApplicationController); } catch (Exception ex) { CsvExtracting = false; CsvForExtraction = true; ApplicationController.UserMessage("Error Downloading CSV: " + ex.DisplayString()); } }); }
/// <summary> /// モデルの読み込み /// </summary> private void InputFileNameButton_Click(object sender, RoutedEventArgs e) { UpdatePhase(ExecutePhase.DataReading); OpenFileDialog ofd = new OpenFileDialog { InitialDirectory = "Resources", FileName = "", DefaultExt = "*.*", Filter = "CSVファイル|*.csv", }; var ret = ofd.ShowDialog(); if (ret.HasValue && ret.Value) { var filePath = ofd.FileName; InputFileNameLabel.Content = ofd.SafeFileName; var datas = CsvUtility.ReadCsv(filePath, true); _cityList.Clear(); _cityList = ModelUtility.GetAllCitiesModel(datas).ToList(); } DrawCities(); UpdatePhase(ExecutePhase.Setting); }
private void CopyAllTasks_Click(object sender, RoutedEventArgs e) { StringBuilder sb = new StringBuilder(); using (StringWriter writer = new StringWriter(sb)) { CsvUtility.WriteLine(writer, new object[] { "Comment", "Priority", "File", "Line", "Project" }); foreach (CommentTask task in this.tasks.Items) { CsvUtility.WriteLine(writer, new object[] { task.Comment, task.Priority, task.FilePath, task.Line, task.Project }); } } // .NET sets the text as Unicode text, but Excel wants CSV data as UTF-8 bytes. string csv = sb.ToString(); DataObject data = new DataObject(); data.SetText(csv); byte[] bytes = Encoding.UTF8.GetBytes(csv); using (var stream = new MemoryStream(bytes)) { data.SetData(DataFormats.CommaSeparatedValue, stream); Clipboard.SetDataObject(data, true); } }
private static void PopulateRestoreResultLookup(Context ctx, string outDir) { foreach (var record in CsvUtility.EnumerateRestoreResults(outDir)) { string variantName = null; if (!string.IsNullOrWhiteSpace(record.VariantName)) { variantName = record.VariantName; if (ctx.ExcludeVariants.Contains(variantName)) { continue; } } var key = (variantName, record.SolutionName, record.LogFileName); if (ctx.RestoreResultLookup.ContainsKey(key)) { ctx.WarningCount++; Console.WriteLine($" WARNING: Duplicate restore result: {variantName}, {record.SolutionName}, {record.LogFileName}"); } else { ctx.RestoreResultLookup.Add(key, record); } } Console.WriteLine($"{ctx.RestoreResultLookup.Count} restore results were found."); }
private void AddValues(params object[] values) { // this.writer can be null if no log file name was provided (e.g., no log folder is configured). if (this.writer != null) { CsvUtility.WriteLine(this.writer, values); } }
/// <summary> /// Gets an <see cref="IEnumerable{T}"/> over the records of this reader csv and parser them to the type T, /// this enumerable will read the records using this reader, so when the iteration /// end the reader will be at the end of the file. /// </summary> /// <returns>An enumerable over the records of this reader csv.</returns> public IEnumerable <T> ReadAllAs <T>(IEnumerable <IValueParser>?parsers = null) { return(ReadAll().Select(record => { Dictionary <string, string> data = record.ToDictionary() !; return CsvUtility.CreateInstance <T>(data, parsers); })); }
public void ReadLineStringTest() { var values = CsvUtility.ReadLine("A, B, C").ToArray(); CollectionAssert.AreEqual(new[] { "A", "B", "C" }, values); values = CsvUtility.ReadLine("A, B, C", false).ToArray(); CollectionAssert.AreEqual(new[] { "A", " B", " C" }, values); }
/// <summary> /// Creates a record from the specified value. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="value">The value.</param> /// <param name="format">The format.</param> /// <returns></returns> public static CsvRecord From <T>(T value, CsvFormat format) { var headerValues = CsvUtility.GetHeader <T>(); var values = CsvUtility.GetValues(value); var header = new CsvHeader(headerValues, format); return(new CsvRecord(header, values, format)); }
public void ReadBlackTest2() { using var csv = CsvUtility.ToStream(" "); var format = CsvFormat.Default.WithIgnoreWhitespace(false); using var reader = new CsvReader(new StreamReader(csv), hasHeader: false, format: format); Assert.AreEqual(" ", reader.Read() !.ToString()); }
void SaveToFile() { Debug.Log("SaveToFile"); string csvStr = CsvUtility.ToCSV(items); string path = System.IO.Path.Combine(Application.streamingAssetsPath, "Save/excelTestSave.csv"); fileWriter.WriteText(path, csvStr); }
/// <summary> /// ログ出力ボタン押下時の処理 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void WriteLogButton_Click(object sender, RoutedEventArgs e) { var date = DateTime.Today; var dateDirectoryName = date.Year.ToString("0000") + date.Month.ToString("00") + date.Day.ToString("00") + "/"; // logフォルダ作成 var directoryInfo = FileUtility.SafeCreateDirectory(LOG_FILE_DIRECTORY + dateDirectoryName); CsvUtility.WriteCsv(_topFittnessList, directoryInfo.FullName + LOG_FILENAME_PREFIX + "(" + _gaSolver.GetSolverInfo() + ")" + CSV_POSTFIX); }
public static void WriteCsvExcelValue(this TextWriter writer, string value) { if (writer is null) { throw new NullReferenceException(); } Contract.EndContractBlock(); CsvWriter.WriteValue(writer, '=' + CsvUtility.WrapQuotes(value)); }
/// <summary> /// Initializes a new instance of the <see cref="CsvDocument{T}"/> class. /// </summary> /// <param name="elements">The elements.</param> /// <param name="format">The format.</param> public CsvDocument(IEnumerable <T> elements, CsvFormat format) { Header = new CsvHeader(CsvUtility.GetHeader <T>(), format); Format = format; foreach (var e in elements) { Write(e); } }
public static void WriteCsvExcelHyperlink(this TextWriter writer, string link, string?text = null) { if (writer is null) { throw new NullReferenceException(); } Contract.EndContractBlock(); WriteCsvExcelValue(writer, $"=HYPERLINK({CsvUtility.WrapQuotes(link)}{(text is null ? string.Empty : (',' + CsvUtility.WrapQuotes(text)))})"); }
public void ReadTableReaderTest() { DataTable table = CreateTestTableAndText(out string tableText); using (StringReader reader = new(tableText)) { DataTable?newTable = CsvUtility.ReadTable(reader); TestReadTable(table, newTable); } }
public void DownloadCsv() { var newFileName = ApplicationController.GetSaveFileName(string.Format("{0}", RecordService.GetCollectionName(RecordType)), "csv"); ApplicationController.DoOnAsyncThread(() => { try { LoadingViewModel.IsLoading = true; if (!string.IsNullOrWhiteSpace(newFileName)) { var folder = Path.GetDirectoryName(newFileName); var fileName = Path.GetFileName(newFileName); var fields = FieldMetadata.ToArray(); var started = DateTime.UtcNow; var labelFuncDictionary = new Dictionary <string, Func <string, string> >(); foreach (var fm in FieldMetadata) { labelFuncDictionary.Add(fm.AliasedFieldName ?? fm.FieldName, s => fm.OverrideLabel ?? RecordService.GetFieldLabel(fm.FieldName, fm.AltRecordType ?? RecordType)); } var displayFuncDictionary = new Dictionary <string, Func <object, string, string> >(); foreach (var fm in FieldMetadata) { displayFuncDictionary.Add(fm.AliasedFieldName ?? fm.FieldName, (o, s) => RecordService.GetFieldAsDisplayString(fm.AltRecordType ?? RecordType, fm.FieldName, ((IRecord)o).GetField(s))); } CsvUtility.CreateCsv(folder, fileName, GetGridRecords(true).Records, fields.Select(f => f.AliasedFieldName ?? f.FieldName), (f) => labelFuncDictionary[f](f), (r, f) => displayFuncDictionary[f](r, f)); ApplicationController.LogEvent("Download CSV", new Dictionary <string, string> { { "Is Completed Event", true.ToString() }, { "Type", RecordType }, { "Seconds Taken", (DateTime.UtcNow - started).TotalSeconds.ToString() }, }); ApplicationController.StartProcess("explorer.exe", "/select, \"" + Path.Combine(folder, fileName) + "\""); } } catch (Exception ex) { ApplicationController.ThrowException(ex); } finally { LoadingViewModel.IsLoading = false; } }); }
public void ReadRecordWithWhiteSpaceTest() { using var csv = CsvUtility.ToStream( "Name,Age\n" + "Homer , 35\n" + " Marge,28\n"); using var reader = CsvReader.FromStream(csv, CsvFormat.Default.WithIgnoreWhitespace(false)); Assert.AreEqual("Homer , 35", reader.Read() !.ToString()); Assert.AreEqual(" Marge,28", reader.Read() !.ToString()); }
public void ReadWithQuoteAlwaysTest() { using var csv = CsvUtility.ToStream( "Name,Age\n" + "Homer,35\n" + "Marge,28\n"); var reader = CsvReader.FromStream(csv, CsvFormat.Default.WithStyle(QuoteStyle.Always)); Assert.AreEqual("\"Homer\",\"35\"", reader.Read() !.ToString()); Assert.AreEqual("\"Marge\",\"28\"", reader.Read() !.ToString()); }
public void CsvUtilityReadCsvTest() { var rows = CsvUtility.SelectAllRows("TestCsv.csv"); Assert.IsTrue(rows.Count() == 4); var textString = rows.Last().GetFieldAsString("TextStringField"); Assert.IsTrue(textString.Length >= 300); var phoneString = rows.Last().GetFieldAsString("PhoneNumber"); Assert.IsTrue(phoneString.StartsWith("0")); }
public void ReadWithQuoteNeverTest() { using var csv = CsvUtility.ToStream( "Name,Age\n" + "Frida \"The Painter\", 35\n" + "Pagannini \"The violinist\",28\n"); var reader = CsvReader.FromStream(csv, CsvFormat.Default.WithStyle(QuoteStyle.Never)); Assert.AreEqual("Frida The Painter,35", reader.Read() !.ToString()); Assert.AreEqual("Pagannini The violinist,28", reader.Read() !.ToString()); }
public override IEnumerable <IFieldMetadata> GetFieldMetadata(string recordType) { lock (_lockObject) { if (_fieldMetadata == null) { _fieldMetadata = CsvUtility.GetColumns(CsvNameQualified).Select(c => new StringFieldMetadata(recordType, c, c)) .ToArray(); } return(_fieldMetadata); } }