⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 writableworkbookimpl.java~

📁 jxtl API Java中Excel的生成与导入解析参考文档
💻 JAVA~
📖 第 1 页 / 共 4 页
字号:

  /**
   * Closes this workbook, and frees makes any memory allocated available
   * for garbage collection
   * 
   * @exception IOException 
   * @exception JxlWriteException
   */
  public void close() throws IOException, JxlWriteException
  {
    outputFile.close(closeStream);
  }

  /**
   * Sets a new output file.  This allows the smae workbook to be
   * written to various different output files without having to
   * read in any templates again
   *
   * @param fileName the file name
   * @exception IOException
   */
  public void setOutputFile(java.io.File fileName) throws IOException
  {
    FileOutputStream fos = new FileOutputStream(fileName);
    outputFile.setOutputFile(fos);
  }


  /**
   * The internal method implementation for creating new sheets
   *
   * @param name
   * @param index
   * @param handleRefs flag indicating whether or not to handle external
   *                   sheet references
   * @return 
   */
  private WritableSheet createSheet(String name, int index, 
                                    boolean handleRefs)
  {
    WritableSheet w = new WritableSheetImpl(name, 
                                            outputFile, 
                                            formatRecords, 
                                            sharedStrings,
                                            settings,
                                            this);

    int pos = index;

    if (index <= 0)
    {
      pos = 0;
      sheets.add(0, w);
    }
    else if (index > sheets.size())
    {
      pos = sheets.size();
      sheets.add(w);
    }
    else
    {
      sheets.add(index, w);
    }

    if (handleRefs && externSheet != null)
    {
      externSheet.sheetInserted(pos);
    }

    if (supbooks != null && supbooks.size() > 0)
    {
      SupbookRecord supbook = (SupbookRecord) supbooks.get(0);
      if (supbook.getType() == SupbookRecord.INTERNAL)
      {
        supbook.adjustInternal(sheets.size());
      }
    }

    return w;
  }

  /**
   * Creates a new sheet within the workbook, at the specified position.
   * The new sheet is inserted at the specified position, or prepended/appended
   * to the list of sheets if the index specified is somehow inappropriate
   * 
   * @param name the name of the new sheet
   * @param index the index at which to add the sheet
   * @return the created sheet
   */
  public WritableSheet createSheet(String name, int index)
  {
    return createSheet(name, index, true);
  }

  /**
   * Removes a sheet from this workbook, the other sheets indices being
   * altered accordingly. If the sheet referenced by the index
   * does not exist, then no action is taken.
   * 
   * @param index the index of the sheet to remove
   */
  public void removeSheet(int index)
  {
    int pos = index;
    if (index <= 0)
    {
      pos = 0;
      sheets.remove(0);
    }
    else if (index >= sheets.size())
    {
      pos = sheets.size() - 1;
      sheets.remove(sheets.size() - 1);
    }
    else
    {
      sheets.remove(index);
    }

    if (externSheet != null)
    {
      externSheet.sheetRemoved(pos);
    }

    if (supbooks != null && supbooks.size() > 0)
    {
      SupbookRecord supbook = (SupbookRecord) supbooks.get(0);
      if (supbook.getType() == SupbookRecord.INTERNAL)
      {
        supbook.adjustInternal(sheets.size());
      }
    }

    if (names != null && names.size() > 0)
    {
      for (int i=0; i< names.size();i++)
      {
        NameRecord n = (NameRecord) names.get(i);
        int oldRef = n.getSheetRef();
        if(oldRef == (pos+1))
        {
          n.setSheetRef(0); // make a global name reference
        } 
        else if (oldRef > (pos+1))
        {
          if(oldRef < 1)
          {
            oldRef = 1;
          }
          n.setSheetRef(oldRef-1); // move one sheet
        }
      }
    }
  }

  /**
   * Moves the specified sheet within this workbook to another index
   * position.
   * 
   * @param fromIndex the zero based index of the reQuired sheet
   * @param toIndex the zero based index of the reQuired sheet
   * @return the sheet that has been moved
   */
  public WritableSheet moveSheet(int fromIndex, int toIndex)
  {
    // Handle dodgy index 
    fromIndex = Math.max(fromIndex, 0);
    fromIndex = Math.min(fromIndex, sheets.size() - 1);
    toIndex   = Math.max(toIndex, 0);
    toIndex   = Math.min(toIndex, sheets.size() - 1);

    WritableSheet sheet= (WritableSheet)sheets.remove(fromIndex);
    sheets.add(toIndex, sheet);
    
    return sheet;
  }

  /**
   * 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 void write() throws IOException
  {
    // 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);
      }
    }

    // Rationalize all the XF and number formats
    if (!settings.getRationalizationDisabled())
    {
      rationalize();
    }

    // Write the workbook globals
    BOFRecord bof = new BOFRecord(BOFRecord.workbookGlobals);
    outputFile.write(bof);

    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();
    outputFile.write(wr);

    CodepageRecord cp = new CodepageRecord();
    outputFile.write(cp);

    DSFRecord dsf = new DSFRecord();
    outputFile.write(dsf);

    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(false);
    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);

    Window1Record w1r = new Window1Record();
    outputFile.write(w1r);

    BackupRecord bkr = new BackupRecord(false);
    outputFile.write(bkr);

    HideobjRecord ho = new HideobjRecord(false);
    outputFile.write(ho);
    
    NineteenFourRecord nf = new NineteenFourRecord(false);
    outputFile.write(nf);

    PrecisionRecord pc = new PrecisionRecord(false);
    outputFile.write(pc);

    RefreshAllRecord rar = new RefreshAllRecord(false);
    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.get(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);
      }
    }

    // Write out the external sheet record, if it exists
    if (externSheet != null)
    {
      //Write out all the supbook records
      for (int i = 0; i < supbooks.size() ; i++)
      {
        SupbookRecord supbook = (SupbookRecord) supbooks.get(i);
        outputFile.write(supbook);
      }
      outputFile.write(externSheet);
    }

    // Write out the names, if any exists
    if (names != null)
    {
      for (int i = 0 ; i < names.size() ; i++)
      {
        NameRecord n = (NameRecord) names.get(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);

    // If no sheet is identified as being selected, then select
    // the first one
    boolean sheetSelected = false;
    WritableSheetImpl wsheet = null;
    for (int i = 0 ; i < getNumberOfSheets() && !sheetSelected ; i++)
    {
      wsheet = (WritableSheetImpl) getSheet(i);
      if (wsheet.getSettings().isSelected())
      {
        sheetSelected = true;
      }
    }

    if (!sheetSelected)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -