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

kz.greetgo.msoffice.xlsx.fastgen.simple.SimpleFastXlsxFile Maven / Gradle / Ivy

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

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.util.UUID;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

import static kz.greetgo.msoffice.util.UtilOffice.toTablePosition;

public class SimpleFastXlsxFile {

  private final String workingDir;
  private int sheetNumber = 0;
  private double[] colWidths;

  private PrintStream outCurrentSheet = null;
  private PrintStream outSharedStrings = null;

  public SimpleFastXlsxFile(String tmpDir) {
    workingDir = tmpDir + "/" + UUID.randomUUID().toString();
    new File(workingDir).mkdirs();

    try {

      prepareFiles();
    } catch (Exception e) {
      throw new RuntimeException(e);
    }
  }

  private void prepareFiles() throws Exception {
    ZipInputStream zin = new ZipInputStream(
      getClass().getResourceAsStream("SimpleXlsxFile.template.xlsx"));
    try {

      byte[] buf = new byte[1024 * 4];

      while (true) {
        ZipEntry entry = zin.getNextEntry();
        if (entry == null) break;
        if (entry.getName().endsWith("/")) continue;

        File entryFile = new File(workingDir + "/zip/" + entry.getName());

        entryFile.getParentFile().mkdirs();

        OutputStream fout = new FileOutputStream(entryFile);
        try {

          IN:
          while (true) {
            int count = zin.read(buf);
            if (count < 0) break IN;
            fout.write(buf, 0, count);
          }

        } finally {
          fout.close();
        }

        zin.closeEntry();
      }
    } finally {
      zin.close();
    }

    openSharedStrings();
  }

  public void newSheet(double[] colWidths) {
    this.colWidths = colWidths;

    closeCurrentSheet();
    sheetNumber++;
    try {
      outCurrentSheet = new PrintStream(
        workingDir + "/zip/xl/worksheets/sheet" + sheetNumber + ".xml", "UTF-8");
      outCurrentSheet.println("");
      outCurrentSheet
        .println("\n"
          + "  \n" + "  \n"
          + "    \n"
          + "      \n" + "    \n"
          + "  \n" + "  ");
      outCurrentSheet.println("");
      for (int i = 1; i <= colWidths.length; i++) {
        outCurrentSheet.println("");
      }
      outCurrentSheet.println("");
      outCurrentSheet.println("");
    } catch (Exception e) {
      throw new RuntimeException(e);
    }

    currentRow = 1;
  }

  private int currentRow;

  public void appendRow(SimpleRowStyle rowStyle, String[] rowData) {
    if (rowData == null) throw new NullPointerException("rowData == null");
    if (rowData.length != colWidths.length) throw new NullPointerException(
      "rowData.length == " + rowData.length + " but must be == " + colWidths.length);
    if (sheetNumber <= 0) throw new RuntimeException("No sheet");
    outCurrentSheet.print("");
    for (int i = 0, C = rowData.length; i < C; i++) {
      String r = " r = \"" + toTablePosition(currentRow, i) + '"';
      String s = getRowStyle(rowStyle);

      String value = rowData[i];

      if (value == null) {
        outCurrentSheet.println("");
      } else {
        outCurrentSheet.println("" + str(rowData[i]) + "");
      }
    }
    outCurrentSheet.println("");
    currentRow++;
  }

  private String getRowStyle(SimpleRowStyle rowStyle) {
    switch (rowStyle) {
      case NORMAL:
        return "";

      case GREEN:
        return " s=\"1\"";

      default:
        throw new IllegalArgumentException("Unknown rowStyle = " + rowStyle);
    }

  }

  public void complete(OutputStream output) {
    try {

      try {
        complete0(output);
      } finally {
        output.close();
      }

    } catch (Exception e) {
      if (e instanceof RuntimeException) throw (RuntimeException) e;
      throw new RuntimeException(e);
    }
  }

  private void complete0(OutputStream output) throws Exception {
    closeCurrentSheet();

    ZipOutputStream zout = new ZipOutputStream(output);

    byte[] buffer = new byte[1024 * 4];

    new File(workingDir + "/zip/xl/workbook.xml").delete();
    new File(workingDir + "/zip/[Content_Types].xml").delete();
    new File(workingDir + "/zip/xl/_rels/workbook.xml.rels").delete();
    new File(workingDir + "/zip/docProps/app.xml").delete();

    new File(workingDir + "/zip/xl/sharedStrings.xml").delete();

    copyAll(zout, null, workingDir + "/zip", buffer);

    printWorkbook(zout);
    printContentTypes(zout);
    printWorkbookXmlRels(zout);
    printApp(zout);

    printSharedStrings(zout);
  }

