All Downloads are FREE. Search and download functionalities are using the official Maven repository.

kz.greetgo.msoffice.xlsx.gen.Sheet Maven / Gradle / Ivy

There is a newer version: 0.5.9
Show newest version
package kz.greetgo.msoffice.xlsx.gen;

import kz.greetgo.msoffice.ImageType;
import kz.greetgo.msoffice.util.UtilOffice;
import kz.greetgo.msoffice.xlsx.parse.SharedStrings;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.PrintStream;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;

public class Sheet {

  private final Xlsx parent;

  private final Style style;
  private final String name;
  private final String workDir;
  private final SharedStrings strs;
  private double defaultRowHeight = 15;
  private boolean summaryBelow = true;
  private boolean summaryRight = true;

  private int curRow = 0;
  private boolean inRow = false;

  private final Map widths = new HashMap();
  private final Set colhiddens = new HashSet();
  private final Map heights = new HashMap();
  final boolean selected;
  private boolean started = false;
  private final boolean finished = false;
  private PrintStream out;
  private final PageMargins pageMargins = new PageMargins();
  private final PageSetup pageSetup = new PageSetup();
  private String displayName;
  private final MergeCells mergeCells = new MergeCells();

  private int lastColumn;

  private final List drawing = new ArrayList<>(); // графические объекты листа
  private Integer drawingFileId; // номер файла графики для листа
  private int drawingRelIdLast = 0; // счётчик ссылок графических объектов внутри файла графики для листа
  private Map imagesrels = new HashMap<>(); // картинка -> ссылка графического объекта в листе   

  public PageMargins pageMargins() {
    return pageMargins;
  }

  public PageSetup pageSetup() {
    return pageSetup;
  }

  /**
   * Установка положения страницы
   *
   * @param orientation положение страницы: горизонтальное Landscape, вертикальное Portrait
   */
  public void setPageOrientation(PageSetup.Orientation orientation) {
    pageSetup.setOrientation(orientation);
  }

  /**
   * Установка формата бумаги
   *
   * @param paperSize формат бумаги: A4, A5, Letter, etc
   */
  public void setPageSize(PageSetup.PaperSize paperSize) {
    pageSetup.setPaperSize(paperSize);
  }

  /**
   * При печати подгонять масштаб под ширину страницы
   */
  public void setScaleByWidth() {
    pageSetup.setFitToHeight(0);
    pageSetup.setFitToWidth(1);
  }

  public String getDisplayName() {
    return displayName;
  }

  public void setDisplayName(String displayName) {
    this.displayName = displayName;
  }

  public double getDefaultRowHeight() {
    return defaultRowHeight;
  }

  public void setDefaultRowHeight(double defaultRowHeight) {
    this.defaultRowHeight = defaultRowHeight;
  }

  public Sheet cellFormula(int col, String formulaValue, NumFmt numFmt) {
    init();
    setLastColumn(col);
    style.numFmt = numFmt;

    if (formulaValue == null) {
      int styleIndex = style.index();
      String pos = UtilOffice.toTablePosition(curRow, col - 1);
      out.printf("", pos, styleIndex);
      out.println();
    } else {
      int styleIndex = style.index();
      String pos = UtilOffice.toTablePosition(curRow, col - 1);
      out.printf("%s", pos, styleIndex, formulaValue);
      out.println();
    }
    return this;
  }

  public Sheet cellFormula(int col, String formulaValue) {
    init();
    setLastColumn(col);
    style.clearNumFmt();

    if (formulaValue == null) {
      int styleIndex = style.index();
      String pos = UtilOffice.toTablePosition(curRow, col - 1);
      out.printf("", pos, styleIndex);
      out.println();
    } else {
      int styleIndex = style.index();
      String pos = UtilOffice.toTablePosition(curRow, col - 1);
      out.printf("%s", pos, styleIndex, formulaValue);
      out.println();
    }
    return this;
  }

  public Sheet cellInlineStr(int col, String strValue) {
    init();
    setLastColumn(col);
    style.clearNumFmt();

    if (strValue == null) {
      int styleIndex = style.index();
      String pos = UtilOffice.toTablePosition(curRow, col - 1);
      out.printf("", pos, styleIndex);
      out.println();
    } else {
      int styleIndex = style.index();
      String pos = UtilOffice.toTablePosition(curRow, col - 1);
      out.printf("" + strValue + "", pos,
        styleIndex);
      out.println();
    }
    return this;
  }

