欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

react導(dǎo)出excel文件的四種方式

 更新時間:2023年11月23日 10:37:29   作者:摸魚第一人  
本文主要介紹了react導(dǎo)出excel文件的四種方式,主要包括原生js導(dǎo)出,使用?js-export-excel,使用xlsx導(dǎo)出, 使用react-html-table-to-excel,感興趣的可以了解一下

 一共總結(jié)了四種方法  前兩種適用范圍比較廣泛 可以適用導(dǎo)出多級表頭合并等,第三種方法導(dǎo)出的文件比較中規(guī)中矩,但是支持導(dǎo)出多張sheet表。第四種方法導(dǎo)出不推薦使用

1、原生js導(dǎo)出  (帶樣式)

/**
 *  原生JS導(dǎo)出為excel文件
 */
export const jsToExcel = (id, name) => {
    //window.location.href='<%=basePath%>pmb/excelShowInfo.do';
    //獲取表格
    var exportFileContent = document.getElementById(id).outerHTML;
    //設(shè)置格式為Excel,表格內(nèi)容通過btoa轉(zhuǎn)化為base64,此方法只在文件較小時使用(小于1M)
    //exportFileContent=window.btoa(unescape(encodeURIComponent(exportFileContent)));
    //var link = "data:"+MIMEType+";base64," + exportFileContent;
    //使用Blob
    var blob = new Blob([exportFileContent], { type: "text/plain;charset=utf-8" });         //解決中文亂碼問題
    blob = new Blob([String.fromCharCode(0xFEFF), blob], { type: blob.type });
    //設(shè)置鏈接
    var link = window.URL.createObjectURL(blob);
    var a = document.createElement("a");    //創(chuàng)建a標(biāo)簽
    a.download = name;  //設(shè)置被下載的超鏈接目標(biāo)(文件名)   建議文件后綴為 .xls
    a.href = link;                            //設(shè)置a標(biāo)簽的鏈接
    document.body.appendChild(a);            //a標(biāo)簽添加到頁面
    a.click();                                //設(shè)置a標(biāo)簽觸發(fā)單擊事件
    document.body.removeChild(a);            //移除a標(biāo)簽
}

使用方式   

<table id='table_report'>...</table>
<div onClick={() => jsToExcel('table_report', '現(xiàn)券交易異常日報.xls')}>導(dǎo)出</div>

 如果想導(dǎo)出xlsx格式請參考方法2,方法1僅改文件后綴 不會被Excel識別  但是wps可以

2、使用xlsx導(dǎo)出(此方法導(dǎo)出的excel文件無樣式,但導(dǎo)出的文件格式是 xlsx格式的) 

首先安裝xlsx  : yarn add xlsx

import XLSX from "xlsx"


/**
 *  用XLSX導(dǎo)出 (導(dǎo)出無樣式)
 */
export const exportExcel = (id, name) => {
    var exportFileContent = document.getElementById(id).cloneNode(true);
    var wb = XLSX.utils.table_to_book(exportFileContent, { sheet: "sheet1" });
    XLSX.writeFile(wb, name);
}

    使用方式

<table id='table_report'>...</table>
<div onClick = {() => exportExcel('table_report', '現(xiàn)券交易異常日報.xlsx')}>導(dǎo)出</div>

3、使用 js-export-excel (可以導(dǎo)出多張sheet表)

首先安裝 js-export-excel  : yarn add js-export-excel 

import { Table } from 'antd';
import { columns } from './config';
import ExportJsonExcel from "js-export-excel";
import { PlusCircleOutlined } from '@ant-design/icons';

function Tables(props) {
  const { isLoading, viewData, data } = props;
  // data格式
  const data1 = [
    {
      adID: "張三",
      leaveCount: 26,
      leaveDuration: 82,
      leaveType: "調(diào)休",
      name: "張三"
    },
    {
      adID: "張三1",
      leaveCount: 526,
      leaveDuration: 82,
      leaveType: "調(diào)休",
      name: "張三1"
    },
    {
      adID: "張三1",
      leaveCount: 26,
      leaveDuration: 852,
      leaveType: "調(diào)休",
      name: "張三1"
    },
    {
      adID: "張三1",
      leaveCount: 256,
      leaveDuration: 82,
      leaveType: "調(diào)休",
      name: "張三1"
    },
  ]
  /**
   *  導(dǎo)出數(shù)據(jù)
   */
  const handleExportCurrentExcel = (data) => {
    let sheetFilter = ["name", "leaveType", "leaveCount", "leaveDuration"];
    let sheetFilter2 = ["name", "leaveType", "leaveCount", "leaveDuration"];
    let option = {};
    option.fileName = '考勤分析結(jié)果';
    option.datas = [
      {
        sheetData: data1,
        sheetName: '考勤分析結(jié)果',
        sheetFilter: sheetFilter,
        sheetHeader: ['姓名', '類型', '次數(shù)', '時長'],
        columnWidths: [10, 10, 10, 10]
      },
      {
        sheetData: data1,  //比較懶得造數(shù)據(jù)了  跟表1數(shù)據(jù)一樣
        sheetName: '考勤分析結(jié)果222',
        sheetFilter: sheetFilter2,
        sheetHeader: ['姓名22', '類型22', '次數(shù)22', '時長22'],
        columnWidths: [10, 10, 10, 10]
      },
    ];
    var toExcel = new ExportJsonExcel(option); //new
    toExcel.saveExcel(); //保存
  }

  return (
    <div>
      <div className='exportButton' onClick={() => handleExportCurrentExcel(data)}>
        <PlusCircleOutlined className='icon-but' />
        導(dǎo)出當(dāng)前數(shù)據(jù)
      </div>
      <Table
        loading={isLoading}
        columns={columns}
        dataSource={viewData}
        pagination={false}
      />
    </div>
  )
}
export default Tables;

