コード例 #1
0
        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));
            }
        }
コード例 #2
0
ファイル: ExcelReaderTests.cs プロジェクト: tevfikoguz/cadru
        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();
                }
            }
        }