public void AutoRemoveQuotes() { var eng = new CsvEngine(new CsvOptions("YourClass", ',', 2, 0)); DataTable dt = eng.ReadFileAsDT(TestCommon.GetPath("Good", "QuoteMode1.txt")); Assert.AreEqual("VINET", dt.Rows[0][1]); }
public static void ToCSV(string path, string[,] data) { System.Data.DataTable dt = new System.Data.DataTable(); int row = data.GetLength(0); int column = data.GetLength(1); for (int j = 0; j < column; j++) { dt.Columns.Add(data[0, j], typeof(String)); } for (int i = 0; i < row; i++) //含表头 { dt.Rows.Add(dt.NewRow()); for (int j = 0; j < column; j++) { if (!String.IsNullOrEmpty(data[i, j])) { dt.Rows[i][j] = "\"" + data[i, j].Replace("\"", "\"\"") + "\""; } } } dt.AcceptChanges(); CsvOptions options = new CsvOptions("String[,]", ',', data.GetLength(1)); CsvEngine.DataTableToCsv(dt, path, options); }
public void AutoRemoveQuotes() { var eng = new CsvEngine(new CsvOptions("YourClass", ',', 2, 0)); DataTable dt = eng.ReadFileAsDT(TestCommon.GetPath("Good", "QuoteMode1.txt")); Assert.AreEqual("VINET", dt.Rows[0][1]); }
public void DualEngine() { string file = TestCommon.GetPath("Good", "RealCsvVerticalBar2.txt"); string classname = "CustomerVerticalBar"; char delimiter = '|'; CsvEngine engine = new CsvEngine(classname, delimiter, file); CsvEngine engine2 = new CsvEngine(classname, delimiter, file); }
public void DualEngine() { string file = TestCommon.GetPath("Good", "RealCsvVerticalBar2.txt"); string classname = "CustomerVerticalBar"; char delimiter = '|'; var engine = new CsvEngine(classname, delimiter, file); var engine2 = new CsvEngine(classname, delimiter, file); }
public void ReadWithCommon() { string file = TestCommon.GetPath("Good", "RealCsvComma1.txt"); string classname = "CustomerComma"; char delimiter = ','; DataTable dt = CsvEngine.CsvToDataTable(file, classname, delimiter); Assert.AreEqual(20, dt.Rows.Count); Assert.AreEqual("CustomerID", dt.Columns[0].ColumnName); }
public DataTable LoadAndDataFile(string filePath = ApplicationConstants.WorkFilePath, char delimiter = ApplicationConstants.FileDelimiter) { var workItemsDataTables = new DataTable(); try { workItemsDataTables = CsvEngine.CsvToDataTable(filePath, delimiter); } catch (Exception exception) { _appLogRepository.Log(exception); } return(workItemsDataTables); }
public void TestRemoveQuotesFirstCellOfRow() { string file = TestCommon.GetPath("Good", "RealCsvQuoteRemoval.txt"); string classname = "CustomerComma"; char delimiter = ','; DataTable dt = CsvEngine.CsvToDataTable(file, classname, delimiter, true, true); Assert.AreEqual(2, dt.Rows.Count); string firstColumn = dt.Rows[0].ItemArray[0].ToString(); Assert.AreEqual("NormalizeAddress", dt.Columns[0].ColumnName); Assert.AreEqual("Y", firstColumn); }
public void GivenDataTableWithColumns_WhenHeaderExportOptionIsOn_ThenHeaderIsInFile() { DataTable presidents = TwoPresidents(); var optionsWithHeader = new CsvOptions("Temp", ',', 2) { IncludeHeaderNames = true }; string path = TestCommon.GetPath("Good", "presidents.txt"); CsvEngine.DataTableToCsv(presidents, path, optionsWithHeader); string[] lines = File.ReadAllLines(path); Assert.That(lines.Length, Is.EqualTo(3)); }
private void RunTest(string file, char delimiter, char delimiterHdr, string classname) { CsvOptions options = new CsvOptions(classname, delimiter, file); options.HeaderDelimiter = delimiterHdr; CsvEngine engine = new CsvEngine(options); Assert.AreEqual(classname, engine.RecordType.Name); DataTable dt = engine.ReadFileAsDT(file); Assert.AreEqual(20, dt.Rows.Count); Assert.AreEqual(20, engine.TotalRecords); Assert.AreEqual(0, engine.ErrorManager.ErrorCount); Assert.AreEqual("CustomerID", dt.Columns[0].ColumnName); }
public void GivenFileWithHeader_WhenImportingWithExportHeaders_ThenImportWillSkipFirstLine() { // Arrange DataTable presidents = TwoPresidents(); var optionsWithHeader = new CsvOptions("Temp", ',', 2) { IncludeHeaderNames = true }; string path = TestCommon.GetPath("Good", "presidents.txt"); CsvEngine.DataTableToCsv(presidents, path, optionsWithHeader); // Act DataTable freshPresidents = CsvEngine.CsvToDataTable(path, optionsWithHeader); // Assert Assert.That(freshPresidents.Columns.Count, Is.EqualTo(2)); }
static public void Export(this DataTable table, string filePath, NullSafeOfStringDictionary <DataExportTarget> targets) { string extension = Path.GetExtension(filePath); var selected = targets.First(p => p.Value == extension).Key; switch (selected) { case DataExportTarget.TXT: using (var stream = File.CreateText(filePath)) foreach (DataRow row in table.Rows) { foreach (DataColumn column in table.Columns) { stream.WriteLine($"{column.ColumnName} = {row[column]}"); } stream.WriteLine(); } break; case DataExportTarget.CSV: var options = new CsvOptions("String[,]", Globals.CSVSeparator, table.Rows.Count) { IncludeHeaderNames = true, DateFormat = "yyyy-MM-dd HH:mm", Encoding = Encoding.UTF8 }; CsvEngine.DataTableToCsv(table, filePath, options); break; case DataExportTarget.JSON: var dataset = new DataSet(Globals.AssemblyTitle); dataset.Tables.Add(table); string lines = JsonConvert.SerializeObject(dataset, Formatting.Indented); File.WriteAllText(filePath, lines, Encoding.UTF8); dataset.Tables.Clear(); dataset.Dispose(); break; default: throw new AdvNotImplementedException(selected); } }
public void ReadFileHeader2() { string file = TestCommon.GetPath("Good", "RealCsvComma1.txt"); string classname = "CustomerComma"; char delimiter = ','; var options = new CsvOptions(classname, delimiter, file); options.HeaderLines = 2; var engine = new CsvEngine(options); Assert.AreEqual(classname, engine.RecordType.Name); DataTable dt = engine.ReadFileAsDT(file); Assert.AreEqual(19, dt.Rows.Count); Assert.AreEqual(19, engine.TotalRecords); Assert.AreEqual(0, engine.ErrorManager.ErrorCount); Assert.AreEqual("CustomerID", dt.Columns[0].ColumnName); }
public void ReadFileHeader2() { string file = TestCommon.GetPath("Good", "RealCsvComma1.txt"); string classname = "CustomerComma"; char delimiter = ','; CsvOptions options = new CsvOptions(classname, delimiter, file); options.HeaderLines = 2; CsvEngine engine = new CsvEngine(options); Assert.AreEqual(classname, engine.RecordType.Name); DataTable dt = engine.ReadFileAsDT(file); Assert.AreEqual(19, dt.Rows.Count); Assert.AreEqual(19, engine.TotalRecords); Assert.AreEqual(0, engine.ErrorManager.ErrorCount); Assert.AreEqual("CustomerID", dt.Columns[0].ColumnName); }
private void RunTest(string file, char delimiter, char delimiterHdr, string classname) { var options = new CsvOptions(classname, delimiter, file); options.HeaderDelimiter = delimiterHdr; var engine = new CsvEngine(options); Assert.AreEqual(classname, engine.RecordType.Name); DataTable dt = engine.ReadFileAsDT(file); Assert.AreEqual(20, dt.Rows.Count); Assert.AreEqual(20, engine.TotalRecords); Assert.AreEqual(0, engine.ErrorManager.ErrorCount); Assert.AreEqual("CustomerID", dt.Columns[0].ColumnName); }
static void Main(string[] args) { var file = "sales.csv"; var table = CsvEngine.CsvToDataTable(file, ';'); var columnTypes = new Dictionary <string, Type>(); columnTypes.Add("Year", typeof(string)); columnTypes.Add("Month", typeof(string)); columnTypes.Add("Quantity", typeof(int)); columnTypes.Add("VAT", typeof(decimal)); columnTypes.Add("Unit_Price", typeof(decimal)); columnTypes.Add("Total_Price", typeof(decimal)); var newTable = new DataTable(); newTable.TableName = "Product Sales"; foreach (DataColumn col in table.Columns) { var t = typeof(string); if (!columnTypes.TryGetValue(col.ColumnName, out t)) { t = typeof(string); } newTable.Columns.Add(col.ColumnName, t); } foreach (DataRow row in table.Rows) { newTable.Rows.Add(row.ItemArray); } var cubeModel = CubeModel.GenerateFromDataTable(newTable); cubeModel.Dimensions.RemoveAll(p => p.Name == "Year"); cubeModel.Dimensions.RemoveAll(p => p.Name == "Month"); cubeModel.Dimensions.Add(new DimensionModel("Year", DataType.Text)); cubeModel.Dimensions.Add(new DimensionModel("Month", DataType.Text)); cubeModel.Dimensions.Add(new DimensionModel("Quarter", DataType.Text) { Expression = "Year + \" \" + Month" }); cubeModel.Measures.Add(new MeasureModel("CalcField", DataType.Integer) { Expression = "Total_Price * 100" }); foreach (var item in cubeModel.Dimensions) { item.Caption = item.Name.Replace('_', ' '); } foreach (var item in cubeModel.Measures) { item.Caption = item.Name.Replace('_', ' '); } cubeModel.Measures.Single(p => p.Name == "Total_Price").Caption = "Total Price"; cubeModel.Measures.Single(p => p.Name == "Unit_Price").Caption = "Unit Price"; cubeModel.Measures.Single(p => p.Name == "Total_Price").DefaultFormat = new DataFormat() { FormatString = "C" }; cubeModel.Measures.Single(p => p.Name == "Unit_Price").DefaultFormat = new DataFormat() { FormatString = "C" }; var ds = new DataSet(); ds.Tables.Add(newTable); ds.WriteXml(cubeModel.Name + "-DataSet.xml"); DataEngine engine = new DataEngine(cubeModel); var query = new MdQuery(); query.SourceData = DynamicObject.ListFromDataTable(newTable); query.Dimensions.Add("Year"); query.Dimensions.Add("Month"); query.Dimensions.Add("Quarter"); query.Measures.Add("Unit_Price"); query.Measures.Add("Total_Price"); query.Measures.Add("CalcField"); query.Sort.Add(new JDash.InMemoryAnalytics.Engine.Sort("Quarter", JDash.InMemoryAnalytics.Engine.SortDirection.Descending)); query.Take = 5; query.Skip = 5; var result = engine.Execute(query); foreach (var header in result.Headers) { Console.Write(header.Caption + "\t"); } Console.WriteLine(); foreach (var row in result.Items) { foreach (var cell in row) { Console.Write(cell.FormattedValue + "\t"); } Console.WriteLine(); } Console.ReadLine(); }
public InsertResult Post([FromBody] List <MeterReading> readings) { CsvEngine csvEngine = new CsvEngine(); return(csvEngine.InputMeterReadings(readings, DataRepo)); }