SheetJS works with traditional and modern software for new spreadsheets

SheetJS works with traditional and modern software for new spreadsheets

2022-10-10 0 1,625
Resource Number 44946 Last Updated 2025-02-24
¥ 0HKD Upgrade VIP
Download Now Matters needing attention
Can't download? Please contact customer service to submit a link error!
Value-added Service: Installation Guide Environment Configuration Secondary Development Template Modification Source Code Installation

The SheetJS recommended in this issue is a new spreadsheet for both traditional and modern software.

SheetJS works with traditional and modern software for new spreadsheets插图

File format support

SheetJS Community Edition provides a tried-and-true open source solution for extracting useful data from almost any complex spreadsheet and generating new spreadsheets that work with both traditional and modern software.

SheetJS Pro offers solutions that go beyond data processing: easy editing of complex templates; Use shape to release your inner Picasso; Create custom worksheets using images/charts/pivottables; Evaluate formula expressions and port calculations to Web applications; Automate common spreadsheet tasks and more.

class=”pgc-h-arrow-right” data-track=”4″>

SheetJS works with traditional and modern software for new spreadsheets插图1

class=”pgc-h-arrow-right” data-track=”37″>

Complete browser independent build save dist/xlsx.full.min.js and can be added directly to with < script> Page with tag:

< script lang="javascript"  src="dist/xlsx.full.min.js"> < /script> 

Each individual release script is available at https://cdn.sheetjs.com/. The latest version uses the latest tag:

< ! -- use the latest version --> 
< script lang="javascript"  src="https://cdn.sheetjs.com/xlsx-latest/package/dist/xlsx.full.min.js"> < /script> 

can refer to a specific distribution by version:

< ! -- use version 0.18.5 --> 
< script lang="javascript"  src= < / span > "https://cdn.sheetjs.com/xlsx-0.18.5/package/dist/xlsx.full.min.js" & gt; < /script> 

For production use, download the script and add it to the public folder along with other scripts.

Browser build

The full single-file version is generated at dist/xlsx.full.min.js

dist/xlsx.core.min.js omit code page library (XLS encoding not supported)

in dist/xlsx.mini.min.js. Compared to the full version:

    • Skip code page library (XLS encoding not supported)

XLSB/XLS/Lotus 1-2-3 / SpreadsheetML 2003 / Numbers

  • Node flow utility removed

 

These scripts are also available on the CDN:

< ! -- use xlsx.mini.min.js from the latest version --> 
< script lang="javascript"  src="https://cdn.sheetjs.com/xlsx-latest/package/dist/xlsx.mini.min.js"> < /script> 

ECMAScript module

ECMAScript module builds are saved to xlsx.mjs and can be added directly to pages with script tags, using type=”module” :

< script type="module"> 
import { read,  writeFileXLSX } from "https://cdn.sheetjs.com/xlsx-latest/package/xlsx.mjs";

/* load the codepage support library for extended support with older formats  */
import { set_cptable } from "https://cdn.sheetjs.com/xlsx-latest/package/xlsx.mjs";
import * as cptable from 'https://cdn.sheetjs.com/xlsx-latest/package/dist/cpexcel.full.mjs';
set_cptable(cptable);
< /script> 

The NodeJS package also exposes modules with the module parameter, which Angular and other projects support:

import { read,  writeFileXLSX } from "xlsx";

/* load the codepage support library for extended support with older formats  */
import { set_cptable } from "xlsx";
import * as cptable from 'xlsx/dist/cpexcel.full.mjs';
set_cptable(cptable); 

class=”pgc-h-arrow-right” data-track=”30″>

Most scenarios involving spreadsheets and data can be divided into 5 parts:

  • Get data : Data can be stored anywhere: local or remote files, databases, HTML tables, or even generated programmatically in a Web browser.
  • Extract data : For spreadsheet files, this involves parsing raw bytes to read cell data. For general JS data, this involves reshaping the data.
  • Processing data : This step from generating summary statistics to cleaning data records is at the heart of the problem.
  • packet : This might involve making a new spreadsheet or using XML to serialize Jsor.stringify or writing XML or simply flattening the data for UI tools.
  • Publish data : The spreadsheet file can be uploaded to the server or written locally. The data can be presented to the user in an HTML TABLE or data grid.

