Embedded filesSpreadsheet
Files stored inside a workbook as embedded objects, such as a Word document, a PDF or an Outlook message, shown with a file-type icon and their name, which you can move, open, download, replace and round-trip through xlsx.
A workbook can carry other files inside it: the signed contract next to the price list, the email that approved a budget, the Word document a table was copied from. Excel calls these embedded objects. The spreadsheet reads them from xlsx files, works out what kind of file each one is, shows it over the grid with a file-type icon and its name, lets the user move, resize, replace and delete it, hands the bytes to your application when it is opened, and writes it back.
import { createSpreadsheet } from '@birchui/spreadsheet';
declare const container: HTMLElement;
declare const contract: Uint8Array;
const sheet = createSpreadsheet(container);
const id = sheet.insertEmbeddedObject({
data: contract,
fileName: 'Supplier agreement 2026.pdf',
anchor: { cell: 'D2' },
});
sheet.on('embeddedObjectOpen', ({ fileName, mimeType, fileType, data }) => {
const url = URL.createObjectURL(new Blob([data as BlobPart], { type: mimeType }));
window.open(url, '_blank', 'noopener');
console.info(`Opened ${fileName} (${fileType.label})`);
});Any file can be embedded. Nothing is uploaded or fetched: the bytes live in the workbook JSON and in the xlsx file.
| Field | Purpose |
|---|---|
data |
The file as a Uint8Array or ArrayBuffer. |
fileName |
The name shown under the icon and used when the file is downloaded. Folder parts are removed. |
mimeType |
Optional. Worked out from the file itself when left out, see File types. |
anchor |
The cell the icon is attached to, with an optional offset and size. Defaults to the active cell and 128 by 96 pixels. |
asIcon |
true by default. Set false when you pass a preview that shows the content. |
preview |
A PNG, JPEG, GIF, BMP, TIFF, EMF or WMF data URL to show instead of the file-type icon. |
alt |
Alternative text read by screen readers. |
File types#
The type shown for a file is worked out from the file, not only from its name. detectEmbeddedFileType(bytes, hints) does the work and is exported for your own use:
import { detectEmbeddedFileType } from '@birchui/spreadsheet';
declare const bytes: Uint8Array;
const type = detectEmbeddedFileType(bytes, { fileName: 'Q3-report.pdf' });
console.info(type.kind, type.label, type.badge, type.extension, type.mime);For the bytes of a PDF this logs pdf, PDF document, PDF, pdf and application/pdf.
It looks, in this order, at the ProgID of the object (Word.Document.12, AcroExch.Document and so on), the stored file name, the content type and extension of the package part, and then at the bytes:
Family (kind) |
Badge | Recognised from the bytes |
|---|---|---|
document |
DOCX, DOCM, DOC, ODT, RTF |
Word files (the parts of the zip package, or the WordDocument stream or class id of the older format), OpenDocument text, rich text |
spreadsheet |
XLSX, XLSM, XLSB, XLS, ODS |
Excel workbooks, also with macros or in the binary and older formats, OpenDocument spreadsheets |
presentation |
PPTX, PPTM, PPT, ODP |
PowerPoint and OpenDocument presentations |
pdf |
PDF |
|
message |
MSG |
Outlook messages |
email |
EML |
Internet mail, from its header lines |
image |
IMG |
PNG, JPEG, GIF, BMP, TIFF, WebP, SVG |
text |
TXT |
Plain text, also HTML, XML, JSON and Markdown by name |
csv |
CSV |
Comma, semicolon, tab or bar separated lines with the same number of fields |
archive |
ZIP |
zip, 7-Zip, RAR, gzip |
audio |
AUD |
MP3, WAV, MPEG-4 audio |
video |
VID |
MP4, AVI, QuickTime |
file |
FILE |
Anything else. The label is made from the extension of the name, for example "DWG file". |
The bytes win over a name that does not match them: a PDF stored as Scan.txt is shown, opened and downloaded as a PDF, and a zip archive named Proposal.docx is shown as an archive. A name is trusted where the bytes cannot tell, as with text formats and files nothing is known about. The function reads a bounded part of the file, never throws, and returns the plain file type for damaged or empty input.
getEmbeddedObjectType(id) returns the same description for an embedded file.
How a file is shown#
- Icon and name. A file without a picture of its own is drawn as a page with a folded corner, a coloured band and the badge of its type, with the file name under it in the text colour of the theme. Each family has its own colour. A name that does not fit is cut with an ellipsis; the tooltip has the whole name, the type and the size, as in "Q3-report.pdf — PDF document — 248 KB".
- Stored picture. When the workbook holds a picture for the object, which is what Excel stores for every embedded file, that picture is drawn at the position and size the file gives it, with a small badge of the type in the corner. If the picture cannot be drawn, the icon and the name are shown.
- The icons are drawn by the spreadsheet. They are not the logos of the programs that own the formats.
Opening and downloading#
Selecting an embedded file shows two buttons next to it, Open and Download. They are placed under the object, or above it or inside its lower edge when there is no room, so they stay visible at the edges of the grid, in right-to-left sheets, at any zoom and in frozen panes.
- Open, a double click, or
Enteron the focused object raisesembeddedObjectOpen. The spreadsheet does not try to display the file itself; the event carries everything a host needs to do that. - Download saves the original bytes. The file name and MIME type come from the detected type, so a file stored without an extension, or with a wrong one, gets the right one added (
Scan.txtwith PDF content is saved asScan.txt.pdf). A MIME type of your own that you passed toinsertEmbeddedObject()is kept.
Both work on read-only spreadsheets and on protected sheets.
A right click, or Shift + F10 on the focused object, opens a menu headed by the icon, name, type and size of the file, with Open, Download, Properties…, Replace… and Remove. Properties…, also Alt + Enter, opens a dialog with the name, type and size and the same commands. Replace… asks for a file and puts it in place of the current one at the same position; it is one undo step.
Each embedded file is a tab stop: Tab reaches the object and then its two buttons. It is announced with its type and name, for example "Embedded PDF document: Q3-report.pdf", or with its alternative text when it has one.
data is a fresh copy, so you may pass it to another component or a worker without affecting the workbook. To show a PDF, an email or a Word document in place, pass the bytes to the matching component; the PDF recipes show the wiring for @birchui/pdf and @birchui/document.
Moving, resizing and deleting#
Embedded files behave like pictures: drag to move, drag the corner handle to resize, arrow keys to nudge by one pixel or ten with Shift, Delete to remove, Escape to return to the grid. Every change is undoable, and the icon follows its anchor cell when rows or columns are inserted or deleted.
Insert > Object opens a file picker, then shows the chosen file with its icon, name, detected type and size. Insert embeds it at the active cell, Choose another file… goes back to the picker, and nothing is added until you confirm.
Embedded files in code#
| Method | Purpose |
|---|---|
insertEmbeddedObject(object, sheet?) |
Embeds a file and returns its id. |
updateEmbeddedObject(id, changes) |
Changes the anchor, the alternative text or the file name, or replaces the file with data (and an optional mimeType). Replacing the file drops the stored picture, so the new file is shown with its own icon. |
removeEmbeddedObject(id) |
Deletes it. |
getEmbeddedObjects(sheet?) |
Returns a sheet's embedded files as EmbeddedObjectJSON. |
getEmbeddedObjectData(id) |
Returns the file bytes, or undefined for an unknown id. |
getEmbeddedObjectType(id) |
Returns the detected type, or undefined for an unknown id. |
openEmbeddedObject(id) |
Raises embeddedObjectOpen. |
downloadEmbeddedObject(id) |
Saves the file; returns false outside a browser page. |
In the workbook JSON#
data is the file itself in base64. container is set only for objects read from a file whose bytes sit inside an OLE wrapper; it holds that wrapper so the export can write it back unchanged. fallback is the picture stored with the object; an icon the spreadsheet generated itself is not kept there, so the object is drawn with the current icon and theme. Treat container as opaque and expect workbooks with embedded files to be large.
Excel files#
| In the file | Read | Written |
|---|---|---|
Word, Excel and PowerPoint files embedded as packages (.docx, .xlsx, .pptx and their macro-enabled forms) |
Yes, as the file | Yes, as a package part |
Any other file, embedded as a Package object: PDF, .msg, .eml, .zip, text and so on |
Yes, with its original name | Yes, in an OLE Package container |
| PDF objects written by Acrobat, and OOXML files wrapped in an OLE storage | Yes, the file is taken out of the wrapper | The original wrapper, unchanged |
Legacy .doc, .xls, .ppt and Outlook message objects stored as OLE storages |
Yes, the storage is the file | Unchanged |
| The picture shown for the object | Yes: PNG, JPEG, GIF, BMP, TIFF, EMF, WMF | Yes; a vector icon with the type badge and the file name is generated when there is none |
| Position and size, alternative text, the "display as icon" flag | Yes | Yes |
| Linked objects, which point at a file outside the workbook | Kept as they are, not shown | Unchanged |
An object is written the way Excel 2010 and later write it: the file under xl/embeddings/, an oleObjects entry in the worksheet with the modern anchor and its fallback, a shape in the legacy drawing, and the icon picture. validateXlsxPackage() checks that these parts line up.
A package part has no file name of its own in the format. For an object shown as an icon, the name is read from the label drawn in its icon picture, which is what Excel shows under the icon, and the extension of the file is added when the label has none: a .docx you embed as Terms.docx comes back as Terms.docx. When the picture is not a metafile, has no text, or the label was shortened with an ellipsis, the object is named after its part, for example Microsoft_Word_Document2.docx. An icon written by the spreadsheet carries the whole file name in a comment record, so a long name survives even though the visible label is shortened. Files embedded as Package objects keep the name stored in their container. Each object in a file is read once, whichever of its two descriptions (the modern one or the fallback) the file carries; a preview picture shared by several objects is written once.
The generated icon#
An object without a picture of its own gets one when the workbook is written, because Excel and LibreOffice show nothing for an object without it. It is an EMF with the same design as the icon on screen, drawn with vector records: the page, the folded corner, the coloured band, the badge and the file name. The name is one text record with the advance of every character taken from the Arial metrics, so other programs space the letters evenly.
How this is verified#
- A workbook with a PDF, a
.docx, an Outlook.msgand a text file with a non-ASCII name is exported, loaded with openpyxl, and each container is opened witholefile, which reads the same bytes back out. - LibreOffice converts the same workbook to OpenDocument; the result holds all four objects byte for byte, each with the generated icon as its replacement picture.
- LibreOffice converts it to PDF; each file name is found in the page text as written, and the band colour of each icon is found in the rendered page.
- Nothing is tested in Microsoft Excel.
LibreOffice Calc does not write embedded objects when it saves as xlsx, so there is no LibreOffice-authored xlsx to read back; the reader is tested against files in the layout Excel 2007 used and the one Excel 2010 and later use, both built for the tests.
Limits#
- The spreadsheet shows an icon or the stored picture. It does not render the embedded file; open it in the component that understands it.
- An embedded file cannot be edited in place. Use Replace… to put a new version in its place.
- Type detection reads signatures and container structure. It does not validate the file, so a damaged PDF is still called a PDF, and a format outside the table above is shown as a plain file.
- Print output shows the icon with the name, or the stored picture with the badge. PDF export draws the icon as vectors with the name; a stored PNG or JPEG picture is drawn as the picture, and a stored EMF or WMF picture is replaced by the icon because the PDF writer does not draw metafiles.
- A picture stored by Excel carries the file name in dark text. With
sheetTheme: 'match'in a dark theme it is drawn on a white plate so the name stays readable. - Embedded files are not written to ods or csv.
- Icons scale with the grid zoom and are drawn in every frozen or split pane they reach.
- Outlook messages are stored as files. To read one, use
parseMsg()from@birchui/pdf/engine.