Showcases

Excel files from JSON data

Produce real XLSX files with typed columns, checkboxes and images from a JSON description, without Excel installed.

Excel Module · Python · Web page

The problem

Users want their data in Excel. Writing XLSX files means a library per language, and Excel itself cannot be installed on a server.

How DMSContainer solves it

Describe the workbook in JSON (sheets, typed columns, rows) and send it to the Excel Module: it returns a real .xlsx file. Columns can be text, numbers, checkboxes or images, with automatic widths. No Excel on the server.

Python

import base64
import requests

URL = "https://dms.example.com/excelrpc"  # your DMSContainer
USER, PASSWORD = "my_user", "my_password"


def call(method, *params):
    r = requests.post(URL, json={"jsonrpc": "2.0", "id": 1, "method": method, "params": list(params)}).json()
    error = r.get("error") or (r.get("result") or {}).get("error")
    if error:
        raise RuntimeError(error["message"])
    return r["result"]


token = call("Login", USER, PASSWORD)["token"]
workbook = {
    "worksheets": [{
        "name": "Orders",
        "columns": [
            {"title": "Customer", "type": "text"},
            {"title": "Orders", "type": "number"},
            {"title": "Paid", "type": "checkbox"},
        ],
        "data": [["ACME Srl", 12, True], ["Globex SpA", 3, False]],
    }]
}
result = call("ConvertToXLSX", token, workbook)
with open("orders.xlsx", "wb") as f:
    f.write(base64.b64decode(result["xlsx"]))

JavaScript in a web page

The same call from the browser: the user signs in with their own DMSContainer account (never write credentials in the page) and gets the file as a download. Serve the page over HTTPS.

<!doctype html>
<html lang="en">
<meta charset="utf-8">
<title>Export to Excel</title>
<form id="form">
  <input id="user" placeholder="DMSContainer user" autocomplete="username" required>
  <input id="password" type="password" placeholder="Password" autocomplete="current-password" required>
  <button>Export the orders</button>
</form>
<p id="status"></p>
<script>
const ENDPOINT = "https://dms.example.com/excelrpc"; // your DMSContainer

async function call(method, ...params) {
  const response = await fetch(ENDPOINT, {
    method: "POST",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify({ jsonrpc: "2.0", id: 1, method, params }),
  });
  const r = await response.json();
  const error = r.error || (r.result && r.result.error);
  if (error) throw new Error(error.message);
  return r.result;
}

// a link that saves the file the service returned (base64)
function downloadLink(base64, fileName, type) {
  const bytes = Uint8Array.from(atob(base64), (c) => c.charCodeAt(0));
  const link = document.createElement("a");
  link.href = URL.createObjectURL(new Blob([bytes], { type }));
  link.download = fileName;
  link.textContent = "Download " + fileName;
  return link;
}
document.getElementById("form").addEventListener("submit", async (event) => {
  event.preventDefault();
  const status = document.getElementById("status");
  try {
    const { token } = await call("Login", document.getElementById("user").value,
      document.getElementById("password").value);
    const workbook = {
      worksheets: [{
        name: "Orders",
        columns: [
          { title: "Customer", type: "text" },
          { title: "Orders", type: "number" },
          { title: "Paid", type: "checkbox" },
        ],
        data: [["ACME Srl", 12, true], ["Globex SpA", 3, false]],
      }],
    };
    const { xlsx } = await call("ConvertToXLSX", token, workbook);
    status.replaceChildren(downloadLink(xlsx, "orders.xlsx",
      "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"));
  } catch (error) {
    status.textContent = error.message;
  }
});
</script>
</html>

The result

orders.xlsx is a real Excel workbook with the column types kept: numbers stay numbers and the “Paid” column is a checkbox.

The Excel Module Download the free trial