<script type="module">
/* load the codepage support library for extended support with older formats */
import { set_cptable } from "https://cdn.sheetjs.com/xlsx-0.18.9/package/xlsx.mjs";
import * as cptable from 'https://cdn.sheetjs.com/xlsx-0.18.9/package/dist/cpexcel.full.mjs';
set_cptable(cptable);
</script>
当然npm库也是有的,因为我们需要额外的样式定制,所以还引入了xlsx-style:
1 2
"xlsx":"^0.17.5", "xlsx-style":"^0.8.13"
使用npm库后,上面兼容XLS部分的标签换成npm库ESM导入的方式如下:
1 2 3 4
/* 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);
基础概念
SheetJS工作的数据基础格式是二维数组(Array of Array) 或者对象数组 (Array of Object).
/** Converts an array of arrays of JS data to a worksheet. */ aoa_to_sheet<T>(data: T[][], opts?: AOA2SheetOpts): WorkSheet; aoa_to_sheet(data: any[][], opts?: AOA2SheetOpts): WorkSheet;
/** Converts an array of JS objects to a worksheet. */ json_to_sheet<T>(data: T[], opts?: JSON2SheetOpts): WorkSheet; json_to_sheet(data: any[], opts?: JSON2SheetOpts): WorkSheet;
/** BROWSER ONLY! Converts a TABLE DOM element to a worksheet. */ table_to_sheet(data: any, opts?: Table2SheetOpts): WorkSheet; table_to_book(data: any, opts?: Table2SheetOpts): WorkBook; sheet_add_dom(ws: WorkSheet, data: any, opts?: Table2SheetOpts): WorkSheet;
/* --- Export Functions --- */
/** Converts a worksheet object to an array of JSON objects */ sheet_to_json<T>(worksheet: WorkSheet, opts?: Sheet2JSONOpts): T[]; sheet_to_json(worksheet: WorkSheet, opts?: Sheet2JSONOpts): any[][]; sheet_to_json(worksheet: WorkSheet, opts?: Sheet2JSONOpts): any[];
/** Set number format for a cell */ cell_set_number_format(cell: CellObject, fmt: string|number): CellObject;
/** Set hyperlink for a cell */ cell_set_hyperlink(cell: CellObject, target: string, tooltip?: string): CellObject;
/** Set internal link for a cell */ cell_set_internal_link(cell: CellObject, target: string, tooltip?: string): CellObject;
/** Add comment to a cell */ cell_add_comment(cell: CellObject, text: string, author?: string): void;
/** Assign an Array Formula to a range */ sheet_set_array_formula(ws: WorkSheet, range: Range|string, formula: string): WorkSheet;
/** Add an array of arrays of JS data to a worksheet */ sheet_add_aoa<T>(ws: WorkSheet, data: T[][], opts?: SheetAOAOpts): WorkSheet; sheet_add_aoa(ws: WorkSheet, data: any[][], opts?: SheetAOAOpts): WorkSheet;
/** Add an array of JS objects to a worksheet */ sheet_add_json(ws: WorkSheet, data: any[], opts?: SheetJSONOpts): WorkSheet; sheet_add_json<T>(ws: WorkSheet, data: T[], opts?: SheetJSONOpts): WorkSheet;
/** Worksheet Cell Object */ export interface CellObject { /** The raw value of the cell. Can be omitted if a formula is specified */ v?: string | number | boolean | Date;
/** Formatted text (if applicable) */ w?: string;
/** * The Excel Data Type of the cell. * b Boolean, n Number, e Error, s String, d Date, z Empty */ t: ExcelDataType;
/** Cell formula (if applicable) */ f?: string;
/** Range of enclosing array if formula is array formula (if applicable) */ F?: string;
/** Rich text encoding (if applicable) */ r?: any;
/** HTML rendering of the rich text (if applicable) */ h?: string;
/** Comments associated with the cell */ c?: Comments;
/** Number format string associated with the cell (if requested) */ z?: NumberFormat;