Esempio n. 1
0
        public void Test47278()
        {
            XSSFWorkbook       wb    = (XSSFWorkbook)_testDataProvider.CreateWorkbook();
            XSSFSheet          sheet = (XSSFSheet)wb.CreateSheet();
            IRow               row   = sheet.CreateRow(0);
            SharedStringsTable sst   = wb.GetSharedStringSource();

            Assert.AreEqual(0, sst.Count);

            //case 1. cell.SetCellValue(new XSSFRichTextString((String)null));
            ICell cell_0           = row.CreateCell(0);
            XSSFRichTextString str = new XSSFRichTextString((String)null);

            Assert.IsNull(str.String);
            cell_0.SetCellValue(str);
            Assert.AreEqual(0, sst.Count);
            Assert.AreEqual(CellType.Blank, cell_0.CellType);

            //case 2. cell.SetCellValue((String)null);
            ICell cell_1 = row.CreateCell(1);

            cell_1.SetCellValue((String)null);
            Assert.AreEqual(0, sst.Count);
            Assert.AreEqual(CellType.Blank, cell_1.CellType);
        }
    public void Test_TableSharedStringsWithNull()
    {
        var t = new SharedStringsTable
        {
            String1 = null,
            String2 = "string",
            String3 = (string)null,
        };

        byte[] destination = new byte[1024];

        var serializer   = FlatBufferSerializer.Default.Compile <SharedStringsTable>();
        int bytesWritten = serializer.Write(default(SpanWriter), destination, t);

        byte[] expectedBytes = new byte[]
        {
            4, 0, 0, 0,             // offset to table start
            248, 255, 255, 255,     // soffset to vtable.
            12, 0, 0, 0,            // uoffset to string
            8, 0, 8, 0,             // vtable length, table length
            0, 0, 4, 0,             // vtable(0), vtable(1)
            6, 0, 0, 0,             // string length
            (byte)'s', (byte)'t', (byte)'r', (byte)'i',
            (byte)'n', (byte)'g', 0 // null terminator.
        };

        Assert.True(expectedBytes.AsSpan().SequenceEqual(destination.AsSpan().Slice(0, bytesWritten)));
    }
Esempio n. 3
0
 /**
  * Construct a XSSFCell.
  *
  * @param row the parent row.
  * @param cell the xml bean Containing information about the cell.
  */
 public XSSFCell(XSSFRow row, CT_Cell cell)
 {
     _cell = cell;
     _row  = row;
     if (cell.r != null)
     {
         _cellNum = new CellReference(cell.r).Col;
     }
     _sharedStringSource = ((XSSFWorkbook)row.Sheet.Workbook).GetSharedStringSource();
     _stylesSource       = ((XSSFWorkbook)row.Sheet.Workbook).GetStylesSource();
 }
Esempio n. 4
0
 /**
  * Construct a XSSFCell.
  *
  * @param row the parent row.
  * @param cell the xml bean Containing information about the cell.
  */
 public XSSFCell(XSSFRow row, CT_Cell cell)
 {
     _cell = cell;
     _row  = row;
     if (cell.r != null)
     {
         _cellNum = new CellReference(cell.r).Col;
     }
     else
     {
         int prevNum = row.LastCellNum;
         if (prevNum != -1)
         {
             _cellNum = (row as XSSFRow).GetCell(prevNum - 1, MissingCellPolicy.RETURN_NULL_AND_BLANK).ColumnIndex + 1;
         }
     }
     _sharedStringSource = ((XSSFWorkbook)row.Sheet.Workbook).GetSharedStringSource();
     _stylesSource       = ((XSSFWorkbook)row.Sheet.Workbook).GetStylesSource();
 }
