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

📄 excelprintsetupfactory.java

📁 swing编写的库存管理程序。毕业设计类
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
      ((short) 40, PageFormatFactory.FANFOLDGERMAN[0], PageFormatFactory.FANFOLDGERMAN[1]);

  /** A standard page format mapping for excel. */
  public static final ExcelPageDefinition FANFOLDGERMANLEGAL = new ExcelPageDefinition
      ((short) 41, PageFormatFactory.FANFOLDGERMANLEGAL[0], 
          PageFormatFactory.FANFOLDGERMANLEGAL[1]);

  /**
   * A read only page format mapping definiton to map a page format to an
   * predefined excel constant.
   */
  public static class ExcelPageDefinition
  {
    /** The excel internal page format code referring to that page size. */
    private final short pageFormatCode;
    /** The width of the page format. */
    private final int width;
    /** The height of the page format. */
    private final int height;

    /**
     * Defines a new excel page format mapping.
     *
     * @param pageFormatCode the excel internal page format code.
     * @param width the width of the page.
     * @param height the height of the page.
     */
    public ExcelPageDefinition(final short pageFormatCode, final int width, final int height)
    {
      this.pageFormatCode = pageFormatCode;
      this.width = width;
      this.height = height;
    }

    /**
     * Return the excel page format code that describes that page size.
     *
     * @return the page format code as defined in the Excel File format.
     */
    public short getPageFormatCode()
    {
      return pageFormatCode;
    }

    /**
     * Returns the defined page width for that page definition.
     *
     * @return the page width;
     */
    public int getWidth()
    {
      return width;
    }

    /**
     * Returns the defined page height for that page definition.
     *
     * @return the page height;
     */
    public int getHeight()
    {
      return height;
    }
  }

  /**
   * Default Constructor.
   */
  private ExcelPrintSetupFactory()
  {
  }

  /**
   * Performs the page setup and searches a matching page format for the
   * report.
   *
   * @param printSetup the print setup object of the current sheet.
   * @param pageformat the pageformat defined for the report.
   * @param paperdef the excel paper size property (may be null).
   * @param paperOrientation the paper orientation, either "Landscape" or "Portrait"
   */
  public static void performPageSetup(final HSSFPrintSetup printSetup,
                                      final PageFormat pageformat,
                                      final String paperdef, final String paperOrientation)
  {
    short pageCode = parsePaperSizeProperty(paperdef);
    if (pageCode == -1)
    {
      pageCode = computePaperSize(pageformat);
    }
    if (pageCode != -1)
    {
      printSetup.setPaperSize(pageCode);
    }
    if (paperOrientation != null)
    {
      printSetup.setLandscape(paperOrientation.equalsIgnoreCase("Landscape"));
    }
    else
    {
      printSetup.setLandscape(pageformat.getOrientation() != PageFormat.PORTRAIT);
    }
  }

  /**
   * Searches all defined excel page formats to find a page format that
   * matches the given pageformat. If no matching format was found, the
   * next greater page format is used.
   * <p>
   * If no page format fits the definition, -1 is returned.
   *
   * @param format the page format
   * @return the computed paper size or -1 if no paper size matches the
   * requirements
   */
  private static short computePaperSize(final PageFormat format)
  {
    ExcelPageDefinition pageDef = null;
    final int width = (int) format.getPaper().getWidth();
    final int height = (int) format.getPaper().getHeight();
    int delta = -1;

    final Field[] fields = ExcelPrintSetupFactory.class.getDeclaredFields();
    for (int i = 0; i < fields.length; i++)
    {
      final Field field = fields[i];
      if (ExcelPageDefinition.class.isAssignableFrom(field.getType()) == false)
      {
        continue;
      }
      if (Modifier.isStatic(field.getModifiers()) == false)
      {
        // is no static field, who defined it here?
        continue;
      }
      try
      {
        final ExcelPageDefinition pageformat = (ExcelPageDefinition) field.get(null);
        if ((pageformat.getWidth() < width) || (pageformat.getHeight() < height))
        {
          // paper is too small, ignore it
          continue;
        }
        final int newDelta = (pageformat.getWidth() - width) + (pageformat.getHeight() - height);
        if ((delta == -1) || (newDelta < delta))
        {
          pageDef = pageformat;
          delta = newDelta;
        }
      }
      catch (IllegalAccessException iae)
      {
        // ignore ..
      }
    }
    if (pageDef == null)
    {
      return -1;
    }
    else
    {
      return pageDef.getPageFormatCode();
    }
  }

  /**
   * Parses the defined paper size for the excel sheets. The paper size can
   * be defined using the report configuration properties.
   *
   * @param paper the paper constant for the excel page size.
   * @return the parsed HSSF paper size constant or -1 if undefined.
   */
  private static short parsePaperSizeProperty(final String paper)
  {
    if (paper == null)
    {
      return -1;
    }
    try
    {
      final Field field = ExcelPrintSetupFactory.class.getDeclaredField(paper);
      if (ExcelPageDefinition.class.isAssignableFrom(field.getType()) == false)
      {
        return -1;
      }
      final Object o = field.get(null);
      final ExcelPageDefinition pageformat = (ExcelPageDefinition) o;
      return pageformat.getPageFormatCode();
    }
    catch (NoSuchFieldException nfe)
    {
      return -1;
    }
    catch (IllegalAccessException aie)
    {
      return -1;
    }
  }

}

⌨️ 快捷键说明

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