4、第四種 使用react-html-table-to-excel  不推薦使用

安裝   react-html-table-to-excel : yarn add react-html-table-to-excel

import React, {  useRef, useEffect } from 'react';
import { Table } from "antd";
import {  columns } from './config';
import ReactHTMLTableToExcel from 'react-html-table-to-excel';
import styles from './index.module.less';
function StudyExcel() {

    const data = [
        {
            key: '0',
            name: '張三'
        },
        {
            key: '1',
            name: '趙四'
        },
        {
            key: '2',
            name: '王五'
        },
        {
            key: '3',
            name: '齊六'
        }
    ];

    // 用ref來獲取組件按鈕實例,使用里面的方法
    const buttonRef = useRef(null);

    // 禁止組件按鈕的默認點擊事件
    useEffect(() => {
        const button = document.querySelector('#test-table-xls-button');
        button.style['pointer-events'] = ('none');
    }, []);


    // 導(dǎo)出表格
    const exportTable = (e) => {
        e.stopPropagation();
        const table = document.getElementsByTagName('table');
        const container = document.querySelector('#hiddenBox');
        const tempTable = document.createElement('table');
        tempTable.appendChild(table[0]);
        tempTable.setAttribute('id', 'table-to-xls');                    // 給table添加id,值與按鈕上的table字段對應(yīng)
        container.appendChild(tempTable);                                // 把創(chuàng)建的節(jié)點添加到頁面容器中
        buttonRef.current.handleDownload();                              // 手動觸發(fā)下載
    };
    return (
        <div style={{ backgroundColor: '#fff' }} className={styles.container}>
            <span onClick={(e) => exportTable(e)}>
                <ReactHTMLTableToExcel
                    width={1900}
                    ref={buttonRef}
                    table="table-to-xls"
                    id='test-table-xls-button'
                    filename='回購日報'
                    sheet='表1'
                    buttonText='導(dǎo)出Excel'
                />
            </span>
            <Table
                columns={columns}
                dataSource={data}
                bordered
                pagination={false}
            />
            <div id='hiddenBox' style={{ position: 'absolute', zIndex: -1, top: 0, left: 0 }} />
        </div>
    )
}
export default StudyExcel;

 到此這篇關(guān)于react導(dǎo)出excel文件的四種方式的文章就介紹到這了,更多相關(guān)react導(dǎo)出excel文件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • React 組件通信的多種方式與最佳實踐

    React 組件通信的多種方式與最佳實踐

    在現(xiàn)代前端開發(fā)中,組件化是 React 的核心理念之一,組件之間的通信是構(gòu)建復(fù)雜應(yīng)用的重要組成部分,在這篇文章中,我們將深入探討 React 組件通信的多種方式,包括它們的優(yōu)缺點、使用場景以及最佳實踐,需要的朋友可以參考下
    2024-11-11
  • React中前端路由的示例代碼

    React中前端路由的示例代碼

    本文主要介紹了React中前端路由的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-04-04
  • react antd實現(xiàn)動態(tài)增減表單

    react antd實現(xiàn)動態(tài)增減表單

    antd是react流行的ui框架庫,本文主要介紹了react antd實現(xiàn)動態(tài)增減表單,分享給大家,感興趣的小伙伴們可以參考一下
    2021-06-06
  • ahooks解決React閉包問題方法示例

    ahooks解決React閉包問題方法示例

    這篇文章主要為大家介紹了ahooks解決React閉包問題方法示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-07-07
  • react實現(xiàn)移動端下拉菜單的示例代碼

    react實現(xiàn)移動端下拉菜單的示例代碼

    這篇文章主要介紹了react實現(xiàn)移動端下拉菜單的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-01-01
  • 如何對react hooks進行單元測試的方法

    如何對react hooks進行單元測試的方法

    這篇文章主要介紹了如何對react hooks進行單元測試的方法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-08-08
  • 解決React報錯Expected an assignment or function call and instead saw an expression

    解決React報錯Expected an assignment or funct

    這篇文章主要為大家介紹了React報錯Expected an assignment or function call and instead saw an expression解決方案詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-12-12
  • create-react-app如何降低react的版本

    create-react-app如何降低react的版本

    這篇文章主要介紹了create-react-app降低react的版本方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-03-03
  • React中useCallback useMemo使用方法快速精通

    React中useCallback useMemo使用方法快速精通

    在React函數(shù)組件中,當(dāng)組件中的props發(fā)生變化時,默認情況下整個組件都會重新渲染。換句話說,如果組件中的任何值更新,整個組件將重新渲染,包括沒有更改values/props的函數(shù)/組件。在react中,我們可以通過memo,useMemo以及useCallback來防止子組件的rerender
    2023-02-02
  • React Router 用法詳細介紹

    React Router 用法詳細介紹

    React Router 是 React 中用于處理頁面導(dǎo)航和路由的工具,它使用 HTML5 History API 或 URL 的哈希部分進行路由管理,本文介紹React Router 用法詳細講解,感興趣的朋友一起看看吧
    2025-01-01

最新評論