private async Task ReadAll() { do { int rowNr = 0; TypeInfo typeInfo = new TypeInfo(typeof(TOutput)); while (ExcelDataReader.Read()) { if (ExcelDataReader.VisibleState != "visible") { continue; } if (HasSheetName && ExcelDataReader.Name != SheetName) { continue; } rowNr++; if (HasRange && rowNr > Range.EndRowIfSet) { break; } if (HasRange && rowNr < Range.StartRow) { continue; } TOutput row = ParseDataRow(typeInfo); await Buffer.SendAsync(row); } } while (ExcelDataReader.NextResult()); }
public void ReadTest() { using (var reader = new ExcelDataReader(@"test.xlsx")) { reader.Read(); Assert.AreEqual(381728, reader.GetInt32(0)); Assert.AreEqual(381728, reader.GetInt64(0)); Assert.AreEqual("Mr Brown", reader.GetString(1)); Assert.AreEqual("Mr Brown", reader[1]); Assert.AreEqual("Mr Brown", reader["Name"]); Assert.AreEqual(new DateTime(1983, 3, 27, 6, 55, 0), reader.GetDateTime(2)); Assert.AreEqual(new Guid("6E2BF784-F116-494A-916D-9DFF9B2A2AA0"), reader.GetGuid(3)); Assert.AreEqual(32, reader.GetInt16(4)); Assert.AreEqual(32, reader.GetInt32(4)); Assert.AreEqual(32, reader.GetInt64(4)); Assert.AreEqual(32, reader.GetByte(4)); Assert.AreEqual(917.68m, reader.GetDecimal(5)); Assert.AreEqual(917.68d, reader.GetDouble(5)); Assert.AreEqual(917.68f, reader.GetFloat(5)); Assert.AreEqual(true, reader.GetBoolean(6)); } }
protected override void ReadAll() { do { int rowNr = 0; while (ExcelDataReader.Read()) { if (ExcelDataReader.VisibleState != "visible") { continue; } if (HasSheetName && ExcelDataReader.Name != SheetName) { continue; } rowNr++; if (HasRange && rowNr > Range.EndRowIfSet) { break; } if (HasRange && rowNr < Range.StartRow) { continue; } try { if (!HasNoHeader && !IsHeaderRead) { ParseHeader(); } else { TOutput row = ParseDataRow(); if (row == null && IgnoreBlankRows) { continue; } else if (row == null && !IgnoreBlankRows) { break; } else if (row != null) { Buffer.SendAsync(row).Wait(); LogProgress(); } } } catch (Exception e) { if (!ErrorHandler.HasErrorBuffer) { throw e; } ErrorHandler.Send(e, $"File: {Uri} -- Sheet: {SheetName ?? ""} -- Row: {rowNr}"); } } } while (ExcelDataReader.NextResult()); }
public void GetField() { //var filePath = @"D:\Users\Scott\Source\github\cadru\tests\Cadru.Data.Tests\Plan Assignment DELETE File 20170615.xlsx"; var filePath = @"Adjustments Table Upload 20170616.xlsx"; using (var stream = File.OpenRead(filePath)) { using (var reader = new ExcelDataReader(stream)) { reader.FirstRowAsHeader = true; reader.Read(); var sheetName = reader.CurrentSheetName; reader.Read(); var empty = this.IsRowEmpty(reader, 0, Enumerable.Empty <int>()); var fiscalYear = reader.Field <int>(0); var empId = reader.Field <string>(1); var startDate = reader.Field <DateTime>(2); var status = reader.Field <int?>(6); var monthlyRate = reader.Field <decimal?>(7); var orgUnit = reader.Field <string>(10); var s1 = reader.ToDelimitedString(); var s2 = reader.ToDelimitedString(true); var action = "I"; var index = -1; if (reader.FieldNames.Contains("ImportAction")) { index = reader.GetOrdinal("ImportAction"); } else if (reader.FieldNames.Contains("Action")) { index = reader.GetOrdinal("Action"); } if (index != -1) { action = reader.GetString(index) ?? "I"; } reader.Close(); } } }
public void Read_ShouldReturnFailedResultWithExpectedMessageWhenValuesInWrongFormat() { var path = TestUtils.BuildPathFor(@"DataReadersTestCases\ExcelDataReaderTestCase2.xlsx"); var reader = new ExcelDataReader(); var result = reader.Read(path); result.Data.Should().BeEmpty(); result.ErrorMessage.Should().ContainEquivalentOf("string was not in a correct format"); }
public void Read_ShouldReturnFailedResultWithExpectedMessageWhenFileNotFound() { var path = TestUtils.BuildPathFor(@"DataReadersTestCases\NotExistedFile.xlsx"); var reader = new ExcelDataReader(); var result = reader.Read(path); result.Data.Should().BeEmpty(); result.ErrorMessage.Should().ContainEquivalentOf("Could not find file"); }
private void ProcessFile(FileItem item, bool alreadyOpened = false) => this.MainWindow.Waiting(async(statusBar, callback) => { if (!alreadyOpened) { sourceList.Add(item); } if (FileContent.IsOpened(item)) { callback(); return; } ExcelDataReader reader = container.Get <ExcelDataReader>().Setup(r => { r.Initialize(item.Location, msg => this.MainWindow.Output(msg)); }); ExcelData data = await reader.Read(); FileContent.Add(item, data); callback(); });
public void Read_ShouldReturnSuccessfulResultWithExpectedDataWhenFileInProtectedView() { var path = TestUtils.BuildPathFor(@"DataReadersTestCases\ExcelDataReaderTestCase3ProtectedView.xlsx"); var data = new[] { new Dictionary <string, double> { { "one", 30.24 }, { "two", 256 }, { "three", 0.4099 } }, new Dictionary <string, double> { { "one", 30.84 }, { "two", 338 }, { "three", 0.0312 } }, new Dictionary <string, double> { { "one", 16.92 }, { "two", 357 }, { "three", 0.4993 } } }; var expectedResult = DataResult.CreateSuccessful(data); var reader = new ExcelDataReader(); var result = reader.Read(path); result.ShouldBeEquivalentTo(expectedResult); }
public void Read_ShouldReturnSuccessfulResultWithExpectedData() { var path = TestUtils.BuildPathFor(@"DataReadersTestCases\ExcelDataReaderTestCase1.xlsx"); var data = new[] { new Dictionary <string, double> { { "one", 1.1 }, { "two", 1.2 }, { "three", 1.3 } }, new Dictionary <string, double> { { "one", 2.1 }, { "two", 2.2 }, { "three", 2.3 } }, new Dictionary <string, double> { { "one", 3.1 }, { "two", 3.2 }, { "three", 3.3 } } }; var expectedResult = DataResult.CreateSuccessful(data); var reader = new ExcelDataReader(); var result = reader.Read(path); result.ShouldBeEquivalentTo(expectedResult); }