/** * A pseudo copy constructor. Takes the handles to the font and formatting * records * * @exception IOException * @param w the workbook to copy * @param os the output stream to write the data to * @param cs TRUE if the workbook should close the output stream, FALSE * @param ws the configuration for this workbook */ public WritableWorkbookImpl(Stream os, Workbook w, bool cs, WorkbookSettings ws) : base() { CSharpJExcel.Jxl.Read.Biff.WorkbookParser wp = (CSharpJExcel.Jxl.Read.Biff.WorkbookParser)w; // Reset the statically declared styles. These are no longer needed // because the Styles class will intercept all calls within // CellValue.setCellDetails and if it detects a standard format, then it // will return a clone. In short, the static cell values will // never get initialized anyway. Still, just to be extra sure... //lock (SYNCHRONIZER) // { // WritableWorkbook.ARIAL_10_PT.uninitialize(); // WritableWorkbook.HYPERLINK_FONT.uninitialize(); // WritableWorkbook.NORMAL_STYLE.uninitialize(); // WritableWorkbook.HYPERLINK_STYLE.uninitialize(); // WritableWorkbook.HIDDEN_STYLE.uninitialize(); // DateRecord.defaultDateFormat.uninitialize(); // } closeStream = cs; sheets = new ArrayList(); sharedStrings = new SharedStrings(); nameRecords = new Dictionary<string, NameRecord>(); fonts = wp.getFonts(); formatRecords = wp.getFormattingRecords(); wbProtected = false; settings = ws; rcirCells = new ArrayList(); styles = new Styles(); outputFile = new File(os, ws, wp.getCompoundFile()); containsMacros = false; if (!ws.getPropertySetsDisabled()) containsMacros = wp.containsMacros(); // Copy the country settings if (wp.getCountryRecord() != null) countryRecord = new CountryRecord(wp.getCountryRecord()); // Copy any add in functions addInFunctionNames = wp.getAddInFunctionNames(); // Copy XCT records xctRecords = wp.getXCTRecords(); // Copy any external sheets if (wp.getExternalSheetRecord() != null) { externSheet = new ExternalSheetRecord(wp.getExternalSheetRecord()); // Get the associated supbooks CSharpJExcel.Jxl.Read.Biff.SupbookRecord[] readsr = wp.getSupbookRecords(); supbooks = new ArrayList(readsr.Length); for (int i = 0; i < readsr.Length; i++) { CSharpJExcel.Jxl.Read.Biff.SupbookRecord readSupbook = readsr[i]; if (readSupbook.getType() == SupbookRecord.INTERNAL || readSupbook.getType() == SupbookRecord.EXTERNAL) supbooks.Add(new SupbookRecord(readSupbook, settings)); else { if (readSupbook.getType() != SupbookRecord.ADDIN) { //logger.warn("unsupported supbook type - ignoring"); } } } } // Copy any drawings. These must be present before we try and copy // the images from the read workbook if (wp.getDrawingGroup() != null) drawingGroup = new DrawingGroup(wp.getDrawingGroup()); // Copy the property set references if (containsMacros && wp.getButtonPropertySet() != null) buttonPropertySet = new ButtonPropertySetRecord(wp.getButtonPropertySet()); // Copy any names if (!settings.getNamesDisabled()) { CSharpJExcel.Jxl.Read.Biff.NameRecord[] na = wp.getNameRecords(); names = new ArrayList(na.Length); for (int i = 0; i < na.Length; i++) { if (na[i].isBiff8()) { NameRecord n = new NameRecord(na[i], i); names.Add(n); string key = n.getName() == null ? NULLKEY : n.getName(); nameRecords.Add(key, n); } else { //logger.warn("Cannot copy Biff7 name records - ignoring"); } } } copyWorkbook(w); // The copy process may have caused some critical fields in the // read drawing group to change. Make sure these updates are reflected // in the writable drawing group if (drawingGroup != null) drawingGroup.updateData(wp.getDrawingGroup()); }
/** * Writes out this sheet to the output file. First it writes out * the standard workbook information required by excel, before calling * the write method on each sheet individually * * @exception IOException */ public override void write() { // Perform some preliminary sheet check before we start writing out // the workbook WritableSheetImpl wsi = null; for (int i = 0; i < getNumberOfSheets(); i++) { wsi = (WritableSheetImpl)getSheet(i); // Check the merged records. This has to be done before the // globals are written out because some more XF formats might be created wsi.checkMergedBorders(); // Check to see if there are any predefined names Range range = wsi.getSettings().getPrintArea(); if (range != null) { addNameArea(BuiltInName.PRINT_AREA, wsi, range.getTopLeft().getColumn(), range.getTopLeft().getRow(), range.getBottomRight().getColumn(), range.getBottomRight().getRow(), false); } // Check to see if print titles by row were set Range rangeR = wsi.getSettings().getPrintTitlesRow(); Range rangeC = wsi.getSettings().getPrintTitlesCol(); if (rangeR != null && rangeC != null) { addNameArea(BuiltInName.PRINT_TITLES, wsi, rangeR.getTopLeft().getColumn(), rangeR.getTopLeft().getRow(), rangeR.getBottomRight().getColumn(), rangeR.getBottomRight().getRow(), rangeC.getTopLeft().getColumn(), rangeC.getTopLeft().getRow(), rangeC.getBottomRight().getColumn(), rangeC.getBottomRight().getRow(), false); } // Check to see if print titles by row were set else if (rangeR != null) { addNameArea(BuiltInName.PRINT_TITLES, wsi, rangeR.getTopLeft().getColumn(), rangeR.getTopLeft().getRow(), rangeR.getBottomRight().getColumn(), rangeR.getBottomRight().getRow(), false); } // Check to see if print titles by column were set else if (rangeC != null) { addNameArea(BuiltInName.PRINT_TITLES, wsi, rangeC.getTopLeft().getColumn(), rangeC.getTopLeft().getRow(), rangeC.getBottomRight().getColumn(), rangeC.getBottomRight().getRow(), false); } } // Rationalize all the XF and number formats if (!settings.getRationalizationDisabled()) rationalize(); // Write the workbook globals BOFRecord bof = new BOFRecord(BOFRecord.workbookGlobals); outputFile.write(bof); // Must immediatly follow the BOF record if (settings.getTemplate()) { // Only write record if we are a template TemplateRecord trec = new TemplateRecord(); outputFile.write(trec); } InterfaceHeaderRecord ihr = new InterfaceHeaderRecord(); outputFile.write(ihr); MMSRecord mms = new MMSRecord(0, 0); outputFile.write(mms); InterfaceEndRecord ier = new InterfaceEndRecord(); outputFile.write(ier); WriteAccessRecord wr = new WriteAccessRecord(settings.getWriteAccess()); outputFile.write(wr); CodepageRecord cp = new CodepageRecord(); outputFile.write(cp); DSFRecord dsf = new DSFRecord(); outputFile.write(dsf); if (settings.getExcel9File()) { // Only write record if we are a template // We are not excel 2000, should we still set the flag Excel9FileRecord e9rec = new Excel9FileRecord(); outputFile.write(e9rec); } TabIdRecord tabid = new TabIdRecord(getNumberOfSheets()); outputFile.write(tabid); if (containsMacros) { ObjProjRecord objproj = new ObjProjRecord(); outputFile.write(objproj); } if (buttonPropertySet != null) outputFile.write(buttonPropertySet); FunctionGroupCountRecord fgcr = new FunctionGroupCountRecord(); outputFile.write(fgcr); // do not support password protected workbooks WindowProtectRecord wpr = new WindowProtectRecord (settings.getWindowProtected()); outputFile.write(wpr); ProtectRecord pr = new ProtectRecord(wbProtected); outputFile.write(pr); PasswordRecord pw = new PasswordRecord(null); outputFile.write(pw); Prot4RevRecord p4r = new Prot4RevRecord(false); outputFile.write(p4r); Prot4RevPassRecord p4rp = new Prot4RevPassRecord(); outputFile.write(p4rp); // If no sheet is identified as being selected, then select // the first one bool sheetSelected = false; WritableSheetImpl wsheet = null; int selectedSheetIndex = 0; for (int i = 0; i < getNumberOfSheets() && !sheetSelected; i++) { wsheet = (WritableSheetImpl)getSheet(i); if (wsheet.getSettings().isSelected()) { sheetSelected = true; selectedSheetIndex = i; } } if (!sheetSelected) { wsheet = (WritableSheetImpl)getSheet(0); wsheet.getSettings().setSelected(true); selectedSheetIndex = 0; } Window1Record w1r = new Window1Record(selectedSheetIndex); outputFile.write(w1r); BackupRecord bkr = new BackupRecord(false); outputFile.write(bkr); HideobjRecord ho = new HideobjRecord(settings.getHideobj()); outputFile.write(ho); NineteenFourRecord nf = new NineteenFourRecord(false); outputFile.write(nf); PrecisionRecord pc = new PrecisionRecord(false); outputFile.write(pc); RefreshAllRecord rar = new RefreshAllRecord(settings.getRefreshAll()); outputFile.write(rar); BookboolRecord bb = new BookboolRecord(true); outputFile.write(bb); // Write out all the fonts used fonts.write(outputFile); // Write out the cell formats used within this workbook formatRecords.write(outputFile); // Write out the palette, if it exists if (formatRecords.getPalette() != null) outputFile.write(formatRecords.getPalette()); // Write out the uses elfs record UsesElfsRecord uer = new UsesElfsRecord(); outputFile.write(uer); // Write out the boundsheet records. Keep a handle to each one's // position so we can write in the stream offset later int[] boundsheetPos = new int[getNumberOfSheets()]; Sheet sheet = null; for (int i = 0; i < getNumberOfSheets(); i++) { boundsheetPos[i] = outputFile.getPos(); sheet = getSheet(i); BoundsheetRecord br = new BoundsheetRecord(sheet.getName()); if (sheet.getSettings().isHidden()) br.setHidden(); if (((WritableSheetImpl)sheets[i]).isChartOnly()) br.setChartOnly(); outputFile.write(br); } if (countryRecord == null) { CountryCode lang = CountryCode.getCountryCode(settings.getExcelDisplayLanguage()); if (lang == CountryCode.UNKNOWN) { //logger.warn("Unknown country code " + // settings.getExcelDisplayLanguage() + // " using " + CountryCode.USA.getCode()); lang = CountryCode.USA; } CountryCode region = CountryCode.getCountryCode(settings.getExcelRegionalSettings()); countryRecord = new CountryRecord(lang, region); if (region == CountryCode.UNKNOWN) { //logger.warn("Unknown country code " + // settings.getExcelDisplayLanguage() + // " using " + CountryCode.UK.getCode()); region = CountryCode.UK; } } outputFile.write(countryRecord); // Write out the names of any add in functions if (addInFunctionNames != null && addInFunctionNames.Length > 0) { // Write out the supbook record // SupbookRecord supbook = new SupbookRecord(); // outputFile.write(supbook); for (int i = 0; i < addInFunctionNames.Length; i++) { ExternalNameRecord enr = new ExternalNameRecord(addInFunctionNames[i]); outputFile.write(enr); } } if (xctRecords != null) { for (int i = 0; i < xctRecords.Length; i++) outputFile.write(xctRecords[i]); } // Write out the external sheet record, if it exists if (externSheet != null) { //Write out all the supbook records for (int i = 0; i < supbooks.Count; i++) { SupbookRecord supbook = (SupbookRecord)supbooks[i]; outputFile.write(supbook); } outputFile.write(externSheet); } // Write out the names, if any exists if (names != null) { for (int i = 0; i < names.Count; i++) { NameRecord n = (NameRecord)names[i]; outputFile.write(n); } } // Write out the mso drawing group, if it exists if (drawingGroup != null) drawingGroup.write(outputFile); sharedStrings.write(outputFile); EOFRecord eof = new EOFRecord(); outputFile.write(eof); // Write out the sheets for (int i = 0; i < getNumberOfSheets(); i++) { // first go back and modify the offset we wrote out for the // boundsheet record outputFile.setData(IntegerHelper.getFourBytes(outputFile.getPos()),boundsheetPos[i] + 4); wsheet = (WritableSheetImpl)getSheet(i); wsheet.write(); } }