A common problem involves generating a valid spreadsheet export from data stored in an HTML table. In this example, the HTML TABLE on the page will be scraped, a line with the report date will be added at the bottom, and the new file will be generated and downloaded locally. XLSX.writeFile is responsible for packing the data and trying to download it locally:

// Acquire Data (reference to the HTML  table)
var table_elt = document.getElementById("my-table-id");

// Extract Data (create a workbook object from the table)
var workbook = XLSX.utils.table_to_book(table_elt);

// Process Data (add a new row)
var ws = workbook.Sheets["Sheet1"];
XLSX.utils.sheet_add_aoa(ws,  [["Created "+new Date().toISOString()]],  {origin:-1});

// Package and Release Data (`writeFile` tries to write and save an XLSB file)
XLSX.writeFile(workbook, "Report.xlsb"); 

read The library tries to simplify steps 2 and 4 by using the ability to extract useful data from the spreadsheet file (/) and generate a new spreadsheet file from the data (/)readFile. Other utility features, such as use with other common data sources such as HTML tables.
writewriteFiletable_to_book This document and various demonstration projects cover many of the common scenarios and approaches for steps 1 and 5.

class=”pgc-h-arrow-right” data-track=”40″>

Parse workbook

Extract data from spreadsheet bytes

var workbook = XLSX.read(data, opts); 

This method can extract data from a spreadsheet byte stored in a JS string, a “binary string”, a NodeJS buffer, or a typed array (or) read. Uint8ArrayArrayBuffer

Read spreadsheet bytes from local file and extract data

var workbook = XLSX.readFile(filename, opts); 

The readFile method attempts to read the spreadsheet file in the provided path. Browsers are generally not allowed to read files this way (it is considered a security risk), and attempts to read files this way will throw an error.

class=”pgc-h-arrow-right” data-track=”69″> example

Local file in NodeJS server

readFilefs.readFileSync is used under the hood:

var XLSX = require("xlsx");

var workbook = XLSX.readFile("test.xlsx"); 

readFile does not enable helpers for the node ESM. Instead, fs.readFileSync should be used to read file data to Buffer for use with XLSX.read:

import { readFileSync } from "fs";
import { read } from "xlsx/xlsx.mjs";

const buf = readFileSync("test.xlsx");
/* buf is a Buffer */
const workbook = read(buf); 

user-submitted file in the web page

// XLSX is a global from the standalone script

async function handleDropAsync(e) {
  e.stopPropagation(); e.preventDefault();
  const f = e.dataTransfer.files[0];
  /* f is a File */
  const data = await f.arrayBuffer();
  /* data is an ArrayBuffer */
  const workbook = XLSX.read(data);

  /* DO SOMETHING WITH workbook HERE */
}
drop_dom_element.addEventListener("drop", handleDropAsync, false);

For maximum compatibility, FileReader should use API:

function handleDrop(e) {
  e.stopPropagation(); e.preventDefault();
  var f = e.dataTransfer.files[0];
  /* f is a File */
  var reader = new FileReader();
  reader.onload = function(e) {
    var data = e.target.result;
    /* reader.readAsArrayBuffer(file) -> data will be an ArrayBuffer */
    var workbook = XLSX.read(data);

    /* DO SOMETHING WITH workbook HERE */
  };
  reader.readAsArrayBuffer(f);
}
drop_dom_element.addEventListener("drop", handleDrop, false);

Simple Download Example

/**
 * JSON Conversion Excel
 */
function jsonToSheet () {
    let json = [
        {
            "Name of person": "EJ",
            "Gender": "male",
        },
        {
            "Name of person": "LSQ",
            "Gender": "famale",
        }
    ]

    // Instantiate a workbook
    let book = XLSX.utils.book_new()

    // Instantiate a Sheet
    let sheet = XLSX.utils.json_to_sheet(json, {
        header: ['Name of person', 'Gender']
    })

    // Writes the Sheet to the workbook
    XLSX.utils.book_append_sheet(book, sheet, 'Sheet1')

    // Write to the file, directly trigger the browser download
    XLSX.writeFile(book, 'jsonToSheet.xlsx')
}

