Beispiel #1
0
        public void Write()
        {
            using (IExcelSpreadsheet spreadsheet = ExcelSpreadsheet.CreateNew(Filename))
            {
                using (IWorksheetWriter writer = spreadsheet.WriteToWorksheet("UnitTests"))
                {
                    Assert.That(writer.GetCurrentCell().Address, Is.EqualTo("A1"));
                    writer.Write("One");
                    Assert.That(writer.GetCurrentCell().Address, Is.EqualTo("B1"));

                    writer.MoveTo("D3");
                    writer.Write("New value at D3");
                    Assert.That(writer.GetCurrentCell().Address, Is.EqualTo("E3"));
                }
            }

            using (IExcelSpreadsheet spreadsheet = ExcelSpreadsheet.OpenReadOnly(Filename))
            {
                IWorksheetReader reader = spreadsheet.ReadWorksheet("UnitTests");
                reader.MoveTo("A1");
                Assert.That(reader.Read(), Is.EqualTo("One"));
                Assert.That(reader.GetCurrentCell().Address, Is.EqualTo("B1"));
                reader.MoveTo("D3");
                Assert.That(reader.Read(), Is.EqualTo("New value at D3"));
                Assert.That(reader.GetCurrentCell().Address, Is.EqualTo("E3"));
            }
        }
        private int ValidateHeader(IWorksheetReader worksheetReader)
        {
            worksheetReader.MoveTo("A1");
            int depth = 0;

            foreach (string expected in header)
            {
                string address = worksheetReader.GetCurrentCell().Address;
                string column  = worksheetReader.Read();
                if (column == string.Empty)
                {
                    return(depth);
                }

                if (column != expected)
                {
                    string message = string.Format("Invalid header at {0}: Expected: '{1}' Actual: '{2}'", address, expected, column);
                    throw new InvalidOperationException(message);
                }
                depth++;
            }
            if (depth == 9)
            {
                List <string> nextColumns = worksheetReader.ReadRow();
                {
                    if (nextColumns.Count > 0)
                    {
                        string message = string.Format("Invalid header. No more columns expected but found {0}",
                                                       string.Join(",", nextColumns));
                        throw new InvalidOperationException(message);
                    }
                }
            }
            return(depth);
        }
Beispiel #3
0
        public void MoveToAndWrite()
        {
            using (IExcelSpreadsheet spreadsheet = ExcelSpreadsheet.CreateNew(Filename))
            {
                using (IWorksheetWriter writer = spreadsheet.WriteToWorksheet("UnitTests"))
                {
                    Assert.That(writer.GetCurrentCell().Address, Is.EqualTo("A1"));
                    writer.MoveTo("B2");
                    writer.WriteRow(new List <string> {
                        "One", "Two", "Three"
                    });
                    Assert.That(writer.GetCurrentCell().Address, Is.EqualTo("B3"));
                    writer.WriteRow(new List <string> {
                        "Four", "Five", "Six"
                    });
                    Assert.That(writer.GetCurrentCell().Address, Is.EqualTo("B4"));
                }
            }

            using (IExcelSpreadsheet spreadsheet = ExcelSpreadsheet.OpenReadOnly(Filename))
            {
                using (IWorksheetReader reader = spreadsheet.ReadWorksheet("UnitTests"))
                {
                    Assert.That(reader.GetCurrentCell().Address, Is.EqualTo("A1"));
                    Assert.That(reader.ReadRow(), Is.EquivalentTo(new List <string>()));

                    reader.MoveTo("B2");
                    Assert.That(reader.GetCurrentCell().Address, Is.EqualTo("B2"));
                    Assert.That(reader.ReadRow(), Is.EquivalentTo(new List <string> {
                        "One", "Two", "Three"
                    }));
                    Assert.That(reader.GetCurrentCell().Address, Is.EqualTo("B3"));
                    Assert.That(reader.ReadRow(), Is.EquivalentTo(new List <string> {
                        "Four", "Five", "Six"
                    }));
                }
            }
        }
        private void ValidateHeader(IWorksheetReader worksheetReader)
        {
            worksheetReader.MoveTo("A1");

            foreach (string expected in header)
            {
                string address = worksheetReader.GetCurrentCell().Address;
                string column  = worksheetReader.Read();
                if (column != expected)
                {
                    string message = string.Format("Invalid header at {0}: Expected: '{1}' Actual: '{2}'", address, expected, column);
                    throw new InvalidOperationException(message);
                }
            }
        }
        private int ValidateHeader(IWorksheetReader worksheetReader)
        {
            worksheetReader.MoveTo("A1");
            int depth = 0;

            foreach (string expected in header)
            {
                string address = worksheetReader.GetCurrentCell().Address;
                string column = worksheetReader.Read();
                if (column == string.Empty)
                {
                    return depth;
                }

                if (column != expected)
                {
                    string message = string.Format("Invalid header at {0}: Expected: '{1}' Actual: '{2}'", address, expected, column);
                    throw new InvalidOperationException(message);
                }
                depth++;
            }
            if (depth == 9)
            {
                List<string> nextColumns = worksheetReader.ReadRow();
                {
                    if (nextColumns.Count > 0)
                    {
                        string message = string.Format("Invalid header. No more columns expected but found {0}",
                                                       string.Join(",", nextColumns));
                        throw new InvalidOperationException(message);
                    }
                }
            }
            return depth;
        }
        private void ValidateHeader(IWorksheetReader worksheetReader)
        {
            worksheetReader.MoveTo("A1");

            foreach (string expected in header)
            {
                string address = worksheetReader.GetCurrentCell().Address;
                string column = worksheetReader.Read();
                if (column != expected)
                {
                    string message = string.Format("Invalid header at {0}: Expected: '{1}' Actual: '{2}'", address, expected, column);
                    throw new InvalidOperationException(message);
                }
            }
        }
Beispiel #7
0
        public void GetCurrentCellAfterMoveRowColumn()
        {
            WriteTestValues();
            using (IExcelSpreadsheet spreadsheet = ExcelSpreadsheet.OpenReadOnly(Filename))
            {
                IWorksheetReader reader = spreadsheet.ReadWorksheet("UnitTests");
                Assert.That(reader.GetCurrentCell().Address, Is.EqualTo("A1"));
                Assert.That(reader.GetCurrentCell().Row, Is.EqualTo(1));
                Assert.That(reader.GetCurrentCell().Column, Is.EqualTo(1));
                Assert.That(reader.GetCurrentCell().Value, Is.EqualTo("A-1"));

                reader.MoveTo(2, 1);

                Assert.That(reader.GetCurrentCell().Address, Is.EqualTo("A2"));
                Assert.That(reader.GetCurrentCell().Row, Is.EqualTo(2));
                Assert.That(reader.GetCurrentCell().Column, Is.EqualTo(1));
                Assert.That(reader.GetCurrentCell().Value, Is.EqualTo("A-2"));

                reader.MoveTo(1, 3);

                Assert.That(reader.GetCurrentCell().Address, Is.EqualTo("C1"));
                Assert.That(reader.GetCurrentCell().Row, Is.EqualTo(1));
                Assert.That(reader.GetCurrentCell().Column, Is.EqualTo(3));
                Assert.That(reader.GetCurrentCell().Value, Is.EqualTo("C-1"));
            }
        }