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

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

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

import kz.greetgo.msoffice.util.UtilOffice;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class Content {

  private final Xlsx xlsx;

  private final List sheets = new ArrayList();
  final CoreProperties coreProperties = new CoreProperties();
  private String workDir;

  Content(Xlsx xlsx) {
    this.xlsx = xlsx;
  }

  public void addSheet(Sheet sheet) {
    sheets.add(sheet);
  }

  public void setWorkDir(String workDir) {
    this.workDir = workDir;
  }

  public void finish() throws Exception {
    printContentTypes();
    printWorkbook();
    printThemes();
    printDrawings();
    printCharts();
    printXl_rels();
    print_rels();
    printAppProperties();
    printCoreProperties();
  }

  private void printContentTypes() throws Exception {
    PrintStream out = new PrintStream(workDir + "/[Content_Types].xml", "UTF-8");
    out.println("");

    out.println("");
    out.println("");
    out.println("");
    out.println("");
    out.println("");

    for (String ext : xlsx.imageexts)
      out.println("");

    out.println("");
    out.println("");

    for (Chart chart : xlsx.getCharts()) {
      out.print("");
    }

    for (Sheet sheet : sheets) {
      out.println("");

      if (sheet.getDrawing().size() < 1) continue;

      out.print("");
    }

    out.println("");

    out.println("");
    out.println("");

    out.close();
  }

  private void printWorkbook() throws Exception {
    String dir = workDir + "/xl";
    new File(dir).mkdirs();
    PrintStream out = new PrintStream(dir + "/workbook.xml", "UTF-8");
    out.println("");

    out.println("");
    out.println("");
    out.println("");
    out.println("");

    int activeIndex = 0;
    for (Sheet sheet : sheets) {
      if (sheet.selected) break;
      activeIndex++;
    }

    out.println("");
    out.println("");
    out.println("");

    int id = 1;
    for (Sheet sheet : sheets) {
      out.println("");
      id++;
    }

    out.println("");
    out.println("");
    out.println("");

    out.close();
  }

  private void printThemes() throws Exception {
    String dir = workDir + "/xl/theme";
    new File(dir).mkdirs();

    OutputStream out = new FileOutputStream(dir + "/theme1.xml");
    InputStream in = getClass().getResourceAsStream("theme1.xml");

    UtilOffice.copyStreams(in, out);

    in.close();
    out.close();
  }

  private void printXl_rels() throws Exception {
    String dir = workDir + "/xl/_rels";
    new File(dir).mkdirs();
    PrintStream out = new PrintStream(dir + "/workbook.xml.rels", "UTF-8");
    out.println("");

    out.println("");

    int id = 1;
    for (Sheet sheet : sheets) {
      out.println("");
      id++;
    }

    out.println("");

    out.println("");

    out.println("");

    out.println("");

    out.close();
  }

  private void print_rels() throws Exception {
    String dir = workDir + "/_rels";
    new File(dir).mkdirs();
    PrintStream out = new PrintStream(dir + "/.rels", "UTF-8");
    out.println("");

    out.println("");

    out.println("");

    out.println("");

    out.println("");

    out.println("");

    out.close();
  }

  private void printAppProperties() throws Exception {
    String dir = workDir + "/docProps";
    new File(dir).mkdirs();
    PrintStream out = new PrintStream(dir + "/app.xml", "UTF-8");
    out.println("");

    out.println("");
    out.println("Microsoft Excel");
    out.println("0");
    out.println("false");

    out.println("");
    out.println("");
    out.println("");
    out.println("Листы");
    out.println("");
    out.println("");
    out.println("" + sheets.size() + "");
    out.println("");
    out.println("");
    out.println("");

    out.println("");
    out.println("");
    for (Sheet sheet : sheets) {
      out.println("" + sheet.getDisplayName() + "");
    }
    out.println("");
    out.println("");

    out.println("Microsoft");
    out.println("false");
    out.println("false");
    out.println("false");
    out.println("12.0000");

    out.println("");

    out.close();
  }

  private void printCoreProperties() throws Exception {
    String dir = workDir + "/docProps";
    new File(dir).mkdirs();
    PrintStream out = new PrintStream(dir + "/core.xml", "UTF-8");
    coreProperties.print(out);
    out.close();
  }

  private void printCharts() throws Exception {

    if (xlsx.getCharts().size() < 1) return;

    String dir = workDir + "/xl/charts";
    new File(dir).mkdirs();

    for (Chart chart : xlsx.getCharts()) {
      PrintStream os = new PrintStream(dir + "/chart" + chart.getFileId() + ".xml", "UTF-8");
      chart.print(os);
      os.close();
    }
  }

  private void printDrawings() throws Exception {

    for (Sheet sheet : sheets) {
      if (sheet.getDrawing().size() < 1) continue;

      String dir = workDir + "/xl/drawings";
      new File(dir).mkdirs();
      String dirrel = workDir + "/xl/drawings/_rels";
      new File(dirrel).mkdirs();

      PrintStream os = new PrintStream(dir + "/drawing" + sheet.getDrawingId() + ".xml", "UTF-8");
      os.println("");
      os.println("");

      PrintStream osrel = new PrintStream(dirrel + "/drawing" + sheet.getDrawingId() + ".xml.rels",
        "UTF-8");
      osrel.println("");
      osrel
        .println("");

      for (TwoCellAnchor anch : sheet.getDrawing()) {
        anch.print(os);

        if (anch instanceof TwoCellAnchorImage) continue;

        osrel.print("");
      }

      for (Map.Entry e : sheet.getImagesRels().entrySet()) {
        osrel.print("");
      }

      os.print("");
      os.close();

      osrel.print("");
      osrel.close();
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy