博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
React实现将Excel文件转为html显示/转为json数据的demo
阅读量:4098 次
发布时间:2019-05-25

本文共 2171 字,大约阅读时间需要 7 分钟。

由SheetJS出品的js-xlsx是一款非常方便的只需要纯JS即可读取和导出excel的工具库,功能强大,支持格式众多,支持xls、xlsx、ods(一种OpenOffice专有表格文件格式)等十几种格式。

,进入选择react

import React, {
PureComponent } from 'react';import {
read, utils } from 'xlsx';/* list of supported file types */const SheetJSFT = [ "xlsx", "xlsb", "xlsm", "xls", "xml", "csv", "txt", "ods", "fods", "uos", "sylk", "dif", "dbf", "prn", "qpw", "123", "wb*", "wq*", "html", "htm"].map(function (x) {
return "." + x;}).join(",");class SheetPage extends PureComponent {
state = {
data: [], cols: [] } make_cols = (refstr: any) => {
let o = [], C = utils.decode_range(refstr).e.c + 1; for (var i = 0; i < C; ++i) o[i] = {
name: utils.encode_col(i), key: i } return o; }; onLoadFile = (e: {
target: {
files: any; }; }) => {
const files = e.target.files; if (files && files[0]) this.handleFile(files[0]); } handleFile = (file: Blob/*:File*/) => {
/* Boilerplate to set up FileReader */ const reader = new FileReader(); const rABS = !!reader.readAsBinaryString; reader.onload = (e) => {
/* Parse data */ const bstr = e.target.result; const wb = read(bstr, {
type: rABS ? 'binary' : 'array' }); /* Get first worksheet */ const wsname = wb.SheetNames[0]; const ws = wb.Sheets[wsname]; /* Convert array of arrays */ const data = utils.sheet_to_json(ws, {
header: 1 }); /* Update state */ this.setState({
data: data, cols: this.make_cols(ws['!ref']) }); }; if (rABS) reader.readAsBinaryString(file); else reader.readAsArrayBuffer(file); }; render() {
console.log(this.state.data) return (

新增页面

); }}class OutTable extends React.Component {
render() {
return (
{
this.props.cols.map((c) =>
)}
{
this.props.data.map((r, i) =>
{
this.props.cols.map(c =>
)}
)}
{
c.name}
{
r[c.key]}
); };};export default SheetPage;

转载地址:http://afqii.baihongyu.com/

你可能感兴趣的文章
Java编程基础:抽象类和接口
查看>>
Java编程基础:异常处理
查看>>
Java编程基础:了解面向对象
查看>>
新一代Java模板引擎Thymeleaf
查看>>
Spring MVC中使用Thymeleaf模板引擎
查看>>
Spring Boot构建简单的微博应用
查看>>
Spring处理表单提交
查看>>
Spring MVC异常处理
查看>>
Leetcode 1180. Count Substrings with Only One Distinct Letter [Python]
查看>>
PHP 7 的五大新特性
查看>>
php使用 memcache 来存储 session
查看>>
php实现socket(转)
查看>>
PHP底层的运行机制与原理
查看>>
深入了解php底层机制
查看>>
PHP中的stdClass 【转】
查看>>
XHProf-php轻量级的性能分析工具
查看>>
PHP7新特性 What will be in PHP 7/PHPNG
查看>>
比较strtr, str_replace和preg_replace三个函数的效率
查看>>
ubuntu 下编译PHP5.5.7问题:configure: error: freetype.h not found.
查看>>
PHP编译configure时常见错误 debian centos
查看>>