Exemple #1
0
        public WorkbookStream EncryptWorkbookStream(WorkbookStream wbs, string password = XorObfuscation.DefaultPassword)
        {
            WorkbookStream encryptedWbs = new WorkbookStream(TransformWorkbookBytes(wbs.ToBytes(), ObfuscationMode.Encrypt, password));

            encryptedWbs = encryptedWbs.FixBoundSheetOffsets();
            return(encryptedWbs);
        }
        public void TestConstructorAndToBytes()
        {
            byte[]         wbBytes = TestHelpers.GetTemplateMacroBytes();
            WorkbookStream wbs     = new WorkbookStream(wbBytes);

            byte[] parsedBytes = wbs.ToBytes();

            Assert.AreEqual(wbBytes.Length, parsedBytes.Length);
            Assert.AreEqual(wbBytes, parsedBytes);
        }
        public void TestAddingSheetRecord()
        {
            byte[]         wbBytes = TestHelpers.GetTemplateMacroBytes();
            WorkbookStream wbs     = new WorkbookStream(wbBytes);

            BoundSheet8 bs8 = new BoundSheet8(BoundSheet8.HiddenState.Visible, BoundSheet8.SheetType.Macrosheet, "MyMacroSheet");
            BoundSheet8 correctOffsetBs8 = ((BiffRecord)bs8.Clone()).AsRecordType <BoundSheet8>();

            BoundSheet8 oldSheetRecord = wbs.GetAllRecordsByType <BoundSheet8>().First();
            BoundSheet8 newSheetRecord = ((BiffRecord)oldSheetRecord.Clone()).AsRecordType <BoundSheet8>();

            // bs8.lbPlyPos = (uint) (oldSheetRecord.lbPlyPos + bs8.GetBytes().Length);

            List <BOF> bofRecords     = wbs.GetAllRecordsByType <BOF>();
            BOF        spreadSheetBOF = bofRecords.Last();



            // newSheetRecord.lbPlyPos = bs8.lbPlyPos;
            long offset = wbs.GetRecordByteOffset(spreadSheetBOF);

            bs8.lbPlyPos = oldSheetRecord.lbPlyPos;
            wbs          = wbs.InsertRecord(bs8, oldSheetRecord);
            offset       = wbs.GetRecordByteOffset(spreadSheetBOF);


            correctOffsetBs8.lbPlyPos = (uint)offset;
            newSheetRecord.lbPlyPos   = (uint)offset;

            wbs = wbs.ReplaceRecord(bs8, correctOffsetBs8);
            wbs = wbs.ReplaceRecord(oldSheetRecord, newSheetRecord);

            ExcelDocWriter writer = new ExcelDocWriter();

            writer.WriteDocument(TestHelpers.AssemblyDirectory + Path.DirectorySeparatorChar + "testbook.xls", wbs.ToBytes());
        }
        public void TestChangeLabel()
        {
            WorkbookStream macroWorkbookStream = new WorkbookStream(TestHelpers.GetMacroTestBytes());
            List <Lbl>     labels = macroWorkbookStream.GetAllRecordsByType <Lbl>();

            Lbl autoOpenLbl = labels.First(l => l.fBuiltin && l.Name.Value.Equals("\u0001"));

            Lbl replaceLabelStringLbl = ((BiffRecord)autoOpenLbl.Clone()).AsRecordType <Lbl>();

            replaceLabelStringLbl.SetName(new XLUnicodeStringNoCch("Auto_Open", true));
            replaceLabelStringLbl.fBuiltin = false;

            var cloneLabel  = ((BiffRecord)replaceLabelStringLbl.Clone()).AsRecordType <Lbl>();
            var cBytes      = cloneLabel.GetBytes();
            var rLabelBytes = replaceLabelStringLbl.GetBytes();

            Assert.AreEqual(rLabelBytes, cBytes);
            macroWorkbookStream = macroWorkbookStream.ReplaceRecord(autoOpenLbl, replaceLabelStringLbl);
            macroWorkbookStream = macroWorkbookStream.FixBoundSheetOffsets();

            ExcelDocWriter writer = new ExcelDocWriter();

            writer.WriteDocument(TestHelpers.AssemblyDirectory + Path.DirectorySeparatorChar + "changedLabel.xls", macroWorkbookStream.ToBytes());
        }
        public void TestAddMacroSheet()
        {
            byte[]         wbBytes = TestHelpers.GetTemplateMacroBytes();
            WorkbookStream wbs     = new WorkbookStream(wbBytes);

            byte[]            mtBytes             = TestHelpers.GetMacroTestBytes();
            WorkbookStream    macroWorkbookStream = new WorkbookStream(mtBytes);
            BoundSheet8       macroSheet          = new BoundSheet8(BoundSheet8.HiddenState.Visible, BoundSheet8.SheetType.Macrosheet, "MacroSheet");
            List <BOF>        macroWorkbookBofs   = macroWorkbookStream.GetAllRecordsByType <BOF>();
            BOF               LastBofRecord       = macroWorkbookBofs.Last();
            List <BiffRecord> sheetRecords        = macroWorkbookStream.GetRecordsForBOFRecord(LastBofRecord);

            byte[] sheetBytes = RecordHelper.ConvertBiffRecordsToBytes(sheetRecords);

            wbs = wbs.AddSheet(macroSheet, sheetBytes);
            ExcelDocWriter writer = new ExcelDocWriter();

            writer.WriteDocument(TestHelpers.AssemblyDirectory + Path.DirectorySeparatorChar + "addedsheet.xls", wbs.ToBytes());
        }
        public void TestNeuterCells()
        {
            WorkbookStream wbs = TestHelpers.GetBuiltinHiddenLblSheet();

            WorkbookEditor wbe = new WorkbookEditor(wbs);

            wbs = wbe.NeuterAutoOpenCells();

            Formula            autoOpenCell     = wbs.GetAllRecordsByType <Formula>().First();
            List <AbstractPtg> openCellPtgStack = autoOpenCell.ptgStack.ToList();

            Assert.AreEqual(typeof(PtgFunc), openCellPtgStack[0].GetType());
            Assert.AreEqual(typeof(PtgConcat), openCellPtgStack.Last().GetType());

            PtgFunc firstItem = (PtgFunc)openCellPtgStack[0];

            Assert.AreEqual(FtabValues.HALT, firstItem.Ftab);

            ExcelDocWriter writer = new ExcelDocWriter();

            writer.WriteDocument(TestHelpers.AssemblyDirectory + Path.DirectorySeparatorChar + "neutered-sheet.xls", wbs.ToBytes());
        }