  public Sheet cellStr(int col, String strValue) {
    init();
    setLastColumn(col);
    style.clearNumFmt();

    if (strValue == null) {
      int styleIndex = style.index();
      String pos = UtilOffice.toTablePosition(curRow, col - 1);
      out.printf("", pos, styleIndex);
      out.println();
    } else {
      int styleIndex = style.index();
      int strIndex = strs.index(strValue);
      String pos = UtilOffice.toTablePosition(curRow, col - 1);
      out.printf("%d", pos, styleIndex, strIndex);
      out.println();
    }
    return this;
  }

  private void setLastColumn(int col) {
    if (col <= 0) throw new IllegalArgumentException("must be col > 0, but col = " + col);
    if (col <= lastColumn) throw new IllegalArgumentException("must be col > " + lastColumn
      + ", but col = " + col);
    lastColumn = col;
  }

  public Sheet cellInt(int col, int intValue) {
    init();
    setLastColumn(col);
    style.clearNumFmt();

    int styleIndex = style.index();
    String pos = UtilOffice.toTablePosition(curRow, col - 1);
    out.printf("%d", pos, styleIndex, intValue);
    out.println();
    return this;
  }

  public Sheet cellDouble(int col, double doubleValue, NumFmt numFmt) {
    init();
    setLastColumn(col);
    style.numFmt = numFmt;

    int styleIndex = style.index();
    String pos = UtilOffice.toTablePosition(curRow, col - 1);
    out.printf("" + doubleValue + "", pos, styleIndex);
    out.println();
    return this;
  }

  public Sheet cellDouble(int col, double doubleValue) {
    init();
    setLastColumn(col);
    style.clearNumFmt();

    int styleIndex = style.index();
    String pos = UtilOffice.toTablePosition(curRow, col - 1);
    out.printf("" + doubleValue + "", pos, styleIndex);
    out.println();
    return this;
  }

  private Sheet cellDate(int col, Date date, NumFmt numFmt) {
    init();
    setLastColumn(col);
    style.numFmt = numFmt;

    int styleIndex = style.index();
    String pos = UtilOffice.toTablePosition(curRow, col - 1);
    out.printf("%s", pos, styleIndex,
      UtilOffice.toExcelDateTime(date));
    out.println();
    return this;
  }

  public Sheet cellYMD(int col, Date date) {
    return cellDate(col, date, NumFmt.YMD);
  }

  public Sheet cellYMD_HMS(int col, Date date) {
    return cellDate(col, date, NumFmt.YMD_HMS);
  }

  public Sheet cellDMY(int col, Date date) {
    return cellDate(col, date, NumFmt.DMY);
  }

  public void skipRows(int rowCount) {
    if (rowCount <= 0) {
      throw new IllegalArgumentException("rowCount must be > 0");
    }
    init();
    innerFinishRow();
    curRow += rowCount;
  }

  public void innerFinishRow() {
    if (!inRow) return;
    out.println("");
    inRow = false;
  }

  public void skipRow() {
    init();
    skipRows(1);
  }

  public class Row {
    private Double height = null;
    private boolean collapsed = false;
    private boolean hidden = false;
    private int outlineLevel = 0;

    private Row() {}

    public Row height(double height) {
      this.height = height;
      return this;
    }

    public Row collapsed() {
      collapsed = true;
      return this;
    }

    public Row hidden() {
      hidden = true;
      return this;
    }

    public Row hidden(boolean hidden) {
      this.hidden = hidden;
      return this;
    }

    public Row collapsed(boolean collapsed) {
      this.collapsed = collapsed;
      return this;
    }

    public Row outline() {
      outlineLevel = 1;
      return this;
    }

    public Row outline(boolean hidden) {
      outlineLevel = 1;
      this.hidden = hidden;
      return this;
    }

    public Row hiddenOutline() {
      outlineLevel = 1;
      hidden = true;
      return this;
    }

    public Row outlineLevel(int outlineLevel) {
      if (outlineLevel < 1) throw new IllegalArgumentException("outlineLevel must be > 0");
      this.outlineLevel = outlineLevel;
      return this;
    }

    public Row outlineLevel(int outlineLevel, boolean hidden) {
      if (outlineLevel < 1) throw new IllegalArgumentException("outlineLevel must be > 0");
      this.hidden = hidden;
      this.outlineLevel = outlineLevel;
      return this;
    }

    public Row start() {
      init();
      innerFinishRow();

      lastColumn = 0;

      StringBuilder after = new StringBuilder();
      out.printf("




© 2015 - 2024 Weber Informatics LLC | Privacy Policy