如何在工作表JS中设置列格式

时间:2019-05-08 09:41:24

标签: angular types

我正在尝试使用具有特殊列的sheetJS创建sheetsx,所有包含空单元格的单元格都作为文本。 我正在使用json_to_sheet。...

我设法将所有具有内容的单元格设置为格式的单元格屁股文本,但是将空单元格设置为“常规” 有没有办法找到特别的阴茎,并且有办法设置柱子?

downloadExcelFile(data: any[], sheetName: string, header?: string[], filename: string = "export") {
let workbook = XLSX.utils.book_new();
let worksheet = XLSX.utils.json_to_sheet(data, { header: header });
let mifiImsiColumn = XLSX.utils.decode_col("AF"); // 1
let mifiMsisdnColumn = XLSX.utils.decode_col("AG"); // 1
let fmt = '@'; // or '"$"#,##0.00_);[Red]\\("$"#,##0.00\\)' or any Excel number format


/* get worksheet range */
let range = XLSX.utils.decode_range(worksheet['!ref']);
for ( let i = range.s.r + 1; i <= range.e.r; ++i) {
  /* find the data cell (range.s.r + 1 skips the header row of the worksheet) */

  let ref = XLSX.utils.encode_cell({r: i , c: mifiMsisdnColumn});
  let ref1 = XLSX.utils.encode_cell({r: i , c: mifiImsiColumn});

  /* if the particular row did not contain data for the column, the cell will not be generated */
  if (!worksheet[ref] && !worksheet[ref1] ) { continue; }
  /* `.t == "n"` for number cells */
  if (worksheet[ref].t !== 'n'  && worksheet[ref1].t !== 'n' ) {

    worksheet[ref].z = fmt;
    worksheet[ref1].z = fmt; }

}

workbook.SheetNames.push(sheetName);
workbook.Sheets[sheetName] = worksheet;



const wbout = XLSX.write(workbook, {bookType: 'xlsx', bookSST: false, type: 'binary'});


/* the saveAs call downloads a file on the local machine */
FileSaver.saveAs(new Blob([this.s2ab(wbout)], {type: 'application/octet-stream'}), `${filename}.xlsx`);

}

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

可以使用excel js插件来完成。使用ws.getCell('A1').numFmt = '0.00%'标记列号类型为百分比。 有关更多信息,请参阅https://www.npmjs.com/package/exceljs#number-formats