Files
docx-js/demo/31-tables.ts

245 lines
12 KiB
TypeScript
Raw Permalink Normal View History

2018-09-12 21:03:06 +01:00
// Example of how you would create a table and add data to it
2023-06-05 00:33:43 +01:00
2018-09-12 21:03:06 +01:00
import * as fs from "fs";
2023-12-27 20:56:13 +00:00
import { BorderStyle, Document, HeadingLevel, Packer, Paragraph, Table, TableCell, TableRow, VerticalAlign, TextDirection } from "docx";
2018-09-12 21:03:06 +01:00
2021-03-19 20:53:56 +00:00
const doc = new Document({
sections: [
{
2019-09-13 00:51:20 +01:00
children: [
2021-03-19 20:53:56 +00:00
new Table({
rows: [
new TableRow({
children: [
new TableCell({
children: [new Paragraph({}), new Paragraph({})],
verticalAlign: VerticalAlign.CENTER,
}),
new TableCell({
children: [new Paragraph({}), new Paragraph({})],
verticalAlign: VerticalAlign.CENTER,
}),
new TableCell({
children: [new Paragraph({ text: "bottom to top" }), new Paragraph({})],
textDirection: TextDirection.BOTTOM_TO_TOP_LEFT_TO_RIGHT,
}),
new TableCell({
children: [new Paragraph({ text: "top to bottom" }), new Paragraph({})],
textDirection: TextDirection.TOP_TO_BOTTOM_RIGHT_TO_LEFT,
}),
],
2019-09-13 00:51:20 +01:00
}),
2021-03-19 20:53:56 +00:00
new TableRow({
children: [
new TableCell({
children: [
new Paragraph({
2022-07-06 15:26:17 +01:00
text: "Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah",
2021-03-19 20:53:56 +00:00
heading: HeadingLevel.HEADING_1,
}),
],
}),
new TableCell({
children: [
new Paragraph({
text: "This text should be in the middle of the cell",
}),
],
verticalAlign: VerticalAlign.CENTER,
}),
new TableCell({
children: [
new Paragraph({
text: "Text above should be vertical from bottom to top",
}),
],
verticalAlign: VerticalAlign.CENTER,
}),
new TableCell({
children: [
new Paragraph({
text: "Text above should be vertical from top to bottom",
}),
],
verticalAlign: VerticalAlign.CENTER,
}),
],
2019-09-13 00:51:20 +01:00
}),
],
2020-05-10 10:36:25 -07:00
}),
2023-12-27 20:56:13 +00:00
new Paragraph("Table with borders"),
new Table({
rows: [
new TableRow({
children: [
new TableCell({
borders: {
top: {
style: BorderStyle.DASH_SMALL_GAP,
size: 1,
color: "000000",
},
bottom: {
style: BorderStyle.DASH_SMALL_GAP,
size: 1,
color: "000000",
},
left: {
style: BorderStyle.DASH_SMALL_GAP,
size: 1,
color: "000000",
},
right: {
style: BorderStyle.DASH_SMALL_GAP,
size: 1,
color: "000000",
},
},
children: [new Paragraph("Dash small gap border")],
}),
new TableCell({
borders: {
top: {
style: BorderStyle.DASH_SMALL_GAP,
size: 1,
color: "000000",
},
bottom: {
style: BorderStyle.DASH_SMALL_GAP,
size: 1,
color: "000000",
},
left: {
style: BorderStyle.DASH_SMALL_GAP,
size: 1,
color: "000000",
},
right: {
style: BorderStyle.DASH_SMALL_GAP,
size: 1,
color: "000000",
},
},
children: [new Paragraph("Dash small gap border")],
}),
],
}),
new TableRow({
children: [
new TableCell({
borders: {
top: {
style: BorderStyle.DOUBLE,
size: 1,
color: "ff0000",
},
bottom: {
style: BorderStyle.DOUBLE,
size: 1,
color: "ff0000",
},
left: {
style: BorderStyle.DOUBLE,
size: 1,
color: "ff0000",
},
right: {
style: BorderStyle.DOUBLE,
size: 1,
color: "ff0000",
},
},
children: [new Paragraph("Double border")],
}),
new TableCell({
borders: {
top: {
style: BorderStyle.DOUBLE,
size: 1,
color: "ff0000",
},
bottom: {
style: BorderStyle.DOUBLE,
size: 1,
color: "ff0000",
},
left: {
style: BorderStyle.DOUBLE,
size: 1,
color: "ff0000",
},
right: {
style: BorderStyle.DOUBLE,
size: 1,
color: "ff0000",
},
},
children: [new Paragraph("Double border")],
}),
],
}),
new TableRow({
children: [
new TableCell({
borders: {
top: {
style: BorderStyle.NONE,
size: 0,
color: "ffffff",
},
bottom: {
style: BorderStyle.NONE,
size: 0,
color: "ffffff",
},
left: {
style: BorderStyle.NONE,
size: 0,
color: "ffffff",
},
right: {
style: BorderStyle.NONE,
size: 0,
color: "ffffff",
},
},
children: [new Paragraph("Should have no border")],
}),
new TableCell({
borders: {
top: {
style: BorderStyle.NONE,
size: 0,
color: "ffffff",
},
bottom: {
style: BorderStyle.NONE,
size: 0,
color: "ffffff",
},
left: {
style: BorderStyle.NONE,
size: 0,
color: "ffffff",
},
right: {
style: BorderStyle.NONE,
size: 0,
color: "ffffff",
},
},
children: [new Paragraph("Should have no border")],
}),
],
}),
],
}),
2019-09-13 00:51:20 +01:00
],
2021-03-19 20:53:56 +00:00
},
2019-09-13 00:51:20 +01:00
],
2019-03-18 23:50:21 +00:00
});
2019-06-25 01:21:28 +01:00
2019-08-07 22:12:14 +01:00
Packer.toBuffer(doc).then((buffer) => {
2018-09-12 21:03:06 +01:00
fs.writeFileSync("My Document.docx", buffer);
});