Esempio n. 5
0
        public void UseSharedStringsTable()
        {
            SXSSFWorkbook wb = new SXSSFWorkbook(null, 10, false, true);

            SharedStringsTable sss = POITestCase.GetFieldValue <SharedStringsTable, SXSSFWorkbook>(typeof(SXSSFWorkbook), wb, typeof(SharedStringsTable), "_sharedStringSource");

            Assert.IsNotNull(sss);

            IRow row = wb.CreateSheet("S1").CreateRow(0);

            row.CreateCell(0).SetCellValue("A");
            row.CreateCell(1).SetCellValue("B");
            row.CreateCell(2).SetCellValue("A");

            XSSFWorkbook xssfWorkbook = SXSSFITestDataProvider.instance.WriteOutAndReadBack(wb) as XSSFWorkbook;

            sss = POITestCase.GetFieldValue <SharedStringsTable, SXSSFWorkbook>(typeof(SXSSFWorkbook), wb, typeof(SharedStringsTable), "_sharedStringSource");
            Assert.AreEqual(2, sss.UniqueCount);
            Assert.IsTrue(wb.Dispose());

            ISheet sheet1 = xssfWorkbook.GetSheetAt(0);

            Assert.AreEqual("S1", sheet1.SheetName);
            Assert.AreEqual(1, sheet1.PhysicalNumberOfRows);
            row = sheet1.GetRow(0);
            Assert.IsNotNull(row);
            ICell cell = row.GetCell(0);

            Assert.IsNotNull(cell);
            Assert.AreEqual("A", cell.StringCellValue);
            cell = row.GetCell(1);
            Assert.IsNotNull(cell);
            Assert.AreEqual("B", cell.StringCellValue);
            cell = row.GetCell(2);
            Assert.IsNotNull(cell);
            Assert.AreEqual("A", cell.StringCellValue);

            xssfWorkbook.Close();
            wb.Close();
        }
    public void Test_TableSharedStringsWithEviction()
    {
        var t = new SharedStringsTable
        {
            String1 = "string",
            String2 = "foo",
            String3 = "string",
        };

        byte[] destination = new byte[1024];
        var    serializer  = FlatBufferSerializer.Default.Compile <SharedStringsTable>()
                             .WithSettings(new SerializerSettings
        {
            SharedStringWriterFactory = () => new SharedStringWriter(1)
        });

        int bytesWritten = serializer.Write(default(SpanWriter), destination, t);

        byte[] expectedBytes = new byte[]
        {
            4, 0, 0, 0,                         // offset to table start
            240, 255, 255, 255,                 // soffset to vtable.
            24, 0, 0, 0,                        // uoffset to string 1
            32, 0, 0, 0,                        // uoffset to string 2
            36, 0, 0, 0,                        // uoffset to string 3
            10, 0, 16, 0,                       // vtable length, table length
            12, 0, 8, 0,                        // vtable(0), vtable(1)
            4, 0, 0, 0,                         // vtable(2), padding
            6, 0, 0, 0,                         // string0 length
            (byte)'s', (byte)'t', (byte)'r', (byte)'i',
            (byte)'n', (byte)'g', 0, 0,         // null terminator + 1 byte padding
            3, 0, 0, 0,                         // string1 length
            (byte)'f', (byte)'o', (byte)'o', 0, // string1 + null terminator
            6, 0, 0, 0,
            (byte)'s', (byte)'t', (byte)'r', (byte)'i',
            (byte)'n', (byte)'g', 0 // null terminator
        };

        Assert.True(expectedBytes.AsSpan().SequenceEqual(destination.AsSpan().Slice(0, bytesWritten)));
    }
    public void Test_TableSharedStringsWithoutEviction()
    {
        var t = new SharedStringsTable
        {
            String1 = "string",
            String2 = "foo",
            String3 = "string",
        };

        byte[] destination = new byte[1024];
        var    serializer  = FlatBufferSerializer.Default.Compile <SharedStringsTable>();

        int bytesWritten = serializer.Write(default(SpanWriter), destination, t);

        byte[] stringBytes = Encoding.UTF8.GetBytes("string");

        // We can't predict ordering since there is hashing under the hood.
        int firstIndex  = destination.AsSpan().IndexOf(stringBytes);
        int secondIndex = destination.AsSpan().Slice(0, firstIndex + 1).IndexOf(stringBytes);

        Assert.Equal(-1, secondIndex);
    }
Esempio n. 8
0
        /// <summary>
        /// Currently only supports writing not reading. E.g. the number of rows returned from a worksheet will be wrong etc.
        /// </summary>
        /// <param name="workbook"></param>
        /// <param name="rowAccessWindowSize"></param>
        /// <param name="compressTmpFiles"></param>
        /// <param name="useSharedStringsTable"></param>
        public SXSSFWorkbook(XSSFWorkbook workbook, int rowAccessWindowSize, bool compressTmpFiles, bool useSharedStringsTable)
        {
            RandomAccessWindowSize = rowAccessWindowSize;

            _compressTmpFiles = compressTmpFiles;

            if (workbook == null)
            {
                XssfWorkbook        = new XSSFWorkbook();
                _sharedStringSource = useSharedStringsTable ? XssfWorkbook.GetSharedStringSource() : null;
            }
            else
            {
                XssfWorkbook        = workbook;
                _sharedStringSource = useSharedStringsTable ? XssfWorkbook.GetSharedStringSource() : null;
                var numberOfSheets = XssfWorkbook.NumberOfSheets;
                for (int i = 0; i < numberOfSheets; i++)
                {
                    XSSFSheet sheet = (XSSFSheet)XssfWorkbook.GetSheetAt(i);
                    CreateAndRegisterSXSSFSheet(sheet);
                }
            }
        }
