Ejemplo n.º 1
0
  @Test
  public void exportWidthSplitTest() {
    SBook book = SBooks.createBook("book1");
    SSheet sheet1 = book.createSheet("Sheet1");
    int defaultWidth = 100;
    sheet1.setDefaultColumnWidth(defaultWidth);
    sheet1.setDefaultRowHeight(200);
    Assert.assertEquals(defaultWidth, sheet1.getDefaultColumnWidth());
    Assert.assertEquals(200, sheet1.getDefaultRowHeight());

    Iterator<SColumnArray> arrays = sheet1.getColumnArrayIterator();
    Assert.assertFalse(arrays.hasNext());

    Assert.assertNull(sheet1.getColumnArray(0));

    sheet1.setupColumnArray(0, 8).setWidth(10);
    sheet1.setupColumnArray(11, 255);
    arrays = sheet1.getColumnArrayIterator();
    SColumnArray array = arrays.next();
    Assert.assertEquals(0, array.getIndex());
    Assert.assertEquals(8, array.getLastIndex());
    Assert.assertEquals(10, array.getWidth());

    array = arrays.next();
    Assert.assertEquals(9, array.getIndex());
    Assert.assertEquals(10, array.getLastIndex());
    Assert.assertEquals(defaultWidth, array.getWidth());

    array = arrays.next();
    Assert.assertEquals(11, array.getIndex());
    Assert.assertEquals(255, array.getLastIndex());
    Assert.assertEquals(defaultWidth, array.getWidth());

    ///////////// first export
    File outFile =
        ImExpTestUtil.writeBookToFile(
            book,
            ImExpTestUtil.DEFAULT_EXPORT_TARGET_PATH + ImExpTestUtil.DEFAULT_EXPORT_FILE_NAME_XLSX,
            EXPORTER_TYPE);
    SBook outBook = ImExpTestUtil.loadBook(outFile, "OutBook");

    sheet1 = outBook.getSheet(0);

    // default width become 104px
    // Assert.assertEquals(defaultWidth, sheet1.getDefaultColumnWidth());
    Assert.assertEquals(200, sheet1.getDefaultRowHeight());

    arrays = sheet1.getColumnArrayIterator();
    Assert.assertTrue(arrays.hasNext());

    arrays = sheet1.getColumnArrayIterator();
    array = arrays.next();
    Assert.assertEquals(0, array.getIndex());
    Assert.assertEquals(8, array.getLastIndex());
    Assert.assertEquals(10, array.getWidth());

    array = arrays.next();
    Assert.assertEquals(9, array.getIndex());
    Assert.assertEquals(10, array.getLastIndex());
    Assert.assertEquals(defaultWidth, array.getWidth());

    array = arrays.next();
    Assert.assertEquals(11, array.getIndex());
    Assert.assertEquals(255, array.getLastIndex());
    Assert.assertEquals(defaultWidth, array.getWidth());

    ///////////// second export
    File outFile2 =
        ImExpTestUtil.writeBookToFile(
            outBook,
            ImExpTestUtil.DEFAULT_EXPORT_TARGET_PATH + ImExpTestUtil.DEFAULT_EXPORT_FILE_NAME_XLSX,
            EXPORTER_TYPE);
    SBook outBook2 = ImExpTestUtil.loadBook(outFile2, "OutBook");

    sheet1 = outBook2.getSheet(0);

    // default width become 104px
    // Assert.assertEquals(100, sheet1.getDefaultColumnWidth());
    Assert.assertEquals(200, sheet1.getDefaultRowHeight());

    arrays = sheet1.getColumnArrayIterator();
    Assert.assertTrue(arrays.hasNext());

    arrays = sheet1.getColumnArrayIterator();
    array = arrays.next();
    Assert.assertEquals(0, array.getIndex());
    Assert.assertEquals(8, array.getLastIndex());
    Assert.assertEquals(10, array.getWidth());

    array = arrays.next();
    Assert.assertEquals(9, array.getIndex());
    Assert.assertEquals(10, array.getLastIndex());
    Assert.assertEquals(100, array.getWidth());

    array = arrays.next();
    Assert.assertEquals(11, array.getIndex());
    Assert.assertEquals(255, array.getLastIndex());
    Assert.assertEquals(100, array.getWidth());
  }
  /*
   * import sheet scope content from POI Sheet.
   */
  protected SSheet importSheet(Sheet poiSheet, int poiSheetIndex) {
    SSheet sheet = book.createSheet(poiSheet.getSheetName());
    sheet.setDefaultRowHeight(UnitUtil.twipToPx(poiSheet.getDefaultRowHeight()));
    // ZSS-952
    importSheetDefaultColumnWidth(poiSheet, sheet);
    // reference FreezeInfoLoaderImpl.getRowFreeze()
    sheet.getViewInfo().setNumOfRowFreeze(BookHelper.getRowFreeze(poiSheet));
    sheet.getViewInfo().setNumOfColumnFreeze(BookHelper.getColumnFreeze(poiSheet));
    sheet
        .getViewInfo()
        .setDisplayGridlines(
            poiSheet
                .isDisplayGridlines()); // Note isDisplayGridlines() and isPrintGridlines() are
                                        // different
    sheet.getViewInfo().setColumnBreaks(poiSheet.getColumnBreaks());
    sheet.getViewInfo().setRowBreaks(poiSheet.getRowBreaks());

    SPrintSetup sps = sheet.getPrintSetup();

    SHeader header = sheet.getViewInfo().getHeader();
    if (header != null) {
      header.setCenterText(poiSheet.getHeader().getCenter());
      header.setLeftText(poiSheet.getHeader().getLeft());
      header.setRightText(poiSheet.getHeader().getRight());
      sps.setHeader(header);
    }

    SFooter footer = sheet.getViewInfo().getFooter();
    if (footer != null) {
      footer.setCenterText(poiSheet.getFooter().getCenter());
      footer.setLeftText(poiSheet.getFooter().getLeft());
      footer.setRightText(poiSheet.getFooter().getRight());
      sps.setFooter(footer);
    }

    if (poiSheet.isDiffOddEven()) {
      Header poiEvenHeader = poiSheet.getEvenHeader();
      if (poiEvenHeader != null) {
        SHeader evenHeader = new HeaderFooterImpl();
        evenHeader.setCenterText(poiEvenHeader.getCenter());
        evenHeader.setLeftText(poiEvenHeader.getLeft());
        evenHeader.setRightText(poiEvenHeader.getRight());
        sps.setEvenHeader(evenHeader);
      }
      Footer poiEvenFooter = poiSheet.getEvenFooter();
      if (poiEvenFooter != null) {
        SFooter evenFooter = new HeaderFooterImpl();
        evenFooter.setCenterText(poiEvenFooter.getCenter());
        evenFooter.setLeftText(poiEvenFooter.getLeft());
        evenFooter.setRightText(poiEvenFooter.getRight());
        sps.setEvenFooter(evenFooter);
      }
    }

    if (poiSheet.isDiffFirst()) {
      Header poiFirstHeader = poiSheet.getFirstHeader();
      if (poiFirstHeader != null) {
        SHeader firstHeader = new HeaderFooterImpl();
        firstHeader.setCenterText(poiFirstHeader.getCenter());
        firstHeader.setLeftText(poiFirstHeader.getLeft());
        firstHeader.setRightText(poiFirstHeader.getRight());
        sps.setFirstHeader(firstHeader);
      }
      Footer poiFirstFooter = poiSheet.getFirstFooter();
      if (poiFirstFooter != null) {
        SFooter firstFooter = new HeaderFooterImpl();
        firstFooter.setCenterText(poiFirstFooter.getCenter());
        firstFooter.setLeftText(poiFirstFooter.getLeft());
        firstFooter.setRightText(poiFirstFooter.getRight());
        sps.setFirstFooter(firstFooter);
      }
    }

    PrintSetup poips = poiSheet.getPrintSetup();

    sps.setBottomMargin(poiSheet.getMargin(Sheet.BottomMargin));
    sps.setTopMargin(poiSheet.getMargin(Sheet.TopMargin));
    sps.setLeftMargin(poiSheet.getMargin(Sheet.LeftMargin));
    sps.setRightMargin(poiSheet.getMargin(Sheet.RightMargin));
    sps.setHeaderMargin(poiSheet.getMargin(Sheet.HeaderMargin));
    sps.setFooterMargin(poiSheet.getMargin(Sheet.FooterMargin));

    sps.setAlignWithMargins(poiSheet.isAlignMargins());
    sps.setErrorPrintMode(poips.getErrorsMode());
    sps.setFitHeight(poips.getFitHeight());
    sps.setFitWidth(poips.getFitWidth());
    sps.setHCenter(poiSheet.getHorizontallyCenter());
    sps.setLandscape(poips.getLandscape());
    sps.setLeftToRight(poips.getLeftToRight());
    sps.setPageStart(poips.getUsePage() ? poips.getPageStart() : 0);
    sps.setPaperSize(PoiEnumConversion.toPaperSize(poips.getPaperSize()));
    sps.setCommentsMode(poips.getCommentsMode());
    sps.setPrintGridlines(poiSheet.isPrintGridlines());
    sps.setPrintHeadings(poiSheet.isPrintHeadings());

    sps.setScale(poips.getScale());
    sps.setScaleWithDoc(poiSheet.isScaleWithDoc());
    sps.setDifferentOddEvenPage(poiSheet.isDiffOddEven());
    sps.setDifferentFirstPage(poiSheet.isDiffFirst());
    sps.setVCenter(poiSheet.getVerticallyCenter());

    Workbook poiBook = poiSheet.getWorkbook();
    String area = poiBook.getPrintArea(poiSheetIndex);
    if (area != null) {
      sps.setPrintArea(area);
    }

    CellRangeAddress rowrng = poiSheet.getRepeatingRows();
    if (rowrng != null) {
      sps.setRepeatingRowsTitle(rowrng.getFirstRow(), rowrng.getLastRow());
    }

    CellRangeAddress colrng = poiSheet.getRepeatingColumns();
    if (colrng != null) {
      sps.setRepeatingColumnsTitle(colrng.getFirstColumn(), colrng.getLastColumn());
    }

    sheet.setPassword(poiSheet.getProtect() ? "" : null);

    // import hashed password directly
    importPassword(poiSheet, sheet);

    // ZSS-832
    // import sheet visible
    if (poiBook.isSheetHidden(poiSheetIndex)) {
      sheet.setSheetVisible(SheetVisible.HIDDEN);
    } else if (poiBook.isSheetVeryHidden(poiSheetIndex)) {
      sheet.setSheetVisible(SheetVisible.VERY_HIDDEN);
    } else {
      sheet.setSheetVisible(SheetVisible.VISIBLE);
    }

    // ZSS-1130
    // import conditionalFormatting
    importConditionalFormatting(sheet, poiSheet);
    return sheet;
  }