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

📄 sheetwriter.java

📁 jxtl API Java中Excel的生成与导入解析参考文档
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
      outputFile.write(opr);

      if (settings.getPassword() != null)
      {
        PasswordRecord pw = new PasswordRecord(settings.getPassword());
        outputFile.write(pw);
      }
      else if (settings.getPasswordHash() != 0)
      {
        PasswordRecord pw = new PasswordRecord(settings.getPasswordHash());
        outputFile.write(pw);
      }
    }

    indexRecord.setDataStartPosition(outputFile.getPos());
    DefaultColumnWidth dcw = 
      new DefaultColumnWidth(settings.getDefaultColumnWidth());
    outputFile.write(dcw);
    
    // Get a handle to the normal styles
    WritableCellFormat normalStyle = 
      sheet.getWorkbook().getStyles().getNormalStyle();
    WritableCellFormat defaultDateFormat = 
      sheet.getWorkbook().getStyles().getDefaultDateFormat();

    // Write out all the column formats
    ColumnInfoRecord cir = null;
    for (Iterator colit = columnFormats.iterator(); colit.hasNext() ; )
    {
      cir = (ColumnInfoRecord) colit.next();

      // Writing out the column info with index 0x100 causes excel to crash
      if (cir.getColumn() < 0x100)
      {
        outputFile.write(cir);
      }

      XFRecord xfr = cir.getCellFormat();
      
      if (xfr != normalStyle && cir.getColumn() < 0x100)
      {
        // Make this the format for every cell in the column
        Cell[] cells = getColumn(cir.getColumn());

        for (int i = 0; i < cells.length; i++)
        {
          if (cells[i] != null &&
              (cells[i].getCellFormat() == normalStyle ||
               cells[i].getCellFormat() == defaultDateFormat))
          {
            // The cell has no overriding format specified, so
            // set it to the column default
            ((WritableCell) cells[i]).setCellFormat(xfr);
          }
        }
      }
    }

    // Write out the auto filter
    if (autoFilter != null)
    {
      autoFilter.write(outputFile);
    }

    DimensionRecord dr = new DimensionRecord(numRows, numCols);
    outputFile.write(dr);

    // Write out all the rows, in blocks of 32
    for (int block = 0; block < numBlocks; block++)
    {
      DBCellRecord dbcell = new DBCellRecord(outputFile.getPos());

      int blockRows = Math.min(32, numRows - block * 32);
      boolean firstRow = true;

      // First write out all the row records
      for (int i = block * 32; i < block * 32 + blockRows; i++)
      {
        if (rows[i] != null)
        {
          rows[i].write(outputFile);
          if (firstRow)
          {
            dbcell.setCellOffset(outputFile.getPos());
            firstRow = false;
          }
        }
      }

      // Now write out all the cells
      for (int i = block * 32; i < block * 32 + blockRows; i++)
      {
        if (rows[i] != null)
        {
          dbcell.addCellRowPosition(outputFile.getPos());
          rows[i].writeCells(outputFile);
        }
      }

      // Now set the current file position in the index record
      indexRecord.addBlockPosition(outputFile.getPos());
      
      // Set the position of the file pointer and write out the DBCell
      // record
      dbcell.setPosition(outputFile.getPos());
      outputFile.write(dbcell);
    }
    
    // Do the drawings and charts if enabled
    if (!workbookSettings.getDrawingsDisabled())
    {
      drawingWriter.write(outputFile);
    }

    Window2Record w2r = new Window2Record(settings);
    outputFile.write(w2r);

    // Handle the frozen panes
    if (settings.getHorizontalFreeze() != 0 ||
        settings.getVerticalFreeze() != 0)
    {
      PaneRecord pr = new PaneRecord(settings.getHorizontalFreeze(),
                                     settings.getVerticalFreeze());
      outputFile.write(pr);

      // Handle the selection record.  First, there will always be a top left
      SelectionRecord sr = new SelectionRecord
        (SelectionRecord.upperLeft, 0, 0);
      outputFile.write(sr);

      // Top right
      if (settings.getHorizontalFreeze() != 0)
      {
        sr = new SelectionRecord
          (SelectionRecord.upperRight, settings.getHorizontalFreeze(), 0);
        outputFile.write(sr);
      }

      // Bottom left
      if (settings.getVerticalFreeze() != 0)
      {
        sr = new SelectionRecord
          (SelectionRecord.lowerLeft, 0, settings.getVerticalFreeze());
        outputFile.write(sr);
      }

      // Bottom right
      if (settings.getHorizontalFreeze() != 0 &&
          settings.getVerticalFreeze() != 0)
      {
        sr = new SelectionRecord
          (SelectionRecord.lowerRight, 
           settings.getHorizontalFreeze(), 
           settings.getVerticalFreeze());
        outputFile.write(sr);
      }

      Weird1Record w1r = new Weird1Record();
      outputFile.write(w1r);
    }
    else
    {
      // No frozen panes - just write out the selection record for the 
      // whole sheet
      SelectionRecord sr = new SelectionRecord
        (SelectionRecord.upperLeft, 0, 0);
      outputFile.write(sr);
    }

    // Handle the zoom factor
    if (settings.getZoomFactor() != 100)
    {
      SCLRecord sclr = new SCLRecord(settings.getZoomFactor());
      outputFile.write(sclr);
    }

    // Now write out all the merged cells
    mergedCells.write(outputFile);

    // Write out all the hyperlinks
    Iterator hi = hyperlinks.iterator();
    WritableHyperlink hlr = null;
    while (hi.hasNext())
    {
      hlr = (WritableHyperlink) hi.next();
      outputFile.write(hlr);
    }

    if (buttonPropertySet != null)
    {
      outputFile.write(buttonPropertySet);
    }

    // Write out the data validations
    if (dataValidation != null || validatedCells.size() > 0)
    {
      writeDataValidation();
    }

    // Write out the conditional formats
    if (conditionalFormats != null && conditionalFormats.size() > 0)
    {
      for (Iterator i = conditionalFormats.iterator() ; i.hasNext() ; )
      {
        ConditionalFormat cf = (ConditionalFormat) i.next();
        cf.write(outputFile);
      }
    }

    EOFRecord eof = new EOFRecord();
    outputFile.write(eof);

    // Now the various cross reference offsets have been calculated,
    // retrospectively set the values in the output file
    outputFile.setData(indexRecord.getData(), indexPos+4);
  }

  /**
   * Gets the header.  Called when copying sheets
   *
   * @return the page header
   */
  final HeaderRecord getHeader()
  {
    return header;
  }

  /**
   * Gets the footer.  Called when copying sheets
   *
   * @return the page footer
   */
  final FooterRecord getFooter()
  {
    return footer;
  }

  /**
   * Sets the data necessary for writing out the sheet.  This method must
   *  be called immediately prior to writing
   *
   * @param rws the rows in the spreadsheet
   */
  void setWriteData(RowRecord[] rws, 
                    ArrayList   rb,
                    ArrayList   cb,
                    ArrayList   hl,
                    MergedCells mc,
                    TreeSet     cf,
                    int         mrol,
                    int         mcol)
  {
    rows = rws;
    rowBreaks = rb;
    columnBreaks = cb;
    hyperlinks = hl;
    mergedCells = mc;
    columnFormats = cf;
    maxRowOutlineLevel = mrol;
    maxColumnOutlineLevel = mcol;
  }

  /**
   * Sets the dimensions of this spreadsheet.  This method must be called 
   * immediately prior to writing
   *
   * @param rws the number of rows
   * @param cls the number of columns
   */
  void setDimensions(int rws, int cls)
  {
    numRows = rws;
    numCols = cls;
  }

  /**
   * Sets the sheet settings for this particular sheet.  Must be
   * called immediately prior to writing
   * 
   * @param sr the sheet settings
   */
  void setSettings(SheetSettings sr)
  {
    settings = sr;
  }

  /**
   * Accessor for the workspace options
   *
   * @return the workspace options
   */
  WorkspaceInformationRecord getWorkspaceOptions()
  {
    return workspaceOptions;
  }

  /**
   * Accessor for the workspace options
   *
   * @param wo the workspace options
   */
  void setWorkspaceOptions(WorkspaceInformationRecord wo)
  {
    if (wo != null)
    {
      workspaceOptions = wo;
    }
  }


  /**
   * Sets the charts for this sheet
   *
   * @param ch the charts
   */
  void setCharts(Chart[] ch)
  {
    drawingWriter.setCharts(ch);
  }

  /**
   * Sets the drawings on this sheet
   *
   * @param dr the list of drawings
   * @param mod a modified flag
   */
  void setDrawings(ArrayList dr, boolean mod)
  {
    drawingWriter.setDrawings(dr, mod);
  }

  /**
   * Accessor for the charts on this sheet
   *
   * @return the charts
   */
  Chart[] getCharts()
  {
    return  drawingWriter.getCharts();
  }

  /**
   * Check all the merged cells for borders.  If the merge record has
   * borders, then we need to rejig the cell formats to take account of this.
   * This is called by the write method of the WritableWorkbookImpl, so that
   * any new XFRecords that are created may be written out with the others
   */
  void checkMergedBorders()
  {
    Range[] mcells = mergedCells.getMergedCells();
    ArrayList borderFormats = new ArrayList();
    for (int mci = 0 ; mci < mcells.length ; mci++)
    {
      Range range = mcells[mci];
      Cell topLeft = range.getTopLeft();
      XFRecord tlformat = (XFRecord) topLeft.getCellFormat();

      if (tlformat != null && 
          tlformat.hasBorders() == true && 
          !tlformat.isRead())
      {
        try
        {
          CellXFRecord cf1 = new CellXFRecord(tlformat);
          Cell bottomRight = range.getBottomRight();

          cf1.setBorder(Border.ALL, BorderLineStyle.NONE, Colour.BLACK);
          cf1.setBorder(Border.LEFT, 
                        tlformat.getBorderLine(Border.LEFT), 
                        tlformat.getBorderColour(Border.LEFT));
          cf1.setBorder(Border.TOP,  
                        tlformat.getBorderLine(Border.TOP),
                        tlformat.getBorderColour(Border.TOP));

          if (topLeft.getRow() == bottomRight.getRow())
          {
            cf1.setBorder(Border.BOTTOM, 
                          tlformat.getBorderLine(Border.BOTTOM),
                          tlformat.getBorderColour(Border.BOTTOM));
          }

          if (topLeft.getColumn() == bottomRight.getColumn())
          {

⌨️ 快捷键说明

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