예제 #1
0
        private static void TestReadMappedSummaryTable(ITableReader tableReader)
        {
            var dict = tableReader.Rows
                       .Where(r => r["Name"] != null)
                       .ToDictionary(r => r.GetValue <string>("Name"), r => r["Value"]);

            Assert.AreEqual(7, dict.Count);

            var normalReader = new TableMappingReader <NameValue>()
                               .Map(o => o.Name)
                               .Map(o => o.Value);

            var nameValueList = normalReader.Read(tableReader);

            Assert.GreaterOrEqual(nameValueList.Count, 2);
            Assert.IsTrue(nameValueList.Any(o => o.Name != o.Value));

            var nameNameReader = new TableMappingReader <NameValue>()
                                 .Map(o => o.Name)
                                 .Map(o => o.Value, "Name");

            nameValueList = nameNameReader.Read(tableReader);
            Assert.GreaterOrEqual(nameValueList.Count, 2);
            Assert.GreaterOrEqual(nameValueList.Count(o => !string.IsNullOrEmpty(o.Name)), 2);
            Assert.IsFalse(nameValueList.Any(o => o.Name != o.Value));

            nameValueList = normalReader.Read(tableReader);
            Assert.GreaterOrEqual(nameValueList.Count, 2);
            Assert.IsTrue(nameValueList.Any(o => o.Name != o.Value));
        }
예제 #2
0
        public static IList <StringCard> GetSCardList(string excelFilePath)
        {
            var package     = new ExcelPackage(new FileInfo(excelFilePath));
            var tableReader = ExcelTableReader.ReadContiguousTableWithHeader(package.Workbook.Worksheets[1], 1);
            var pocoReader  = new TableMappingReader <StringCard>()
                              .Map(o => o.图片Id)
                              .Map(o => o.中文名)
                              .Map(o => o.效果Id)
                              .Map(o => o.英文名)
                              .Map(o => o.战力)
                              .Map(o => o.品质)
                              .Map(o => o.阵营)
                              .Map(o => o.站位区)
                              .Map(o => o.属性)
                              .Map(o => o.卡牌介绍)
                              .Map(o => o.效果)
                              .Map(o => o.稀有度)
                              .Map(o => o.排序Id);

            return(pocoReader.Read(tableReader));
        }
예제 #3
0
        public void SimplePoco()
        {
            var outPath  = GetNewOutFilePath("-simple");
            var workbook = new ExcelPackage(new FileInfo(outPath));

            var dataSetExportConfig = new DataSetExportAutoConfig();

            const string dataTableName = "One";
            const string sheetName     = "OneSheet";

            var configurator = new PocoExportConfigurator <PocoTwo>(sheetName, dataTableName);

            Expression <Func <PocoBase, int> >      refId       = o => o.Id;
            Expression <Func <PocoBase, DateTime> > refDateTime = o => o.DateTime;
            Expression <Func <PocoTwo, long?> >     refInt      = o => o.FooInt;
            // implicit conversion from float to double
            Expression <Func <PocoTwo, double?> > refFloat    = o => o.FooFloat;
            Expression <Func <PocoTwo, string> >  refString   = o => o.FooString;
            Expression <Func <PocoTwo, long?> >   refFieldInt = o => o.FieldInt;

            var idColumnSource       = PocoColumnSourceFactory.Create(refId);
            var dateTimeColumnSource = PocoColumnSourceFactory.Create(refDateTime);

            configurator
            .AddColumn(idColumnSource)
            .AddColumn(dateTimeColumnSource)
            .AddColumn(refInt)
            .AddColumn(refFloat)
            .AddColumn(refString)
            .AddColumn(refFieldInt)
            // same column via reflection; duplicate caption allowed when exporting, but not when importing
            // as the reader would not be able to choose which column to get data from
            .AddColumn <int?>(nameof(PocoTwo.FieldInt), "ReflectionFieldInt")
            // when extracted type is unknown at compile time (type parameter is object), actual type will be resolved via reflection
            .AddColumn <object>(nameof(PocoTwo.FieldInt), "ReflectionFieldIntLateType");

            Assert.AreEqual(typeof(int), configurator.Config.GetAutoColumnConfig("ReflectionFieldIntLateType").ColumnDataSource.DataType);

            dataSetExportConfig.AddSheet(configurator.Config);

            var dataSet = new DataSetAdapter();
            var data1   = Enumerable.Range(0, 100)
                          .Select(i => new PocoTwo(true))
                          .ToList();

            dataSet.Add(data1, dataTableName);

            var exporter = new DataSetToWorkbookExporter(dataSetExportConfig)
            {
                DataSet = dataSet
            };

            exporter.Export(workbook);

            workbook.Save();
            TestContext.WriteLine($"Saved {outPath}.");

            workbook.Dispose();

            workbook = new ExcelPackage(new FileInfo(outPath));

            var reader = ExcelTableReader.ReadContiguousTableWithHeader(workbook.Workbook.Worksheets["OneSheet"], 1);

            var pocoReader = new TableMappingReader <PocoTwo>();

            pocoReader
            .Map(o => o.Id)
            .Map(o => o.DateTime)
            .Map(o => o.FooInt)
            .Map(o => o.FooFloat)
            .Map(o => o.FooString)
            .Map(o => o.FieldInt);

            var readPocos = pocoReader.Read(reader);

            CheckEquality(data1, readPocos);

            workbook.Dispose();

            if (_deleteExportedFiles)
            {
                File.Delete(outPath);
            }
        }