How to use xlsx

Comprehensive xlsx code examples:

How to use xlsx.parse:

721
722
723
724
725
726
727
728
729
  res.makeError(400, '请上传 .xls 或 .xlsx文件!');
  if (next)
    return next('route');
}

// let parseData = xlsx.parse(file.path);
let parseData = xlsx.readFile(file.path);
let resultArr = [];
let first = true;

How to use xlsx.stream:

17
18
19
20
21
22
23
24
25
26
const dataWriter = await Datatable.create();

const worksheet = workbook.Sheets[args.sheetname || workbook.SheetNames[0]];
const range = (isInteger(args.range) && `A${args.range}:`) || args.range || undefined;

const stream = XLSX.stream.to_json(
  worksheet,
  {
    range,
    raw: false,

How to use xlsx.writeFileXLSX:

35
36
37
38
39
40
41
42
43
44
  return flatRow;
}));
const workbook = XLSX.utils.book_new();
//const sheetrr= flatten(worksheet, flatRow);
XLSX.utils.book_append_sheet(workbook, worksheet, "Sheet1");
// XLSX.writeFileXLSX(workbook,"MYTABLE.xlsx");
const fileBuffer = XLSX.write(workbook, {
  bookType: "xlsx",
  type: "array"
});

How to use xlsx.SSF:

22
23
24
25
26
27
28
29
30
var cell_ref = XLSX.utils.encode_cell({c:C,r:R});

if(typeof cell.v === 'number') cell.t = 'n';
else if(typeof cell.v === 'boolean') cell.t = 'b';
else if(cell.v instanceof Date) {
  cell.t = 'n'; cell.z = XLSX.SSF._table[14];
  cell.v = datenum(cell.v);
}
else cell.t = 's';

How to use xlsx.default:

107
108
109
110
111
112
113
114
115
116
const input = fs_1.default.readFileSync(files["ravex"][0].path, { encoding: "binary" });
const filePath = path_1.default.resolve(__dirname, "..", "..", "..", "tmp");
const fileHash = crypto_1.default.randomBytes(10).toString("hex");
fs_1.default.writeFileSync(filePath + "/" + fileHash + ".txt", input);
let ravexDataRaw = [];
const fileRavex = xlsx_1.default.readFile(filePath + "/" + fileHash + ".txt", { dense: true });
const ravexHeaderListOfList = xlsx_1.default.utils.sheet_to_json(fileRavex.Sheets[fileRavex.SheetNames[0]], {
    defval: "",
    raw: false,
    header: 1,

How to use xlsx.write:

62
63
64
65
66
67
68
69
70
71
    var name = worksheet.name || 'Sheet';
    var data = sheet_from_array_of_arrays(worksheet.data || []);
    wb.SheetNames.push(name);
    wb.Sheets[name] = data;
  });
  var data = XLSX.write(wb, _.defaults(options || {}, defaults));
  if(!data) return false;
  var buffer = new Buffer(data, 'binary');
  return buffer;
}

How to use xlsx.read:

44
45
46
47
48
49
50
51
52
53

module.exports = {
  parse: function(mixed, options) {
    var ws;
    if(typeof mixed === 'string') ws = XLSX.readFile(mixed, options);
    else ws = XLSX.read(mixed, options);
    return _.map(ws.Sheets, function(sheet, name) {
      return {name: name, data: XLSX.utils.sheet_to_json(sheet, {header: 1, raw: true})};
    });
  },

How to use xlsx.writeFile:

67
68
69
70
71
72
73
74
75
76
    var ws = sheet_from_array_of_arrays(twodarr);
    wb.Sheets[sheetdispname] = ws;
    if (process.env.debug)
      console.log(filename, '/', sheetdispname);
  }
  xlsx.writeFile(wb, filename);
}

function convertObjArray(objarray) {
  try {

How to use xlsx.readFile:

43
44
45
46
47
48
49
50
51
52
}

module.exports = {
  parse: function(mixed, options) {
    var ws;
    if(typeof mixed === 'string') ws = XLSX.readFile(mixed, options);
    else ws = XLSX.read(mixed, options);
    return _.map(ws.Sheets, function(sheet, name) {
      return {name: name, data: XLSX.utils.sheet_to_json(sheet, {header: 1, raw: true})};
    });

How to use xlsx.utils:

17
18
19
20
21
22
23
24
25
26
if(range.s.c > C) range.s.c = C;
if(range.e.r < R) range.e.r = R;
if(range.e.c < C) range.e.c = C;
var cell = {v: data[R][C] };
if(cell.v === null) continue;
var cell_ref = XLSX.utils.encode_cell({c:C,r:R});

if(typeof cell.v === 'number') cell.t = 'n';
else if(typeof cell.v === 'boolean') cell.t = 'b';
else if(cell.v instanceof Date) {