【问题标题】:How can I style cells in react-export-excel?如何在 react-export-excel 中设置单元格样式?
【发布时间】:2020-01-15 17:03:15
【问题描述】:

我正在使用 react-export-excel(最新 0.5.3) 工作正常。

我不知道如何设置单元格样式? (link to documentation

我想要:

  1. 扩展一些单元格宽度。
  2. 适当时将类型设置为数字或日期

这是运行良好的示例代码的一部分taken from here

import React from "react";
import ReactExport from "react-export-excel";

const ExcelFile = ReactExport.ExcelFile;
const ExcelSheet = ReactExport.ExcelFile.ExcelSheet;
const ExcelColumn = ReactExport.ExcelFile.ExcelColumn;

const dataSet1 = [
    {
        name: "Johson",
        amount: 30000,
        sex: 'M',
        is_married: true
    },
    {
        name: "Monika",
        amount: 355000,
        sex: 'F',
        is_married: false
    },
    {
        name: "John",
        amount: 250000,
        sex: 'M',
        is_married: false
    },
    {
        name: "Josef",
        amount: 450500,
        sex: 'M',
        is_married: true
    }
];


class Download extends React.Component {
    render() {
        return (
            <ExcelFile element={<button>Download Data</button>}>
                <ExcelSheet data={dataSet1} name="Employees">
                    <ExcelColumn label="Name" value="name"/>
                    <ExcelColumn label="Wallet Money" value="amount"/>
                    <ExcelColumn label="Gender" value="sex"/>
                    <ExcelColumn label="Marital Status"
                                 value={(col) => col.is_married ? "Married" : "Single"}/>
                </ExcelSheet>

            </ExcelFile>
        );
    }
}

【问题讨论】:

    标签: excel reactjs


    【解决方案1】:

    在包的非分叉版本中有一个示例可能对您有所帮助:

    https://github.com/securedeveloper/react-data-export/blob/master/examples/styled_excel_sheet.md

    import React, {Component} from 'react';
    import ReactExport from 'react-data-export';
    import './App.css';
    
    const ExcelFile = ReactExport.ExcelFile;
    const ExcelSheet = ReactExport.ExcelFile.ExcelSheet;
    
    const multiDataSet = [
        {
            columns: [
                {title: "Headings", width: {wpx: 80}},//pixels width 
                {title: "Text Style", width: {wch: 40}},//char width 
                {title: "Colors", width: {wpx: 90}},
            ],
            data: [
                [
                    {value: "H1", style: {font: {sz: "24", bold: true}}},
                    {value: "Bold", style: {font: {bold: true}}},
                    {value: "Red", style: {fill: {patternType: "solid", fgColor: {rgb: "FFFF0000"}}}},
                ],
                [
                    {value: "H2", style: {font: {sz: "18", bold: true}}},
                    {value: "underline", style: {font: {underline: true}}},
                    {value: "Blue", style: {fill: {patternType: "solid", fgColor: {rgb: "FF0000FF"}}}},
                ],
                [
                    {value: "H3", style: {font: {sz: "14", bold: true}}},
                    {value: "italic", style: {font: {italic: true}}},
                    {value: "Green", style: {fill: {patternType: "solid", fgColor: {rgb: "FF00FF00"}}}},
                ],
                [
                    {value: "H4", style: {font: {sz: "12", bold: true}}},
                    {value: "strike", style: {font: {strike: true}}},
                    {value: "Orange", style: {fill: {patternType: "solid", fgColor: {rgb: "FFF86B00"}}}},
                ],
                [
                    {value: "H5", style: {font: {sz: "10.5", bold: true}}},
                    {value: "outline", style: {font: {outline: true}}},
                    {value: "Yellow", style: {fill: {patternType: "solid", fgColor: {rgb: "FFFFFF00"}}}},
                ],
                [
                    {value: "H6", style: {font: {sz: "7.5", bold: true}}},
                    {value: "shadow", style: {font: {shadow: true}}},
                    {value: "Light Blue", style: {fill: {patternType: "solid", fgColor: {rgb: "FFCCEEFF"}}}}
                ]
            ]
        }
    ];
    
    class App extends Component {
        render() {
            return (
                <div>
                    <ExcelFile element={<button>Download Data With Styles</button>}>
                        <ExcelSheet dataSet={multiDataSet} name="Organization"/>
                    </ExcelFile>
                </div>
            );
        }
    }
    

    该库还带有一个用于单元格的 numFmt 属性,在这里您可以看到不同的值:

    /* ExcelNumFormat */
    type ExcelNumFormat = "0" | "0.00%" | "0.0%" | "0.00%;\\(0.00%\\);\\-;@" | "m/dd/yy" | string;
    

    https://github.com/securedeveloper/react-data-export/blob/master/types/types.md

    【讨论】:

    • 你是对的。谢谢。只是强调您必须在 ExcelSheet 道具中使用 ``` ``` 数据集而不是 data。不是最好的设计...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-01
    • 2012-05-22
    • 2022-11-17
    • 1970-01-01
    相关资源
    最近更新 更多