  private void copyAll(ZipOutputStream zout, String localDir, String realDir, byte[] buffer)
    throws Exception {
    for (String name : new File(realDir).list(null)) {
      File file = new File(realDir + "/" + name);
      String localName = localDir == null ? name : localDir + "/" + name;
      if (file.isDirectory()) {
        copyAll(zout, localName, realDir + "/" + name, buffer);
      } else if (file.isFile()) {
        zout.putNextEntry(new ZipEntry(localName));
        FileInputStream fin = new FileInputStream(file);
        try {
          while (true) {
            int count = fin.read(buffer);
            if (count < 0) break;
            zout.write(buffer, 0, count);
          }
        } finally {
          fin.close();
        }
        zout.closeEntry();
      }
    }
  }

  private void closeCurrentSheet() {
    if (outCurrentSheet == null) return;

    outCurrentSheet.print("");
    outCurrentSheet.close();
    outCurrentSheet = null;
  }

  private int newStringIndex = 0;

  private void openSharedStrings() throws Exception {
    outSharedStrings = new PrintStream(workingDir + "/sharedStrings.xml", "UTF-8");
  }

  private int str(String s) {
    s = s.replaceAll("<", "<");
    outSharedStrings.print("" + s + "");
    return newStringIndex++;
  }

  private void printWorkbook(ZipOutputStream zout) throws Exception {
    zout.putNextEntry(new ZipEntry("xl/workbook.xml"));

    PrintStream out = new PrintStream(zout, false, "UTF-8");

    out.println("");
    out.println(
      "\n"
        + "  \n"
        + "  \n" + "  \n"
        + "    \n"
        + "  ");

    out.println("");
    for (int i = 0; i < sheetNumber; i++) {
      out.println("");
    }
    out.println("");

    out.println("\n" + "");

    out.flush();

    zout.closeEntry();

  }

  private void printContentTypes(ZipOutputStream zout) throws Exception {
    zout.putNextEntry(new ZipEntry("[Content_Types].xml"));

    PrintStream out = new PrintStream(zout, false, "UTF-8");

    out.println("");
    out.println("\n"
      + "  \n"
      + "  \n"
      + "  \n"
      + "  \n"
      + "  \n"
      + "  \n"
      + "  \n"
      + "  ");

    for (int i = 0; i < sheetNumber; i++) {
      out.println("");
    }
    out.println("");

    out.flush();

    zout.closeEntry();
  }

  private void printWorkbookXmlRels(ZipOutputStream zout) throws Exception {

    zout.putNextEntry(new ZipEntry("xl/_rels/workbook.xml.rels"));

    PrintStream out = new PrintStream(zout, false, "UTF-8");

    out.println("");
    out.println(
      "\n"
        + "  \n"
        + "  \n"
        + "  ");

    for (int i = 0; i < sheetNumber; i++) {
      out.println("");
    }
    out.println("");

    out.flush();

    zout.closeEntry();
  }

  private void printApp(ZipOutputStream zout) throws Exception {

    zout.putNextEntry(new ZipEntry("docProps/app.xml"));

    PrintStream out = new PrintStream(zout, false, "UTF-8");

    out.println("");
    out.println(
      "\n"
        + "  Microsoft Excel\n" + "  0\n"
        + "  false\n" + "  \n"
        + "    \n" + "      \n"
        + "        Листы\n" + "      \n"
        + "      \n" + "        3\n" + "      \n"
        + "    \n" + "  \n" + "  ");

    out.println("");

    for (int i = 0; i < sheetNumber; i++) {
      out.println("Лист" + (i + 1) + "");
    }

    out.println("");

    out.println("\n" + "  Microsoft\n"
      + "  false\n" + "  false\n"
      + "  false\n"
      + "  12.0000\n" + "");

    out.flush();

    zout.closeEntry();
  }

  private void printSharedStrings(ZipOutputStream zout) throws Exception {
    outSharedStrings.close();
    outSharedStrings = null;

    zout.putNextEntry(new ZipEntry("xl/sharedStrings.xml"));

    PrintStream out = new PrintStream(zout, false, "UTF-8");

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

    {
      InputStream in = new FileInputStream(workingDir + "/sharedStrings.xml");
      try {
        byte[] buf = new byte[1024 * 4];
        while (true) {
          int count = in.read(buf);
          if (count < 0) break;
          zout.write(buf, 0, count);
        }
      } finally {
        in.close();
      }
    }

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

    zout.closeEntry();
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy