Skip to Content
NexPDF

Office editing

Edit Excel and Word files without Office with NexPDF for Flutter. Set cells and recalculate every formula and chart, read cells, replace text across runs, and set, insert and delete paragraphs.

English

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 xlsxEdit, xlsxCellsJson, docxEdit and docxParagraphsJson (options as a Map or List, or a JSON string). 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 'dart:convert';
import 'dart:io';
import 'package:nexpdf_flutter/nexpdf_flutter.dart';

final sdk = NexPdfFlutter();
final edited = sdk.xlsxEdit(File('budget.xlsx').readAsBytesSync(), {
  'sheet': 'Budget',
  'edits': [
    {'cell': 'C2', 'value': 6.5},
    {'cell': 'Summary!B1', 'value': '=SUM(Budget!D1:D3)'},
  ],
});
final cells = jsonDecode(sdk.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:

ValueWritten as
a numbera number (the cell keeps its number format)
true / falsea boolean
texttext, 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 =
nullthe 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

final docx = File('offer.docx').readAsBytesSync();
final paragraphs = jsonDecode(sdk.docxParagraphsJson(docx));
final editedDocx = sdk.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},
]);

Operations run in order. Each one sees the document as the previous ones left it.

OperationWhat it does
replaceReplaces 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_textReplaces 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.
insertAdds 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.
deleteRemoves 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.

On this page