예제 #1
0
        public void CreateSpreadsheetFromIEnumerable()
        {
            DataSourceConverter <DisneyCharacter> converter = new DataSourceConverter <DisneyCharacter>();

            converter.Fields.Add(new AdapterBoundField <DisneyCharacter>("Name", "Character Name"));
            converter.Fields.Add(new AdapterBoundField <DisneyCharacter>("Age", null, "{0:###}"));
            XlsDocument doc = new XlsDocument();

            converter.CreateWorksheet(doc, _dataList, "testing");

            ValidateXlsToIList(doc);
        }
예제 #2
0
        public void CreateWorksheetWithDataTable()
        {
            DataSourceConverter <DataRow> converter = new DataSourceConverter <DataRow>();

            converter.Fields.Add(new AdapterBoundField <DataRow>("Name", "Character Name"));
            converter.Fields.Add(new AdapterBoundField <DataRow>("Age", "Age", "{0:###}"));
            XlsDocument doc = new XlsDocument();

            converter.CreateWorksheet(doc, _data, _data.TableName);

            Assert.AreEqual(1, doc.Workbook.Worksheets.Count);
            ValidateWorksheetFromDataTable(doc.Workbook.Worksheets[0], _data);
        }
예제 #3
0
        public void CreateSpreadSheetFromObjectDataSource()
        {
            DataSourceConverter <DisneyCharacter> converter = new DataSourceConverter <DisneyCharacter>();

            converter.Fields.Add(new AdapterBoundField <DisneyCharacter>("Name", "Character Name"));
            converter.Fields.Add(new AdapterBoundField <DisneyCharacter>("Age", null, "{0:###}"));
            XlsDocument      doc = new XlsDocument();
            ObjectDataSource ods = new ObjectDataSource("org.in2bits.MyXls.Data.DataSourceConverterTest, org.in2bits.MyXlsTests", "GetData");

            converter.CreateWorksheet(doc, ods, "testing");

            ValidateXlsToIList(doc);
        }
예제 #4
0
        public void CreateSpreadsheetWithBoldHeadersAndCallback()
        {
            DataSourceConverter <DataRow> converter = new DataSourceConverter <DataRow>();

            converter.Fields.Add(new AdapterBoundField <DataRow>("Name", "Character Name"));
            AdapterBoundField <DataRow> ageField = new AdapterBoundField <DataRow>("Age");

            // Create a delegate on the field that handles populating each row
            // this delegate is called for each row for the field in the data
            ageField.CellDataBound += delegate(object sender, AdapterFieldEventArgs <DataRow> args)
            {
                args.Cell.Value = String.Format("{0} is {1} Years Old", args.DataItem["Name"], args.DataItem["Age"]);
            };

            converter.Fields.Add(ageField);
            converter.HeaderDataBound += DataSourceConverter <DataRow> .BoldAllCells;
            XlsDocument doc = new XlsDocument();

            converter.CreateWorksheet(doc, _data, "Testing");

            // validate worksheet is created
            Assert.AreEqual(1, doc.Workbook.Worksheets.Count);
            Assert.AreEqual(_data.Rows.Count + 1, doc.Workbook.Worksheets[0].Rows.Count);

            // validate header row
            Assert.AreEqual("Character Name", doc.Workbook.Worksheets[0].Rows[1].CellAtCol(1).Value);
            Assert.IsTrue(doc.Workbook.Worksheets[0].Rows[1].CellAtCol(1).Font.Bold);
            Assert.AreEqual("Age", doc.Workbook.Worksheets[0].Rows[1].CellAtCol(2).Value);
            Assert.IsTrue(doc.Workbook.Worksheets[0].Rows[1].CellAtCol(2).Font.Bold);

            ushort i = 2;

            foreach (DataRow row in _data.Rows)
            {
                Assert.AreEqual(row["Name"], doc.Workbook.Worksheets[0].Rows[i].CellAtCol(1).Value);
                Assert.AreEqual(String.Format("{0} is {1} Years Old", row["Name"], row["Age"]),
                                doc.Workbook.Worksheets[0].Rows[i].CellAtCol(2).Value);
                i++;
            }
        }