1.1.0 • Published 1 year ago

table-xlsx-quot v1.1.0

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

table-xlsx

npm version Build Status codecov NPM downloads

基于SheetJS封装,帮助你快速将xlsx文件转换成表格数据或表格数据导出生成xlsx文件,导出支持简单样式设置、多sheet页、行/列合并等

文档&演示地址:https://pengchen96.github.io/table-xlsx/docs/intro

📦 安装

使用npm或yarn

npm install --save table-xlsx @pengchen/xlsx
# or
yarn add table-xlsx @pengchen/xlsx

使用CDN

<!-- 注意: 部署时,将 "development.umd.js" 替换为 "production.umd.js"-->
<script crossorigin src="https://unpkg.com/table-xlsx/dist/table-xlsx.development.umd.js"></script>

如果使用CDN的方式引入table-xlsx,建议设置crossorigin属性

CDNURL
unpkghttps://unpkg.com/table-xlsx/
jsDelivrhttps://jsdelivr.com/package/npm/table-xlsx

🔨 使用

导出

import { exportFile } from "table-xlsx";

const columns = [
  { title: '姓名', dataIndex: 'name' },
  { title: '年龄', dataIndex: 'age' },
  { title: '住址', dataIndex: 'address' },
];
const dataSource = [
  { key: '1', name: '胡彦斌', age: 32, address: '西湖区湖底公园1号' },
  { key: '2', name: '胡彦祖', age: 42, address: '西湖区湖底公园1号' },
];

exportFile({ columns, dataSource });

解析

import { parseFile } from "table-xlsx";

parseFile({ file/*binary*/ }).then((result) => {
  // result = {
  //   wb, // wookbook对象
  //   tables: [{
  //     sheetName,
  //     dataSource,
  //     columns,
  //   },
  //   ...
  //   ]
  // }
});

📖 API

exportFile

参数说明类型默认值
columns表格列array[]
cellStyle单元格样式object{}
headerCellStyle表头单元格样式object{}
bodyCellStyle主体单元格样式object{}
dataSource表格数据array[]
fileName文件名string'table.xlsx'
showHeader显示表头booltrue
raw是否格式化值的类型boolfalse
sheetNamessheet页array'sheet1'
useRender使用render返回的值booleantrue
onTxBodyRow设置表格主体行,@return里可设置单元格样式function(record, index) { return { style } }-

columns

参数说明类型默认值
dataIndex列数据在数据项中对应的路径,支持通过数组查询嵌套路径string/string[]-
title列头显示文字string-
width列宽度string/number100
txHeaderCellStyle列头单元格样式object-
render生成复杂数据的渲染函数,参数分别为当前行的值,当前行数据,行索引,@return 里面可以设置表格行/列合并function(text, record, index) {}-
onTxBodyCell设置表格主体单元格,@return里可设置单元格样式;如果是多级表头,需要设置在末级列function(record, index) { return { style } }-

cellStyle、headerCellStyle、bodyCellStyle、txHeaderCellStyle、onTxBodyCell().style

参数说明类型默认值
fontName字体string'Calibri'
fontColorRgb字体颜色(ARGB)string'333333'
fontBold加粗booleanfalse
fillFgColorRgb背景填充颜色(ARGB)string'ffffff'
borderStyle边框风格(thin、medium、thick、dotted、hair、dashed、mediumDashed、dashDot、mediumDashDot、dashDotDot、mediumDashDotDot、slantDashDot)string'thin'
borderColorRgb边框颜色(ARGB)string'd1d3d8'
alignmentHorizontal水平对齐(left、center、right)string'center'
alignmentVertical垂直对齐(top、center、bottom)string'center'
alignmentWrapText自动换行(true、false)booleanfalse
alignmentReadingOrder阅读顺序number2
alignmentTextRotation文本旋转(0 to 180 or 255)number0

parseFile

参数说明类型默认值
file文件binary-

输出内容

    [
      {
        columns: [],
        dataSource: [],
        sheetName: ''
      },
      ...
    ]

📝 License

table-xlsx is available under the MIT License.

1.1.0

1 year ago