Esempio n. 9
0
        private void OnWorkbookCreate()
        {
            this.workbook = new CT_Workbook();
            this.workbook.AddNewWorkbookPr().date1904 = false;
            this.workbook.AddNewBookViews().AddNewWorkbookView().activeTab = 0U;
            this.workbook.AddNewSheets();
            CT_ExtendedProperties underlyingProperties = this.GetProperties().GetExtendedProperties().GetUnderlyingProperties();

            underlyingProperties.Application                = POIXMLDocument.DOCUMENT_CREATOR;
            underlyingProperties.DocSecurity                = 0;
            underlyingProperties.DocSecuritySpecified       = true;
            underlyingProperties.ScaleCrop                  = false;
            underlyingProperties.ScaleCropSpecified         = true;
            underlyingProperties.LinksUpToDate              = false;
            underlyingProperties.LinksUpToDateSpecified     = true;
            underlyingProperties.HyperlinksChanged          = false;
            underlyingProperties.HyperlinksChangedSpecified = true;
            underlyingProperties.SharedDoc                  = false;
            underlyingProperties.SharedDocSpecified         = true;
            this.sharedStringSource = (SharedStringsTable)this.CreateRelationship((POIXMLRelation)XSSFRelation.SHARED_STRINGS, (POIXMLFactory)XSSFFactory.GetInstance());
            this.stylesSource       = (StylesTable)this.CreateRelationship((POIXMLRelation)XSSFRelation.STYLES, (POIXMLFactory)XSSFFactory.GetInstance());
            this.namedRanges        = new List <XSSFName>();
            this.sheets             = new List <XSSFSheet>();
        }
Esempio n. 10
0
        public void TestReadWrite()
        {
            XSSFWorkbook       wb   = XSSFTestDataSamples.OpenSampleWorkbook("sample.xlsx");
            SharedStringsTable sst1 = wb.GetSharedStringSource();

            //Serialize, read back and compare with the original
            SharedStringsTable sst2 = XSSFTestDataSamples.WriteOutAndReadBack(wb).GetSharedStringSource();

            Assert.AreEqual(sst1.Count, sst2.Count);
            Assert.AreEqual(sst1.UniqueCount, sst2.UniqueCount);

            IList <CT_Rst> items1 = sst1.Items;
            IList <CT_Rst> items2 = sst2.Items;

            Assert.AreEqual(items1.Count, items2.Count);
            for (int i = 0; i < items1.Count; i++)
            {
                CT_Rst st1 = items1[i];
                CT_Rst st2 = items2[i];
                Assert.AreEqual(st1.ToString(), st2.ToString());
            }

            Assert.IsNotNull(XSSFTestDataSamples.WriteOutAndReadBack(wb));
        }
Esempio n. 11
0
 public SheetDataWriter(SharedStringsTable sharedStringsTable) : this()
 {
     _sharedStringSource = sharedStringsTable;
 }
