deno.land / x / docx@8.5.0 / demo / 4-basic-table.ts
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133// Example of how you would create a table and add data to it
import * as fs from "fs";import { Document, Packer, Paragraph, Table, TableCell, TableRow, WidthType } from "docx";
const table = new Table({ columnWidths: [3505, 5505], rows: [ new TableRow({ children: [ new TableCell({ width: { size: 3505, type: WidthType.DXA, }, children: [new Paragraph("Hello")], }), new TableCell({ width: { size: 5505, type: WidthType.DXA, }, children: [], }), ], }), new TableRow({ children: [ new TableCell({ width: { size: 3505, type: WidthType.DXA, }, children: [], }), new TableCell({ width: { size: 5505, type: WidthType.DXA, }, children: [new Paragraph("World")], }), ], }), ],});
const table2 = new Table({ columnWidths: [4505, 4505], rows: [ new TableRow({ children: [ new TableCell({ width: { size: 4505, type: WidthType.DXA, }, children: [new Paragraph("Hello")], }), new TableCell({ width: { size: 4505, type: WidthType.DXA, }, children: [], }), ], }), new TableRow({ children: [ new TableCell({ width: { size: 4505, type: WidthType.DXA, }, children: [], }), new TableCell({ width: { size: 4505, type: WidthType.DXA, }, children: [new Paragraph("World")], }), ], }), ],});
const table3 = new Table({ rows: [ new TableRow({ children: [ new TableCell({ children: [new Paragraph("Hello")], }), new TableCell({ children: [], }), ], }), new TableRow({ children: [ new TableCell({ children: [], }), new TableCell({ children: [new Paragraph("World")], }), ], }), ],});
const doc = new Document({ sections: [ { children: [ new Paragraph({ text: "Table with skewed widths" }), table, new Paragraph({ text: "Table with equal widths" }), table2, new Paragraph({ text: "Table without setting widths" }), table3, ], }, ],});
Packer.toBuffer(doc).then((buffer) => { fs.writeFileSync("My Document.docx", buffer);});
Version Info