Skip to content

Commit 92c5158

Browse files
committed
first commit
0 parents  commit 92c5158

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+8906
-0
lines changed

.github/workflows/test.yml

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
name: test
2+
3+
on: [push]
4+
5+
jobs:
6+
publish:
7+
runs-on: ubuntu-latest
8+
steps:
9+
- uses: actions/checkout@v4
10+
- uses: actions/setup-node@v4
11+
with:
12+
node-version: "20.x"
13+
- run: npm ci
14+
- run: npm run build
15+
env:
16+
RUSTFLAGS: -C target-feature=+bulk-memory
17+
- run: npm test

.gitignore

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
rust/target
2+
rust/pkg
3+
4+
**/node_modules
5+
**/*.xlsx
6+
!test/expected/*.xlsx
7+
8+
**/dist
9+
10+
docs/

LICENSE

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
MIT License
2+
3+
Copyright (c) 2024 estie, inc.
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.
22+
23+
This project includes software from third-party libraries, which are licensed under their own terms.
24+
25+
# Third-Party Licenses
26+
27+
## rust_xlsxwriter
28+
29+
MIT License
30+
31+
Copyright 2022-2024 John McNamara <[email protected]>
32+
33+
Permission is hereby granted, free of charge, to any person obtaining a copy
34+
of this software and associated documentation files (the "Software"), to deal
35+
in the Software without restriction, including without limitation the rights
36+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
37+
copies of the Software, and to permit persons to whom the Software is
38+
furnished to do so, subject to the following conditions:
39+
40+
The above copyright notice and this permission notice shall be included in all
41+
copies or substantial portions of the Software.
42+
43+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
44+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
45+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
46+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
47+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
48+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
49+
SOFTWARE.
50+

README.md

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
# wasm-xlsxwriter ![NPM Version](https://img.shields.io/npm/v/wasm-xlsxwriter)
2+
3+
The `wasm-xlsxwriter` library is a lightweight wrapper around the write API of Rust's [`rust_xlsxwriter`](https://crates.io/crates/rust_xlsxwriter), compiled to WebAssembly (Wasm) with minimal setup to make it easily usable from JavaScript.
4+
5+
With this library, you can generate Excel files in the browser using JavaScript, complete with support for custom formatting, formulas, links, images, and more.
6+
7+
## Getting Started
8+
9+
To get started, install the library via npm:
10+
11+
```bash
12+
npm install wasm-xlsxwriter
13+
```
14+
15+
### Usage
16+
17+
Here’s an example of how to use `wasm-xlsxwriter` to create an Excel file:
18+
19+
```typescript
20+
import xlsxInit, {
21+
Format,
22+
FormatAlign,
23+
FormatBorder,
24+
Formula,
25+
Workbook,
26+
Image,
27+
Url,
28+
} from "wasm-xlsxwriter";
29+
30+
// Load the WebAssembly module and initialize the library.
31+
await xlsxInit();
32+
33+
// Create a new Excel file object.
34+
const workbook = new Workbook();
35+
36+
// Create some formats to use in the worksheet.
37+
const boldFormat = new Format().setBold();
38+
const decimalFormat = new Format().setNumFormat("0.000");
39+
const dateFormat = new Format().setNumFormat("yyyy-mm-dd");
40+
const mergeFormat = new Format()
41+
.setBorder(FormatBorder.Thin)
42+
.setAlign(FormatAlign.Center);
43+
44+
// Add a worksheet to the workbook.
45+
const worksheet = workbook.addWorksheet();
46+
47+
// Set the column width for clarity.
48+
worksheet.setColumnWidth(0, 22);
49+
50+
// Write a string without formatting.
51+
worksheet.write(0, 0, "Hello");
52+
53+
// Write a string with the bold format defined above.
54+
worksheet.writeWithFormat(1, 0, "World", boldFormat);
55+
56+
// Write some numbers.
57+
worksheet.write(2, 0, 1);
58+
worksheet.write(3, 0, 2.34);
59+
60+
// Write a number with formatting.
61+
worksheet.writeWithFormat(4, 0, 3.0, decimalFormat);
62+
63+
// Write a formula.
64+
worksheet.write(5, 0, new Formula("=SIN(PI()/4)"));
65+
66+
// Write a date.
67+
const date = new Date(2023, 1, 25);
68+
worksheet.writeWithFormat(6, 0, date, dateFormat);
69+
70+
// Write some links.
71+
worksheet.write(7, 0, new Url("https://www.rust-lang.org"));
72+
worksheet.write(8, 0, new Url("https://www.rust-lang.org").setText("Rust"));
73+
74+
// Write some merged cells.
75+
worksheet.mergeRange(9, 0, 9, 1, "Merged cells", mergeFormat);
76+
77+
// Insert an image (ensure `imageBuffer` contains the image data).
78+
const image = Image.newFromBuffer(imageBuffer);
79+
worksheet.insertImage(1, 2, image);
80+
81+
// Save the file to a buffer.
82+
const buf = workbook.saveToBufferSync();
83+
```
84+
85+
## License
86+
87+
MIT

examples/nextjs/.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules/
2+
.next/

examples/nextjs/app/layout.tsx

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export default function RootLayout({
2+
children,
3+
}: {
4+
children: React.ReactNode;
5+
}) {
6+
return (
7+
<html lang="en">
8+
<body>{children}</body>
9+
</html>
10+
);
11+
}

examples/nextjs/app/page.tsx

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
"use client";
2+
import initModule, { Workbook } from "wasm-xlsxwriter";
3+
4+
export default function Page() {
5+
return (
6+
<button
7+
onClick={async () => {
8+
const workbook = await generateXlsx();
9+
download(workbook);
10+
}}
11+
>
12+
Generate a xlsx file
13+
</button>
14+
);
15+
}
16+
17+
const generateXlsx = async () => {
18+
await initModule();
19+
const workbook = new Workbook();
20+
const worksheet = workbook.addWorksheet();
21+
worksheet.write(0, 0, "Hello, world!");
22+
return workbook;
23+
};
24+
25+
const download = (workbook: Workbook) => {
26+
const buffer = workbook.saveToBufferSync();
27+
const blob = new Blob([buffer]);
28+
const url = window.URL.createObjectURL(blob);
29+
const a = document.createElement("a");
30+
a.href = url;
31+
a.download = "generated.xlsx";
32+
a.click();
33+
a.remove();
34+
};

examples/nextjs/next-env.d.ts

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/// <reference types="next" />
2+
/// <reference types="next/image-types/global" />
3+
4+
// NOTE: This file should not be edited
5+
// see https://nextjs.org/docs/app/building-your-application/configuring/typescript for more information.

0 commit comments

Comments
 (0)