1.0.10 • Published 11 months ago

@bkfullstack/table-to-excel v1.0.10

Weekly downloads
-
License
MIT
Repository
github
Last release
11 months ago

HTML to Excel Converter

This project provides a utility to convert HTML tables into Excel spreadsheets, complete with styles like background colors and font colors. The generated file can be returned as a Blob, making it compatible with both Node.js and browser environments.


Features

  • Parses HTML tables and converts them to Excel files.
  • Supports inline styles like background-color,color,text-align,border,colspan,rowspan.
  • Works in both Node.js and browser environments.
  • Returns the Excel file as a Blob or File.

Installation

Install the package using npm:

npm install @bkfullstack/table-to-excel

Usage

Node.js Example

import { htmlToExcelBlob, htmlToExcelFile } from '@bkfullstack/table-to-excel';
import fs from 'fs';

const html = `
<table>
  <tr>
    <th style="background-color: #ff9999; color: white;">Name</th>
    <th style="background-color: #ff9999; color: white;">Age</th>
  </tr>
  <tr>
    <td style="background-color: #ffe6e6;">John</td>
    <td style="background-color: #ffe6e6;">30</td>
  </tr>
</table>
`;

htmlToExcelBlob(html).then(async (blob) => {
  const buffer = Buffer.from(await blob.arrayBuffer());
  fs.writeFileSync('output.xlsx', buffer);
  console.log('Excel file created successfully as output.xlsx');
});

htmlToExcelFile(html, 'output.xlsx').then(async (file) => {
  console.log('Excel file created successfully as output.xlsx');
}).catch((error) => {
  console.error('Error creating Excel file:', error);
});

Browser Example

import { htmlToExcelBlob } from '@bkfullstack/table-to-excel';

const html = `
<table>
  <tr>
    <th style="background-color: #ff9999; color: white;">Name</th>
    <th style="background-color: #ff9999; color: white;">Age</th>
  </tr>
  <tr>
    <td style="background-color: #ffe6e6;">John</td>
    <td style="background-color: #ffe6e6;">30</td>
  </tr>
</table>
`;

htmlToExcelBlob(html).then((blob) => {
  const url = URL.createObjectURL(blob);
  const a = document.createElement('a');
  a.href = url;
  a.download = 'output.xlsx';
  document.body.appendChild(a);
  a.click();
  document.body.removeChild(a);
  console.log('Excel file downloaded successfully');
});

API

htmlToExcelBlob(html: string): Promise<Blob>

Converts an HTML string containing a <table> to an Excel file and returns it as a Blob.

Parameters:

  • html (string): The HTML string containing the table to convert.

Returns:

  • Promise<Blob>: A Blob representing the Excel file.

htmlToExcelFile(html: string, outputFilePath: string): Promise<void>

Converts an HTML string containing a <table> to an Excel file and returns it as a Blob.

Parameters:

  • html (string): The HTML string containing the table to convert.
  • outputFilePath (string): The path to the output file.

Returns:

  • Promise<void>: A Promise that resolves when the Excel file is created.

Requirements

  • Node.js 18 or later (for Node.js usage).
  • A modern browser (for browser usage).

License

This project is licensed under the MIT License.


Acknowledgments

  • ExcelJS for Excel file generation.
  • JSDOM for HTML parsing in Node.js.

Enjoy converting HTML tables to Excel with ease!

1.0.10

11 months ago

1.0.9

11 months ago

1.0.8

11 months ago

1.0.7

11 months ago

1.0.6

11 months ago

1.0.5

11 months ago

1.0.4

11 months ago

1.0.3

11 months ago

1.0.2

11 months ago

1.0.1

11 months ago

1.0.0

11 months ago