/**
* Array conversion Excel
 */
function arrayToSheet () {
    let data = [
        ['Name of person', 'Gender'],
        ['EJ', 'male'],
        ['LSQ', 'famale']
    ]

    // Instantiate a workbook
    let book = XLSX.utils.book_new()

    // Instantiate a Sheet
    let sheet = XLSX.utils.aoa_to_sheet(data)

    //Writes the Sheet to the workbook
    XLSX.utils.book_append_sheet(book, sheet, 'Sheet1')

    // Write to the file, directly trigger the browser download
    XLSX.writeFile(book, 'arrayToSheet.xlsx')
}

—END—

Open source protocol:Apache-2.0 License

资源下载此资源为免费资源立即下载
Telegram:@John_Software

Disclaimer: This article is published by a third party and represents the views of the author only and has nothing to do with this website. This site does not make any guarantee or commitment to the authenticity, completeness and timeliness of this article and all or part of its content, please readers for reference only, and please verify the relevant content. The publication or republication of articles by this website for the purpose of conveying more information does not mean that it endorses its views or confirms its description, nor does it mean that this website is responsible for its authenticity.

Ictcoder Free Source Code SheetJS works with traditional and modern software for new spreadsheets https://ictcoder.com/sheetjs-works-with-traditional-and-modern-software-for-new-spreadsheets/

Share free open-source source code

Q&A
  • 1. Automatic: After making an online payment, click the (Download) link to download the source code; 2. Manual: Contact the seller or the official to check if the template is consistent. Then, place an order and make payment online. The seller ships the goods, and both parties inspect and confirm that there are no issues. ICTcoder will then settle the payment for the seller. Note: Please ensure to place your order and make payment through ICTcoder. If you do not place your order and make payment through ICTcoder, and the seller sends fake source code or encounters any issues, ICTcoder will not assist in resolving them, nor can we guarantee your funds!
View details
  • 1. Default transaction cycle for source code: The seller manually ships the goods within 1-3 days. The amount paid by the user will be held in escrow by ICTcoder until 7 days after the transaction is completed and both parties confirm that there are no issues. ICTcoder will then settle with the seller. In case of any disputes, ICTcoder will have staff to assist in handling until the dispute is resolved or a refund is made! If the buyer places an order and makes payment not through ICTcoder, any issues and disputes have nothing to do with ICTcoder, and ICTcoder will not be responsible for any liabilities!
View details
  • 1. ICTcoder will permanently archive the transaction process between both parties and snapshots of the traded goods to ensure the authenticity, validity, and security of the transaction! 2. ICTcoder cannot guarantee services such as "permanent package updates" and "permanent technical support" after the merchant's commitment. Buyers are advised to identify these services on their own. If necessary, they can contact ICTcoder for assistance; 3. When both website demonstration and image demonstration exist in the source code, and the text descriptions of the website and images are inconsistent, the text description of the image shall prevail as the basis for dispute resolution (excluding special statements or agreements); 4. If there is no statement such as "no legal basis for refund" or similar content, any indication on the product that "once sold, no refunds will be supported" or other similar declarations shall be deemed invalid; 5. Before the buyer places an order and makes payment, the transaction details agreed upon by both parties via WhatsApp or email can also serve as the basis for dispute resolution (in case of any inconsistency between the agreement and the description of the conflict, the agreement shall prevail); 6. Since chat records and email records can serve as the basis for dispute resolution, both parties should only communicate with each other through the contact information left on the system when contacting each other, in order to prevent the other party from denying their own commitments. 7. Although the probability of disputes is low, it is essential to retain important information such as chat records, text messages, and email records, in case a dispute arises, so that ICTcoder can intervene quickly.
View details
  • 1. As a third-party intermediary platform, ICTcoder solely protects transaction security and the rights and interests of both buyers and sellers based on the transaction contract (product description, agreed content before the transaction); 2. For online trading projects not on the ICTcoder platform, any consequences are unrelated to this platform; regardless of the reason why the seller requests an offline transaction, please contact the administrator to report.
View details

Related Source code

ICTcoder Customer Service

24-hour online professional services