Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
/* xlsx.js (C) 2013-present SheetJS -- http://sheetjs.com */
/* from the electron quick-start */
var electron = require('electron');
var XLSX = require('xlsx');
var app = electron.app;
var win = null;
function createWindow() {
if(win) return;
win = new electron.BrowserWindow({width:800, height:600});
win.loadURL("file://" + __dirname + "/index.html");
win.webContents.openDevTools();
win.on('closed', function() { win = null; });
}
if(app.setAboutPanelOptions) app.setAboutPanelOptions({ applicationName: 'sheetjs-electron', applicationVersion: "XLSX " + XLSX.version, copyright: "(C) 2017-present SheetJS LLC" });
app.on('open-file', function() { console.log(arguments); });
app.on('ready', createWindow);
app.on('activate', createWindow);
app.on('window-all-closed', function() { if(process.platform !== 'darwin') app.quit(); });
doc.sheets = sheetNames.map(sheetName => {
const sheet = file.Sheets[sheetName];
const data = XLSX.utils.sheet_to_json(sheet, {
raw: true,
cellText: false
}); // jshint ignore:line
const headers = data.length ? Object.keys(data) : [];
return {
name: sheetName,
headers: headers,
rows: data.map(x => {
Object.keys(x)
.map(y => [y, !x[y].split ? x : x[y].split(';;').filter(x => !!x)])
.forEach(values => {
if (values[1].length > 1) x[values[0]] = values[1];
});
return Object.assign({}, x, { $doc_type: sheetName });
})
};
function _parseExcel(data) {
const fixedData = fixdata(data)
// btoa是binary to ascii 将binary的数据用ascii码表示
const workbook = XLSX.read(btoa(fixedData), {type: 'base64'})
// 读取多个sheet表
let sheetTable = []
const sheetList = workbook.Sheets
if (sheetList) {
Object.keys(sheetList).forEach(key => {
const worksheet = sheetList[key]
if (RANGE_KEY in worksheet) {
if (ignore && ignore.length) {
const wsRefArr = worksheet[RANGE_KEY].split(':')
const startCellRowNum = ignore[0].replace(/[a-zA-Z]+/, '') || 0
const startCellRowStr = ignore[0].replace(/[0-9]+/, '') || 'A'
const endCellRowNum = ignore[1].replace(/[a-zA-Z]+/, '') || 0
const maxNum =
startCellRowNum > endCellRowNum ? startCellRowNum : endCellRowNum
worksheet[RANGE_KEY] = `${startCellRowStr}${+maxNum + 1}:${
wsRefArr[1]
reader.onload = function (e) {
// 数据预处理
var binary = "";
var bytes = new Uint8Array(e.target.result);
var length = bytes.byteLength;
for (var i = 0; i < length; i++) {
binary += String.fromCharCode(bytes[i]);
}
/* 读取 workbook */
var wb = XLSX.read(binary, {
type: 'binary'
});
/* 选择第一个sheet */
var wsname = wb.SheetNames[0];
var ws = wb.Sheets[wsname];
/* excel转换json数组,加上{header:1}是普通数组,不写是对象数组 */
self.data = XLSX.utils.sheet_to_json(ws);
console.log(self.data);
/* 生成html表格 */
var HTML = XLSX.utils.sheet_to_html(ws);
document.getElementById('out-table').innerHTML = HTML;
/* 显示导出Excel按钮 */
const steps = resources.map(async res => {
if (excelFormats.includes(res.descriptor.format)) {
const buffer = await res.buffer
const workbook = XLSX.read(buffer, {type: 'buffer', bookSheets: true})
if (sheets === 'all') {
sheets = Array.from(Array(workbook.SheetNames.length).keys())
// We are using sheet idx starting from 1 so we need to add 1 to each idx:
sheets = sheets.map(value => value + 1)
} else if (sheets) { // When sheets are indexes of sheets separated by ','
sheets = sheets.split(',').map(value => parseInt(value))
} else { // Default case
sheets = [1]
}
for (let i of sheets) {
const rows = await toArray(await xlsxParser(res, false, i-1))
const schema = await infer(rows)
const step = {
input: res.descriptor.name,
output: `${res.descriptor.name}-sheet-${i}`,
tabulator: {
exportToExcel(): void {
try {
/* generate worksheet */
const ws: XLSX.WorkSheet = XLSX.utils.json_to_sheet(this.data);
/* generate workbook and add the worksheet */
const wb: XLSX.WorkBook = XLSX.utils.book_new();
XLSX.utils.book_append_sheet(wb, ws, 'Sheet1');
/* save to file */
XLSX.writeFile(wb, 'file.xlsx');
} catch (err) {
console.error('export error', err);
}
}
/* xlsx.js (C) 2013-present SheetJS -- http://sheetjs.com */
var XLSX = require('xlsx');
console.log(XLSX.version);
var w = XLSX.read('abc,def\nghi,jkl', {type:'binary'});
var j = XLSX.utils.sheet_to_json(w.Sheets[w.SheetNames[0]], {header:1});
console.log(j);
// 引入 xlsx 模块
// https://github.com/SheetJS/js-xlsx
var xlsx = require("xlsx");
var fs = require("fs");
// workbook 对象表示整份 Excel 文档
// 一个 workbook 对象包含了 N 个 sheets(看 Excel 文档的左下角)
var workbook = xlsx.readFileSync('example.xlsx');
// 打印出每个 sheet 的 name
for (var i in workbook.SheetNames)
console.log(workbook.SheetNames[i]);
// 获取第一个 sheet 的名字
var firstSheetName = workbook.SheetNames[0];
// 获取第一个 sheet 对象
// 通过表名获取表格
var sheet = workbook.Sheets[firstSheetName];
// 返回表的有效范围
// A1:E180
// console.log(sheet['!ref']);
getHeaderRow(sheet) {
const headers = []
const range = XLSX.utils.decode_range(sheet['!ref'])
let C
const R = range.s.r /* start in the first row */
for (C = range.s.c; C <= range.e.c; ++C) { /* walk every column in the range */
var cell = sheet[XLSX.utils.encode_cell({ c: C, r: R })] /* find the cell in the first row */
var hdr = 'UNKNOWN ' + C // <-- replace with your desired default
if (cell && cell.t) hdr = XLSX.utils.format_cell(cell)
headers.push(hdr)
}
return headers
}
}
get_header_row(sheet) {
const headers = []
const range = XLSX.utils.decode_range(sheet['!ref'])
let C
const R = range.s.r /* start in the first row */
for (C = range.s.c; C <= range.e.c; ++C) { /* walk every column in the range */
var cell = sheet[XLSX.utils.encode_cell({ c: C, r: R })] /* find the cell in the first row */
var hdr = 'UNKNOWN ' + C // <-- replace with your desired default
if (cell && cell.t) hdr = XLSX.utils.format_cell(cell)
headers.push(hdr)
}
return headers
},
generateDate(header, body) {