Esempio n. 12
0
 internal override void OnDocumentRead()
 {
     try
     {
         this.workbook = WorkbookDocument.Parse(this.GetPackagePart().GetInputStream()).Workbook;
         Dictionary <string, XSSFSheet> dictionary = new Dictionary <string, XSSFSheet>();
         foreach (POIXMLDocumentPart relation in this.GetRelations())
         {
             if (relation is SharedStringsTable)
             {
                 this.sharedStringSource = (SharedStringsTable)relation;
             }
             else if (relation is StylesTable)
             {
                 this.stylesSource = (StylesTable)relation;
             }
             else if (relation is ThemesTable)
             {
                 this.theme = (ThemesTable)relation;
             }
             else if (relation is CalculationChain)
             {
                 this.calcChain = (CalculationChain)relation;
             }
             else if (relation is MapInfo)
             {
                 this.mapInfo = (MapInfo)relation;
             }
             else if (relation is XSSFSheet)
             {
                 dictionary.Add(relation.GetPackageRelationship().Id, (XSSFSheet)relation);
             }
         }
         if (this.stylesSource != null)
         {
             this.stylesSource.SetTheme(this.theme);
         }
         if (this.sharedStringSource == null)
         {
             this.sharedStringSource = (SharedStringsTable)this.CreateRelationship((POIXMLRelation)XSSFRelation.SHARED_STRINGS, (POIXMLFactory)XSSFFactory.GetInstance());
         }
         this.sheets = new List <XSSFSheet>(dictionary.Count);
         foreach (CT_Sheet ctSheet in this.workbook.sheets.sheet)
         {
             XSSFSheet xssfSheet = dictionary[ctSheet.id];
             if (xssfSheet == null)
             {
                 XSSFWorkbook.logger.Log(5, (object)("Sheet with name " + ctSheet.name + " and r:id " + ctSheet.id + " was defined, but didn't exist in package, skipping"));
             }
             else
             {
                 xssfSheet.sheet = ctSheet;
                 xssfSheet.OnDocumentRead();
                 this.sheets.Add(xssfSheet);
             }
         }
         this.namedRanges = new List <XSSFName>();
         if (!this.workbook.IsSetDefinedNames())
         {
             return;
         }
         foreach (CT_DefinedName name in this.workbook.definedNames.definedName)
         {
             this.namedRanges.Add(new XSSFName(name, this));
         }
     }
     catch (XmlException ex)
     {
         throw new POIXMLException((Exception)ex);
     }
 }
Esempio n. 13
0
        public void TestCreateNew()
        {
            SharedStringsTable sst = new SharedStringsTable();

            CT_Rst st;
            int    idx;

            // Check defaults
            Assert.IsNotNull(sst.Items);
            Assert.AreEqual(0, sst.Items.Count);
            Assert.AreEqual(0, sst.Count);
            Assert.AreEqual(0, sst.UniqueCount);

            st   = new CT_Rst();
            st.t = ("Hello, World!");

            idx = sst.AddEntry(st);
            Assert.AreEqual(0, idx);
            Assert.AreEqual(1, sst.Count);
            Assert.AreEqual(1, sst.UniqueCount);

            //add the same entry again
            idx = sst.AddEntry(st);
            Assert.AreEqual(0, idx);
            Assert.AreEqual(2, sst.Count);
            Assert.AreEqual(1, sst.UniqueCount);

            //and again
            idx = sst.AddEntry(st);
            Assert.AreEqual(0, idx);
            Assert.AreEqual(3, sst.Count);
            Assert.AreEqual(1, sst.UniqueCount);

            st   = new CT_Rst();
            st.t = ("Second string");

            idx = sst.AddEntry(st);
            Assert.AreEqual(1, idx);
            Assert.AreEqual(4, sst.Count);
            Assert.AreEqual(2, sst.UniqueCount);

            //add the same entry again
            idx = sst.AddEntry(st);
            Assert.AreEqual(1, idx);
            Assert.AreEqual(5, sst.Count);
            Assert.AreEqual(2, sst.UniqueCount);

            st = new CT_Rst();
            CT_RElt   r  = st.AddNewR();
            CT_RPrElt pr = r.AddNewRPr();

            pr.AddNewColor().SetRgb(new byte[] { (byte)0xFF, 0, 0 }); //red
            pr.AddNewI().val = (true);                                //bold
            pr.AddNewB().val = (true);                                //italic
            r.t = ("Second string");

            idx = sst.AddEntry(st);
            Assert.AreEqual(2, idx);
            Assert.AreEqual(6, sst.Count);
            Assert.AreEqual(3, sst.UniqueCount);

            idx = sst.AddEntry(st);
            Assert.AreEqual(2, idx);
            Assert.AreEqual(7, sst.Count);
            Assert.AreEqual(3, sst.UniqueCount);

            //OK. the sst table is Filled, check the contents
            Assert.AreEqual(3, sst.Items.Count);
            Assert.AreEqual("Hello, World!", new XSSFRichTextString(sst.GetEntryAt(0)).ToString());
            Assert.AreEqual("Second string", new XSSFRichTextString(sst.GetEntryAt(1)).ToString());
            Assert.AreEqual("Second string", new XSSFRichTextString(sst.GetEntryAt(2)).ToString());
        }
Esempio n. 14
0
 /**
  * @param sharedStringsTable the shared strings table, or null if inline text is used
  */
 public GZIPSheetDataWriter(SharedStringsTable sharedStringsTable) : base(sharedStringsTable)
 {
 }