Office editing
Edit Excel and Word files without Office with NexPDF for Android. Set cells and recalculate every formula and chart, read cells, replace text across runs, and set, insert and delete paragraphs.
License module
Office editing needs the conversion module (Startup tier and above).
NexPDF edits .xlsx workbooks and .docx documents directly. No Office install or server is involved. Only
what you change is rewritten: styles, merged cells, column widths, conditional formats, charts,
images, numbering and headers stay exactly as they were. The calls are NexPdfFunctions.xlsxEdit, xlsxCellsJson, docxEdit and docxParagraphsJson. Each takes the file
bytes and JSON, and returns the new file. To get a PDF, open the result as an Office document.
Excel: set cells and recalculate
import com.nexdevit.nexpdf.NexPdfFunctions
import org.json.JSONArray
import org.json.JSONObject
val xlsx = context.assets.open("budget.xlsx").readBytes()
val edits = JSONObject()
.put("sheet", "Budget")
.put("edits", JSONArray()
.put(JSONObject().put("cell", "C2").put("value", 6.5))
.put(JSONObject().put("cell", "Summary!B1").put("value", "=SUM(Budget!D1:D3)")))
val edited = NexPdfFunctions.xlsxEdit(xlsx, edits.toString())
val cells = JSONObject(NexPdfFunctions.xlsxCellsJson(edited, """{"sheet":"Budget","range":"A1:D12"}"""))Each edit names a cell: C2, $C$2 or Summary!B1. With no sheet in the cell or in the edit,
the top-level sheet applies, else the first sheet. The value decides what is written:
| Value | Written as |
|---|---|
| a number | a number (the cell keeps its number format) |
true / false | a boolean |
| text | text, kept as text even when it looks like a number |
text starting with = | a formula ("formula": "SUM(A1:A3)" says so explicitly) |
"text": "=…" | text that starts with = |
null | the cell is cleared (it keeps its format) |
ranges writes rows of values from start, down and to the right, for tables and imports.
After the edits, every formula in the workbook is recalculated by the built-in evaluator,
across sheets and defined names, and the results are stored in the file. Readers that do not
calculate, and the PDF converter, then show the new values. Chart caches are refreshed from their
cells, and the workbook is marked to recalculate when Excel or LibreOffice opens it. Pass
"recalculate": false to only write the cells. clock_unix_seconds fixes NOW() and TODAY(),
and rand_seed fixes RAND().
- A cell that belongs to a shared formula gets its own formula, and the other cells of the group keep working with their references moved.
- An array formula can be changed or cleared from its first cell. Changing another cell of its range is an error, as in Excel.
- Text is written as inline strings, so the shared string table is never rewritten.
xlsx_cells_json reads cells back: {"sheets": [{"name", "cells": [{"cell", "row", "col", "type", "value", "formula"}]}]}. type is number, text, bool or error; for an error, value
is the error text, such as #DIV/0!. Options: sheet, range and recalculate. By default it
returns the values stored in the file; "recalculate": true calculates the formulas first.
Word: replace and insert text
val docx = context.assets.open("offer.docx").readBytes()
val paragraphs = JSONArray(NexPdfFunctions.docxParagraphsJson(docx))
val editedDocx = NexPdfFunctions.docxEdit(docx, """
[{"op": "replace", "find": "{client}", "replace": "ACME Ltd", "required": true},
{"op": "insert", "after": 3, "text": "Delivery within 10 working days."},
{"op": "delete", "paragraph": 7}]
""".trimIndent())Operations run in order. Each one sees the document as the previous ones left it.
| Operation | What it does |
|---|---|
replace | Replaces every occurrence of find in the body, tables, headers, footers, footnotes and endnotes, also where Word split the text over several runs. The new text takes the format of the run where the match starts. match_case (default true); with required, finding nothing is an error. |
set_text | Replaces the text of a paragraph. It keeps the paragraph's style and settings and the format of its first run. Its other content (images, fields) is removed. |
insert | Adds a paragraph before or after a paragraph, or at "start" or "end" of the document. With a style (a style id such as Heading2) it takes that style. Without one, it copies the neighbouring paragraph, so inserting after a list item makes another list item. |
delete | Removes a paragraph. If it is the only paragraph of a table cell, it is emptied instead. |
In text, \n is a line break and \t a tab. Paragraphs are numbered from 0 in document order,
table cells included and text boxes left out. docx_paragraphs_json lists them as
[{"index", "style", "text", "in_table"}], so you can find the index to act on.
To fill whole documents from data, with loops over table rows, conditions and images, use templates.
Limits
- Excel: rows and columns are not inserted or deleted, so references do not move. Formulas the
evaluator does not know return
#NAME?until Excel recalculates them on opening. - Word: tracked changes are not created, and edits are not tracked. Text inside fields and text boxes is not replaced.