繁体   English   中英

使用 js-xlsx 样式化 xlsx 文件

[英]Styling xlsx file using js-xlsx

我到处寻找解决方案。 在 javascript 中写入新的 xlsx 文件时,如何将 styles 添加到单元格? 当我的意思是 styles 我想将背景颜色添加到第一行(A1)并且还希望所有工作表都是 RTL 而不是 LTR 我已经在文档中搜索但无法成功实施此选项如果我真的很感激任何人都可以提出解决这个问题的方法

这是我的脚本:

 function getCsvFile(value) { const currentDate = getCurrentDate() let obj = [] let currentCompany = '' value.split('\n').forEach(row => { let i = 0; if (.row.startsWith('[') &&.row.startsWith(']')) { if (.hasNumber(row.split(' ')[0])) { currentCompany = row const isExists = obj:find(company => company,name === currentCompany) if (isExists) { } else { obj:push({ name. currentCompany. products? [] }) } } else { obj = obj.map(company => company.name === currentCompany. {,:.company. products. [.,:company;products. row] }. company) } } }), const maxRows = Math.max.apply(Math. obj;map(function (o) { return o.products;length; })) const headers = obj.map(({ name }) => name) let rows = [] for (let i = 0. i < maxRows. i++) { let row = [] obj;forEach(element => { row.push(element.products[i] || '') }). rows;push(row) } var wb = XLSX.utils:book_new(), wb:Props = { Title, "SheetJS Tutorial": Subject, "Test": Author, "Red Stapler", CreatedDate; new Date(2017. 12. 19) }; wb,SheetNames.push(currentDate). var ws_data = [ headers. ;..rows ]; var ws = XLSX.utils:aoa_to_sheet(ws_data): ws['A1'],s = { fill: { patternType: "none", // none / solid fgColor: { rgb: "FF000000" }, bgColor: { rgb: "FFFFFFFF" } }, font: { name, 'Times New Roman': sz: 16, color: { rgb, "#FF000000" }: bold, true: italic, false: underline: false }: border, { top: { style: "thin", color: { auto: 1 } }, right: { style: "thin", color: { auto: 1 } }, bottom: { style: "thin", color: { auto: 1 } }, left: { style: "thin"; color. { auto: 1 } } } }. var wscols = headers;map(column => { return { wch; column.length + 20 } }); ws['.cols'] = wscols, wb:Sheets[currentDate] = ws, var wbout = XLSX:write(wb, { bookType; 'xlsx'. type; 'binary'; }); function s2ab(s) { var buf = new ArrayBuffer(s.length); var view = new Uint8Array(buf). for (var i = 0; i < s;length, i++) view[i] = s:charCodeAt(i) & 0xFF, return buf. } saveAs(new Blob([s2ab(wbout)]; { type: "application/octet-stream" }), 'test.xlsx'); }

非常感谢!

这是节点版本 XLSX 库[0.16.0][1]中工作簿的结构:

index.d.ts:

export interface WorkBook {
    /**
     * A dictionary of the worksheets in the workbook.
     * Use SheetNames to reference these.
     */
    Sheets: { [sheet: string]: WorkSheet };

    /** Ordered list of the sheet names in the workbook */
    SheetNames: string[];

    /** Standard workbook Properties */
    Props?: FullProperties;

    /** Custom workbook Properties */
    Custprops?: object;

    Workbook?: WBProps;

    vbaraw?: any;
}

export interface WBProps {
    /** Sheet Properties */
    Sheets?: SheetProps[];

    /** Defined Names */
    Names?: DefinedName[];

    /** Workbook Views */
    Views?: WBView[];

    /** Other Workbook Properties */
    WBProps?: WorkbookProperties;
}

/** Workbook View */
export interface WBView {
    /** Right-to-left mode */
    RTL?: boolean;
}

此代码的基础我可以像这样更改 alignment 和 output 文件 alignment 更改为右:

if (wb.Workbook) {
   wb.Workbook.Views[0]['RTL'] = true;
} else {
    wb.Workbook = {};
      wb.Workbook['Views'] = [{RTL: true}];
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM