public ActionResult UpdateFont(int id, string family, string tags) { FontRecord newFont = new FontRecord() { }; newFont.Id = id; newFont.Family = family; newFont.Priority = 0; int i = 0; if (tags != null) { string[] stringSeparators = new string[] { "," }; string[] separatedTags; string resultTags = "["; separatedTags = tags.Split(stringSeparators, StringSplitOptions.None); foreach (var item in separatedTags) { if (i != 0) { resultTags = resultTags + ","; } resultTags = resultTags + "\"" + item + "\""; i++; } resultTags += "]"; newFont.Tags = resultTags; } _fontService.EditFont(newFont); Services.Notifier.Information(T("The font has been updated.")); return(RedirectToAction("FontList")); }
public ActionResult EditFont(int id) { FontRecord record = _fontService.GetFont(id); FontViewModel editRecord = new FontViewModel() { }; editRecord.Id = record.Id; editRecord.Family = record.Family; editRecord.FileName = record.FileName; string[] stringSeparators = new string[] { "," }; string[] separatedTags; string Tags = record.Tags.Trim(new Char[] { '[', '*', ',', ']', ' ', '.' }); Tags = Tags.Replace("\"", ""); string resultTags = ""; separatedTags = Tags.Split(stringSeparators, StringSplitOptions.None); int i = 0; foreach (var item in separatedTags) { if (i != 0) { resultTags = resultTags + "," + " "; } resultTags = resultTags + item; i++; } editRecord.Tags = resultTags; return(View(editRecord)); }
public void TestStore() { // .fontheight = c8 // .attributes = 0 // .italic = false // .strikout = false // .macoutlined= false // .macshadowed= false // .colorpalette = 7fff // .boldweight = 190 // .supersubscript = 0 // .underline = 0 // .family = 0 // .charset = 0 // .namelength = 5 // .fontname = Arial FontRecord record = new FontRecord(); record.FontHeight = ((short)0xc8); record.Attributes = ((short)0); record.ColorPaletteIndex = ((short)0x7fff); record.BoldWeight = ((short)0x190); record.SuperSubScript = ((short)0); record.Underline = ((byte)0); record.Family = ((byte)0); record.Charset = ((byte)0); record.FontName = ("Arial"); byte[] recordBytes = record.Serialize(); TestcaseRecordInputStream.ConfirmRecordEncoding(0x31, data, recordBytes); }
/// <summary> /// Clones all the style information from another /// HSSFCellStyle, onto this one. This /// HSSFCellStyle will then have all the same /// properties as the source, but the two may /// be edited independently. /// Any stylings on this HSSFCellStyle will be lost! /// The source HSSFCellStyle could be from another /// HSSFWorkbook if you like. This allows you to /// copy styles from one HSSFWorkbook to another. /// </summary> /// <param name="source">The source.</param> public void CloneStyleFrom(HSSFCellStyle source) { // First we need to clone the extended format // record _format.CloneStyleFrom(source._format); // Handle matching things if we cross workbooks if (_workbook != source._workbook) { lastDateFormat = short.MinValue; lastFormats = null; getDataFormatStringCache = null; // Then we need to clone the format string, // and update the format record for this short fmt = (short)_workbook.CreateFormat( source.GetDataFormatString() ); this.DataFormat = (fmt); // Finally we need to clone the font, // and update the format record for this FontRecord fr = _workbook.CreateNewFont(); fr.CloneStyleFrom( source._workbook.GetFontRecordAt( source.FontIndex ) ); HSSFFont font = new HSSFFont( (short)_workbook.GetFontIndex(fr), fr ); this.SetFont(font); } }
public void TestFontStuff() { InternalWorkbook wb = TestHSSFWorkbook.GetInternalWorkbook(new HSSFWorkbook()); Assert.AreEqual(4, wb.NumberOfFontRecords); Assert.AreEqual(68, wb.Records.Count); FontRecord f1 = wb.GetFontRecordAt(0); FontRecord f4 = wb.GetFontRecordAt(3); Assert.AreEqual(0, wb.GetFontIndex(f1)); Assert.AreEqual(3, wb.GetFontIndex(f4)); Assert.AreEqual(f1, wb.GetFontRecordAt(0)); Assert.AreEqual(f4, wb.GetFontRecordAt(3)); // There is no 4! new ones go in at 5 FontRecord n = wb.CreateNewFont(); Assert.AreEqual(69, wb.Records.Count); Assert.AreEqual(5, wb.NumberOfFontRecords); Assert.AreEqual(5, wb.GetFontIndex(n)); Assert.AreEqual(n, wb.GetFontRecordAt(5)); // And another FontRecord n6 = wb.CreateNewFont(); Assert.AreEqual(70, wb.Records.Count); Assert.AreEqual(6, wb.NumberOfFontRecords); Assert.AreEqual(6, wb.GetFontIndex(n6)); Assert.AreEqual(n6, wb.GetFontRecordAt(6)); // Now remove the one formerly at 5 Assert.AreEqual(70, wb.Records.Count); wb.RemoveFontRecord(n); // Check that 6 has gone to 5 Assert.AreEqual(69, wb.Records.Count); Assert.AreEqual(5, wb.NumberOfFontRecords); Assert.AreEqual(5, wb.GetFontIndex(n6)); Assert.AreEqual(n6, wb.GetFontRecordAt(5)); // Check that the earlier ones are unChanged Assert.AreEqual(0, wb.GetFontIndex(f1)); Assert.AreEqual(3, wb.GetFontIndex(f4)); Assert.AreEqual(f1, wb.GetFontRecordAt(0)); Assert.AreEqual(f4, wb.GetFontRecordAt(3)); // Finally, add another one FontRecord n7 = wb.CreateNewFont(); Assert.AreEqual(70, wb.Records.Count); Assert.AreEqual(6, wb.NumberOfFontRecords); Assert.AreEqual(6, wb.GetFontIndex(n7)); Assert.AreEqual(n7, wb.GetFontRecordAt(6)); }
public void EditFont(FontRecord font) { FontRecord record = _fontRepository.Get(f => f.Id == font.Id); record.Family = font.Family; //record.FileName = font.FileName; record.Priority = font.Priority; record.Tags = font.Tags; _fontRepository.Update(record); }
internal Font(Workbook wb, FontRecord f) : base(wb) { _height = f.FontHeight; _options = f.Options; _color = wb.Palette.GetColor(f.ColorIdx); _boldNess = f.Boldness; _escapement = f.Escapement; _underline = f.Underline; _family = f.FontFamily; _characterSet = f.CharacterSet; _fontName = f.FontName; }
public RedirectToRouteResult AddNewFont(FontViewModel model) { string errorMsg = ""; if (model.Family == null) { errorMsg += T("Font family is required.\n"); } if (!((model.TtfFile == null) || (model.WoffFile == null))) { errorMsg += T("Font file is required.\n"); } if (model.Thumbnail == null) { errorMsg += T("Thumbnail is required.\n"); } if (errorMsg.Length > 0) { Services.Notifier.Error(T(errorMsg)); return(RedirectToAction("AddFont", model)); } FontRecord newFont = new FontRecord() { }; newFont.Family = model.Family; newFont.FileName = model.FileName; newFont.Priority = 0; newFont.FontCulture = cultureUsed; int i = 0; if (model.Tags != null) { string[] stringSeparators = new string[] { "," }; string[] separatedTags; string resultTags = "["; separatedTags = model.Tags.Split(stringSeparators, StringSplitOptions.None); foreach (var item in separatedTags) { if (i != 0) { resultTags = resultTags + ","; } resultTags = resultTags + "\"" + item + "\""; i++; } resultTags += "]"; newFont.Tags = resultTags; } _fontService.AddFont(newFont); Services.Notifier.Information(T("The font has been added")); return(RedirectToAction("FontList")); }
public void TestCloneOnto() { FontRecord base1 = new FontRecord(TestcaseRecordInputStream.Create(0x31, data)); FontRecord other = new FontRecord(); other.CloneStyleFrom(base1); byte[] recordBytes = other.Serialize(); Assert.AreEqual(recordBytes.Length - 4, data.Length); for (int i = 0; i < data.Length; i++) { Assert.AreEqual(data[i], recordBytes[i + 4], "At offset " + i); } }
public void TestSameProperties() { FontRecord f1 = new FontRecord(TestcaseRecordInputStream.Create(0x31, data)); FontRecord f2 = new FontRecord(TestcaseRecordInputStream.Create(0x31, data)); Assert.IsTrue(f1.SameProperties(f2)); f2.FontName = ("Arial2"); Assert.IsFalse(f1.SameProperties(f2)); f2.FontName = ("Arial"); Assert.IsTrue(f1.SameProperties(f2)); f2.FontHeight = ((short)11); Assert.IsFalse(f1.SameProperties(f2)); f2.FontHeight = ((short)0xc8); Assert.IsTrue(f1.SameProperties(f2)); }
public void TestLoad() { FontRecord record = new FontRecord(TestcaseRecordInputStream.Create(0x31, data)); Assert.AreEqual(0xc8, record.FontHeight); Assert.AreEqual(0x00, record.Attributes); Assert.IsFalse(record.IsItalic); Assert.IsFalse(record.IsStrikeout); Assert.IsFalse(record.IsMacoutlined); Assert.IsFalse(record.IsMacshadowed); Assert.AreEqual(0x7fff, record.ColorPaletteIndex); Assert.AreEqual(0x190, record.BoldWeight); Assert.AreEqual(FontSuperScript.None, record.SuperSubScript); Assert.AreEqual(FontUnderlineType.None, record.Underline); Assert.AreEqual(0x00, record.Family); Assert.AreEqual(0x00, record.Charset); Assert.AreEqual("Arial", record.FontName); Assert.AreEqual(21 + 4, record.RecordSize); }
public void TestEmptyName_bug47250() { byte[] emptyNameData = HexRead.ReadFromString( "C8 00 00 00 FF 7F 90 01 00 00 00 00 00 00 " + "00" // zero length + "00" // unicode options byte ); RecordInputStream in1 = TestcaseRecordInputStream.Create(SID, emptyNameData); FontRecord fr = new FontRecord(in1); if (in1.Available() == 1) { throw new AssertionException("Identified bug 47250"); } Assert.AreEqual(0, in1.Available()); Assert.AreEqual(0, fr.FontName.Length); byte[] recordBytes = fr.Serialize(); TestcaseRecordInputStream.ConfirmRecordEncoding(SID, emptyNameData, recordBytes); }
private void GenerateHeader(HSSFSheet sheet, HSSFWorkbook workbook, int startRow, int cellIndex) { FontRecord rec = new FontRecord() { FontHeight = 16 }; rec.BoldWeight = short.MaxValue; HSSFFont font = new HSSFFont(10, rec); //font.Index = 16; var style = workbook.CreateCellStyle(); SetCellStyle(style); style.SetFont(font); foreach (var header in _headers) { HSSFRow row = sheet.CreateRow(startRow); var cell = row.CreateCell(cellIndex++); cell.SetCellValue(Resource.GetString(header)); cell.CellStyle = style; } }
public FontListAggregate(RecordStream rs, ChartRecordAggregate container) : base(RuleName_FONTLIST, container) { frtFontList = (FrtFontListRecord)rs.GetNext(); startObject = (ChartStartObjectRecord)rs.GetNext(); FontRecord f = null; FbiRecord fbi = null; while (rs.PeekNextChartSid() == FontRecord.sid) { f = (FontRecord)rs.GetNext(); if (rs.PeekNextChartSid() == FbiRecord.sid) { fbi = (FbiRecord)rs.GetNext(); } else { fbi = null; } dicFonts.Add(f, fbi); } endObject = (ChartEndObjectRecord)rs.GetNext(); }
public void AddFont(FontRecord font) { _fontRepository.Create(font); }
private Biff GetCorrectRecord(GenericBiff record, Stream stream, SstRecord sst) { Biff ret = record; switch (record.Id) { case (ushort)RecordType.Bof: BofRecord bof = new BofRecord(record); if (bof.Version < 0x0600) { throw new Exception("Versions below Excel 97/2000 are currently not supported."); } ret = bof; break; case (ushort)RecordType.Boundsheet: ret = new BoundSheetRecord(record); break; case (ushort)RecordType.Index: ret = new IndexRecord(record); break; case (ushort)RecordType.DbCell: ret = new DbCellRecord(record); break; case (ushort)RecordType.Row: ret = new RowRecord(record); break; case (ushort)RecordType.Continue: ret = new ContinueRecord(record); break; case (ushort)RecordType.Blank: ret = new BlankRecord(record); break; case (ushort)RecordType.BoolErr: ret = new BoolErrRecord(record); break; case (ushort)RecordType.Formula: ret = new FormulaRecord(record, stream); break; case (ushort)RecordType.Label: ret = new LabelRecord(record); break; case (ushort)RecordType.LabelSst: ret = new LabelSstRecord(record, sst); break; case (ushort)RecordType.MulBlank: ret = new MulBlankRecord(record); break; case (ushort)RecordType.MulRk: ret = new MulRkRecord(record); break; case (ushort)RecordType.String: ret = new StringValueRecord(record); break; case (ushort)RecordType.Xf: ret = new XfRecord(record); break; case (ushort)RecordType.Rk: ret = new RkRecord(record); break; case (ushort)RecordType.Number: ret = new NumberRecord(record); break; case (ushort)RecordType.Array: ret = new ArrayRecord(record); break; case (ushort)RecordType.ShrFmla: ret = new SharedFormulaRecord(record); break; case (ushort)RecordType.Table: ret = new TableRecord(record); break; case (ushort)RecordType.Sst: ret = new SstRecord(record, stream); break; case (ushort)RecordType.Eof: ret = new EofRecord(record); break; case (ushort)RecordType.Font: ret = new FontRecord(record); break; case (ushort)RecordType.Format: ret = new Net.SourceForge.Koogra.Excel.Records.FormatRecord(record); break; case (ushort)RecordType.Palette: ret = new PaletteRecord(record); break; case (ushort)RecordType.Hyperlink: ret = new HyperLinkRecord(record); break; } return(ret); }
/** * Constructor * * @param fnt the font * @param form the format */ public CellXFRecord(FontRecord fnt, DisplayFormat form) : base(fnt, form) { setXFDetails(XFRecord.cell, 0); }
public static void CopyCell(HSSFCell oldCell, HSSFCell newCell, IDictionary <Int32, HSSFCellStyle> styleMap, Dictionary <short, short> paletteMap, Boolean keepFormulas) { if (styleMap != null) { if (oldCell.CellStyle != null) { if (oldCell.Sheet.Workbook == newCell.Sheet.Workbook) { newCell.CellStyle = oldCell.CellStyle; } else { int styleHashCode = oldCell.CellStyle.GetHashCode(); if (styleMap.ContainsKey(styleHashCode)) { newCell.CellStyle = styleMap[styleHashCode]; } else { HSSFCellStyle newCellStyle = (HSSFCellStyle)newCell.Sheet.Workbook.CreateCellStyle(); newCellStyle.CloneStyleFrom(oldCell.CellStyle); RemapCellStyle(newCellStyle, paletteMap); //Clone copies as-is, we need to remap colors manually newCell.CellStyle = newCellStyle; //Clone of cell style always clones the font. This makes my life easier IFont theFont = newCellStyle.GetFont(newCell.Sheet.Workbook); if (theFont.Color > 0 && paletteMap.ContainsKey(theFont.Color)) { theFont.Color = paletteMap[theFont.Color]; //Remap font color } styleMap.Add(styleHashCode, newCellStyle); } } } else { newCell.CellStyle = null; } } switch (oldCell.CellType) { case CellType.String: HSSFRichTextString rts = oldCell.RichStringCellValue as HSSFRichTextString; newCell.SetCellValue(rts); if (rts != null) { for (int j = 0; j < rts.NumFormattingRuns; j++) { short fontIndex = rts.GetFontOfFormattingRun(j); int startIndex = rts.GetIndexOfFormattingRun(j); int endIndex = 0; if (j + 1 == rts.NumFormattingRuns) { endIndex = rts.Length; } else { endIndex = rts.GetIndexOfFormattingRun(j + 1); } FontRecord fr = newCell.BoundWorkbook.CreateNewFont(); fr.CloneStyleFrom(oldCell.BoundWorkbook.GetFontRecordAt(fontIndex)); HSSFFont font = new HSSFFont((short)(newCell.BoundWorkbook.GetFontIndex(fr)), fr); newCell.RichStringCellValue.ApplyFont(startIndex, endIndex, font); } } break; case CellType.Numeric: newCell.SetCellValue(oldCell.NumericCellValue); break; case CellType.Blank: newCell.SetCellType(CellType.Blank); break; case CellType.Boolean: newCell.SetCellValue(oldCell.BooleanCellValue); break; case CellType.Error: newCell.SetCellValue(oldCell.ErrorCellValue); break; case CellType.Formula: if (keepFormulas) { newCell.SetCellType(CellType.Formula); newCell.CellFormula = oldCell.CellFormula; } else { try { newCell.SetCellType(CellType.Numeric); newCell.SetCellValue(oldCell.NumericCellValue); } catch (Exception ex) { newCell.SetCellType(CellType.String); newCell.SetCellValue(oldCell.ToString()); } } break; default: break; } }
/// <summary> /// Initializes a new instance of the <see cref="HSSFFont"/> class. /// </summary> /// <param name="index">The index.</param> /// <param name="rec">The record.</param> public HSSFFont(short index, FontRecord rec) { font = rec; this.index = index; }
/** * Does the hard work of building up the object graph from the excel bytes * * @exception BiffException * @exception PasswordException if the workbook is password protected */ protected override void parse() { Record r = null; BOFRecord bof = new BOFRecord(excelFile.next()); workbookBof = bof; bofs++; if (!bof.isBiff8() && !bof.isBiff7()) { throw new BiffException(BiffException.unrecognizedBiffVersion); } if (!bof.isWorkbookGlobals()) { throw new BiffException(BiffException.expectedGlobals); } ArrayList continueRecords = new ArrayList(); ArrayList localNames = new ArrayList(); nameTable = new ArrayList(); addInFunctions = new ArrayList(); // Skip to the first worksheet while (bofs == 1) { r = excelFile.next(); if (r.getType() == Type.SST) { continueRecords.Clear(); Record nextrec = excelFile.peek(); while (nextrec.getType() == Type.CONTINUE) { continueRecords.Add(excelFile.next()); nextrec = excelFile.peek(); } // cast the array Record[] records = new Record[continueRecords.Count]; int pos = 0; foreach (Record record in continueRecords) { records[pos++] = record; } sharedStrings = new SSTRecord(r, records, settings); } else if (r.getType() == Type.FILEPASS) { throw new PasswordException(); } else if (r.getType() == Type.NAME) { NameRecord nr = null; if (bof.isBiff8()) { nr = new NameRecord(r, settings, nameTable.Count); } else { nr = new NameRecord(r, settings, nameTable.Count, NameRecord.biff7); } // Add all local and global names to the name table in order to // preserve the indexing nameTable.Add(nr); if (nr.isGlobal()) { namedRecords.Add(nr.getName(), nr); } else { localNames.Add(nr); } } else if (r.getType() == Type.FONT) { FontRecord fr = null; if (bof.isBiff8()) { fr = new FontRecord(r, settings); } else { fr = new FontRecord(r, settings, FontRecord.biff7); } fonts.addFont(fr); } else if (r.getType() == Type.PALETTE) { CSharpJExcel.Jxl.Biff.PaletteRecord palette = new CSharpJExcel.Jxl.Biff.PaletteRecord(r); formattingRecords.setPalette(palette); } else if (r.getType() == Type.NINETEENFOUR) { NineteenFourRecord nr = new NineteenFourRecord(r); nineteenFour = nr.is1904(); } else if (r.getType() == Type.FORMAT) { FormatRecord fr = null; if (bof.isBiff8()) { fr = new FormatRecord(r, settings, FormatRecord.biff8); } else { fr = new FormatRecord(r, settings, FormatRecord.biff7); } try { formattingRecords.addFormat(fr); } catch (NumFormatRecordsException e) { // This should not happen. Bomb out Assert.verify(false, e.Message); } } else if (r.getType() == Type.XF) { XFRecord xfr = null; if (bof.isBiff8()) { xfr = new XFRecord(r, settings, XFRecord.biff8); } else { xfr = new XFRecord(r, settings, XFRecord.biff7); } try { formattingRecords.addStyle(xfr); } catch (NumFormatRecordsException e) { // This should not happen. Bomb out Assert.verify(false, e.Message); } } else if (r.getType() == Type.BOUNDSHEET) { BoundsheetRecord br = null; if (bof.isBiff8()) { br = new BoundsheetRecord(r, settings); } else { br = new BoundsheetRecord(r, BoundsheetRecord.biff7); } if (br.isSheet()) { boundsheets.Add(br); } else if (br.isChart() && !settings.getDrawingsDisabled()) { boundsheets.Add(br); } } else if (r.getType() == Type.EXTERNSHEET) { if (bof.isBiff8()) { externSheet = new ExternalSheetRecord(r, settings); } else { externSheet = new ExternalSheetRecord(r, settings, ExternalSheetRecord.biff7); } } else if (r.getType() == Type.XCT) { XCTRecord xctr = new XCTRecord(r); xctRecords.Add(xctr); } else if (r.getType() == Type.CODEPAGE) { CodepageRecord cr = new CodepageRecord(r); settings.setCharacterSet(cr.getCharacterSet()); } else if (r.getType() == Type.SUPBOOK) { Record nextrec = excelFile.peek(); while (nextrec.getType() == Type.CONTINUE) { r.addContinueRecord(excelFile.next()); nextrec = excelFile.peek(); } SupbookRecord sr = new SupbookRecord(r, settings); supbooks.Add(sr); } else if (r.getType() == Type.EXTERNNAME) { ExternalNameRecord enr = new ExternalNameRecord(r, settings); if (enr.isAddInFunction()) { addInFunctions.Add(enr.getName()); } } else if (r.getType() == Type.PROTECT) { ProtectRecord pr = new ProtectRecord(r); wbProtected = pr.isProtected(); } else if (r.getType() == Type.OBJPROJ) { doesContainMacros = true; } else if (r.getType() == Type.COUNTRY) { countryRecord = new CountryRecord(r); } else if (r.getType() == Type.MSODRAWINGGROUP) { if (!settings.getDrawingsDisabled()) { msoDrawingGroup = new MsoDrawingGroupRecord(r); if (drawingGroup == null) { drawingGroup = new DrawingGroup(Origin.READ); } drawingGroup.add(msoDrawingGroup); Record nextrec = excelFile.peek(); while (nextrec.getType() == Type.CONTINUE) { drawingGroup.add(excelFile.next()); nextrec = excelFile.peek(); } } } else if (r.getType() == Type.BUTTONPROPERTYSET) { buttonPropertySet = new ButtonPropertySetRecord(r); } else if (r.getType() == Type.EOF) { bofs--; } else if (r.getType() == Type.REFRESHALL) { RefreshAllRecord rfm = new RefreshAllRecord(r); settings.setRefreshAll(rfm.getRefreshAll()); } else if (r.getType() == Type.TEMPLATE) { TemplateRecord rfm = new TemplateRecord(r); settings.setTemplate(rfm.getTemplate()); } else if (r.getType() == Type.EXCEL9FILE) { Excel9FileRecord e9f = new Excel9FileRecord(r); settings.setExcel9File(e9f.getExcel9File()); } else if (r.getType() == Type.WINDOWPROTECT) { WindowProtectedRecord winp = new WindowProtectedRecord(r); settings.setWindowProtected(winp.getWindowProtected()); } else if (r.getType() == Type.HIDEOBJ) { HideobjRecord hobj = new HideobjRecord(r); settings.setHideobj(hobj.getHideMode()); } else if (r.getType() == Type.WRITEACCESS) { WriteAccessRecord war = new WriteAccessRecord(r, bof.isBiff8(), settings); settings.setWriteAccess(war.getWriteAccess()); } else { // logger.info("Unsupported record type: " + // Integer.toHexString(r.getCode())+"h"); } } bof = null; if (excelFile.hasNext()) { r = excelFile.next(); if (r.getType() == Type.BOF) { bof = new BOFRecord(r); } } // Only get sheets for which there is a corresponding Boundsheet record while (bof != null && getNumberOfSheets() < boundsheets.Count) { if (!bof.isBiff8() && !bof.isBiff7()) { throw new BiffException(BiffException.unrecognizedBiffVersion); } if (bof.isWorksheet()) { // Read the sheet in SheetImpl s = new SheetImpl(excelFile, sharedStrings, formattingRecords, bof, workbookBof, nineteenFour, this); BoundsheetRecord br = (BoundsheetRecord)boundsheets[getNumberOfSheets()]; s.setName(br.getName()); s.setHidden(br.isHidden()); addSheet(s); } else if (bof.isChart()) { // Read the sheet in SheetImpl s = new SheetImpl(excelFile, sharedStrings, formattingRecords, bof, workbookBof, nineteenFour, this); BoundsheetRecord br = (BoundsheetRecord)boundsheets[getNumberOfSheets()]; s.setName(br.getName()); s.setHidden(br.isHidden()); addSheet(s); } else { //logger.warn("BOF is unrecognized"); while (excelFile.hasNext() && r.getType() != Type.EOF) { r = excelFile.next(); } } // The next record will normally be a BOF or empty padding until // the end of the block is reached. In exceptionally unlucky cases, // the last EOF will coincide with a block division, so we have to // check there is more data to retrieve. // Thanks to liamg for spotting this bof = null; if (excelFile.hasNext()) { r = excelFile.next(); if (r.getType() == Type.BOF) { bof = new BOFRecord(r); } } } // Add all the local names to the specific sheets foreach (NameRecord nr in localNames) { if (nr.getBuiltInName() == null) { //logger.warn("Usage of a local non-builtin name"); } else if (nr.getBuiltInName() == BuiltInName.PRINT_AREA || nr.getBuiltInName() == BuiltInName.PRINT_TITLES) { // appears to use the internal tab number rather than the // external sheet index SheetImpl s = (SheetImpl)sheets[nr.getSheetRef() - 1]; s.addLocalName(nr); } } }
/** * Constructor * * @param fnt the font for this style * @param form the format of this style */ public StyleXFRecord(FontRecord fnt, DisplayFormat form) : base(fnt, form) { setXFDetails(XFRecord.style, 0xfff0); }
/// <summary> Does the hard work of building up the object graph from the excel bytes /// /// </summary> /// <exception cref=""> BiffException /// </exception> /// <exception cref=""> PasswordException if the workbook is password protected /// </exception> protected internal override void parse() { Record r = null; BOFRecord bof = new BOFRecord(excelFile.next()); workbookBof = bof; bofs++; if (!bof.isBiff8() && !bof.isBiff7()) { throw new BiffException(BiffException.unrecognizedBiffVersion); } if (!bof.isWorkbookGlobals()) { throw new BiffException(BiffException.expectedGlobals); } ArrayList continueRecords = new ArrayList(); nameTable = new ArrayList(); // Skip to the first worksheet while (bofs == 1) { r = excelFile.next(); if (r.Type == NExcel.Biff.Type.SST) { continueRecords.Clear(); Record nextrec = excelFile.peek(); while (nextrec.Type == NExcel.Biff.Type.CONTINUE) { continueRecords.Add(excelFile.next()); nextrec = excelFile.peek(); } // cast the array System.Object[] rec = continueRecords.ToArray(); Record[] records = new Record[rec.Length]; Array.Copy(rec, 0, records, 0, rec.Length); sharedStrings = new SSTRecord(r, records, settings); } else if (r.Type == NExcel.Biff.Type.FILEPASS) { throw new PasswordException(); } else if (r.Type == NExcel.Biff.Type.NAME) { NameRecord nr = null; if (bof.isBiff8()) { nr = new NameRecord(r, settings, namedRecords.Count); } else { nr = new NameRecord(r, settings, namedRecords.Count, NameRecord.biff7); } namedRecords[nr.Name] = nr; nameTable.Add(nr); } else if (r.Type == NExcel.Biff.Type.FONT) { FontRecord fr = null; if (bof.isBiff8()) { fr = new FontRecord(r, settings); } else { fr = new FontRecord(r, settings, FontRecord.biff7); } fonts.addFont(fr); } else if (r.Type == NExcel.Biff.Type.PALETTE) { NExcel.Biff.PaletteRecord palette = new NExcel.Biff.PaletteRecord(r); formattingRecords.Palette = palette; } else if (r.Type == NExcel.Biff.Type.NINETEENFOUR) { NineteenFourRecord nr = new NineteenFourRecord(r); nineteenFour = nr.is1904(); } else if (r.Type == NExcel.Biff.Type.FORMAT) { FormatRecord fr = null; if (bof.isBiff8()) { fr = new FormatRecord(r, settings, FormatRecord.biff8); } else { fr = new FormatRecord(r, settings, FormatRecord.biff7); } try { formattingRecords.addFormat(fr); } catch (NumFormatRecordsException e) { // This should not happen. Bomb out // Assert.verify(false, e.getMessage()); Assert.verify(false, "This should not happen. 64"); } } else if (r.Type == NExcel.Biff.Type.XF) { XFRecord xfr = null; if (bof.isBiff8()) { xfr = new XFRecord(r, XFRecord.biff8); } else { xfr = new XFRecord(r, XFRecord.biff7); } try { formattingRecords.addStyle(xfr); } catch (NumFormatRecordsException e) { // This should not happen. Bomb out // Assert.verify(false, e.getMessage()); Assert.verify(false, "This should not happen. 59"); } } else if (r.Type == NExcel.Biff.Type.BOUNDSHEET) { BoundsheetRecord br = null; if (bof.isBiff8()) { br = new BoundsheetRecord(r); } else { br = new BoundsheetRecord(r, BoundsheetRecord.biff7); } if (br.isSheet() || br.Chart) { boundsheets.Add(br); } } else if (r.Type == NExcel.Biff.Type.EXTERNSHEET) { if (bof.isBiff8()) { externSheet = new ExternalSheetRecord(r, settings); } else { externSheet = new ExternalSheetRecord(r, settings, ExternalSheetRecord.biff7); } } else if (r.Type == NExcel.Biff.Type.CODEPAGE) { CodepageRecord cr = new CodepageRecord(r); settings.CharacterSet = cr.CharacterSet; } else if (r.Type == NExcel.Biff.Type.SUPBOOK) { SupbookRecord sr = new SupbookRecord(r, settings); supbooks.Add(sr); } else if (r.Type == NExcel.Biff.Type.PROTECT) { ProtectRecord pr = new ProtectRecord(r); wbProtected = pr.IsProtected(); } else if (r.Type == NExcel.Biff.Type.MSODRAWINGGROUP) { msoDrawingGroup = new MsoDrawingGroupRecord(r); if (drawingGroup == null) { drawingGroup = new DrawingGroup(DrawingGroup.READ); } drawingGroup.add(msoDrawingGroup); Record nextrec = excelFile.peek(); while (nextrec.Type == NExcel.Biff.Type.CONTINUE) { drawingGroup.add(excelFile.next()); nextrec = excelFile.peek(); } } else if (r.Type == NExcel.Biff.Type.EOF) { bofs--; } } bof = null; if (excelFile.hasNext()) { r = excelFile.next(); if (r.Type == NExcel.Biff.Type.BOF) { bof = new BOFRecord(r); } } // Only get sheets for which there is a corresponding Boundsheet record while (bof != null && NumberOfSheets < boundsheets.Count) { if (!bof.isBiff8() && !bof.isBiff7()) { throw new BiffException(BiffException.unrecognizedBiffVersion); } if (bof.isWorksheet()) { // Read the sheet in SheetImpl s = new SheetImpl(excelFile, sharedStrings, formattingRecords, bof, workbookBof, nineteenFour, this); BoundsheetRecord br = (BoundsheetRecord)boundsheets[NumberOfSheets]; s.setName(br.Name); s.Hidden = br.isHidden(); addSheet(s); } else if (bof.isChart()) { // Read the sheet in SheetImpl s = new SheetImpl(excelFile, sharedStrings, formattingRecords, bof, workbookBof, nineteenFour, this); BoundsheetRecord br = (BoundsheetRecord)boundsheets[NumberOfSheets]; s.setName(br.Name); s.Hidden = br.isHidden(); addSheet(s); } else { logger.warn("BOF is unrecognized"); while (excelFile.hasNext() && r.Type != NExcel.Biff.Type.EOF) { r = excelFile.next(); } } // The next record will normally be a BOF or empty padding until // the end of the block is reached. In exceptionally unlucky cases, // the last EOF will coincide with a block division, so we have to // check there is more data to retrieve. // Thanks to liamg for spotting this bof = null; if (excelFile.hasNext()) { r = excelFile.next(); if (r.Type == NExcel.Biff.Type.BOF) { bof = new BOFRecord(r); } } } }
/// <summary> /// Goes through the Workbook, optimising the fonts by /// removing duplicate ones. /// For now, only works on fonts used in HSSFCellStyle /// and HSSFRichTextString. Any other font uses /// (eg charts, pictures) may well end up broken! /// This can be a slow operation, especially if you have /// lots of cells, cell styles or rich text strings /// </summary> /// <param name="workbook">The workbook in which to optimise the fonts</param> public static void OptimiseFonts(HSSFWorkbook workbook) { // Where each font has ended up, and if we need to // delete the record for it. Start off with no change short[] newPos = new short[workbook.Workbook.NumberOfFontRecords + 1]; bool[] zapRecords = new bool[newPos.Length]; for (int i = 0; i < newPos.Length; i++) { newPos[i] = (short)i; zapRecords[i] = false; } // Get each font record, so we can do deletes // without Getting confused FontRecord[] frecs = new FontRecord[newPos.Length]; for (int i = 0; i < newPos.Length; i++) { // There is no 4! if (i == 4) { continue; } frecs[i] = workbook.Workbook.GetFontRecordAt(i); } // Loop over each font, seeing if it is the same // as an earlier one. If it is, point users of the // later duplicate copy to the earlier one, and // mark the later one as needing deleting // Note - don't change built in fonts (those before 5) for (int i = 5; i < newPos.Length; i++) { // Check this one for being a duplicate // of an earlier one int earlierDuplicate = -1; for (int j = 0; j < i && earlierDuplicate == -1; j++) { if (j == 4) { continue; } FontRecord frCheck = workbook.Workbook.GetFontRecordAt(j); if (frCheck.SameProperties(frecs[i])) { earlierDuplicate = j; } } // If we got a duplicate, mark it as such if (earlierDuplicate != -1) { newPos[i] = (short)earlierDuplicate; zapRecords[i] = true; } } // Update the new positions based on // deletes that have occurred between // the start and them // Only need to worry about user fonts for (int i = 5; i < newPos.Length; i++) { // Find the number deleted to that // point, and adjust short preDeletePos = newPos[i]; short newPosition = preDeletePos; for (int j = 0; j < preDeletePos; j++) { if (zapRecords[j]) { newPosition--; } } // Update the new position newPos[i] = newPosition; } // Zap the un-needed user font records for (int i = 5; i < newPos.Length; i++) { if (zapRecords[i]) { workbook.Workbook.RemoveFontRecord( frecs[i] ); } } // Tell HSSFWorkbook that it needs to // re-start its HSSFFontCache workbook.ResetFontCache(); // Update the cell styles to point at the // new locations of the fonts for (int i = 0; i < workbook.Workbook.NumExFormats; i++) { ExtendedFormatRecord xfr = workbook.Workbook.GetExFormatAt(i); xfr.FontIndex = ( newPos[xfr.FontIndex] ); } // Update the rich text strings to point at // the new locations of the fonts // Remember that one underlying unicode string // may be shared by multiple RichTextStrings! ArrayList doneUnicodeStrings = new ArrayList(); for (int sheetNum = 0; sheetNum < workbook.NumberOfSheets; sheetNum++) { NPOI.SS.UserModel.ISheet s = workbook.GetSheetAt(sheetNum); IEnumerator rIt = s.GetRowEnumerator(); while (rIt.MoveNext()) { HSSFRow row = (HSSFRow)rIt.Current; IEnumerator cIt = row.GetEnumerator(); while (cIt.MoveNext()) { ICell cell = (HSSFCell)cIt.Current; if (cell.CellType == NPOI.SS.UserModel.CellType.STRING) { HSSFRichTextString rtr = (HSSFRichTextString)cell.RichStringCellValue; UnicodeString u = rtr.RawUnicodeString; // Have we done this string already? if (!doneUnicodeStrings.Contains(u)) { // Update for each new position for (short i = 5; i < newPos.Length; i++) { if (i != newPos[i]) { u.SwapFontUse(i, newPos[i]); } } // Mark as done doneUnicodeStrings.Add(u); } } } } } }