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 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 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(); }