mirror of
https://github.com/DeNNiiInc/dbgate.git
synced 2026-04-18 19:36:00 +00:00
using xlsx lib instead of exceljs, export excel files
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
const exceljs = require('exceljs');
|
||||
const xlsx = require('xlsx');
|
||||
const _ = require('lodash');
|
||||
|
||||
module.exports = {
|
||||
@@ -6,14 +6,7 @@ module.exports = {
|
||||
|
||||
analyseExcel_meta: 'get',
|
||||
async analyseExcel({ filePath }) {
|
||||
const workbook = new exceljs.Workbook();
|
||||
await workbook.xlsx.readFile(filePath);
|
||||
return {
|
||||
tables: workbook.worksheets.map((sheet) => {
|
||||
const header = sheet.getRow(1);
|
||||
const columns = _.range(header.cellCount).map((index) => ({ columnName: header.getCell(index + 1).value }));
|
||||
return { pureName: sheet.name, columns };
|
||||
}),
|
||||
};
|
||||
const workbook = xlsx.readFile(filePath, { bookSheets: true });
|
||||
return workbook.SheetNames;
|
||||
},
|
||||
};
|
||||
|
||||
@@ -12,6 +12,7 @@ const dbgateApi = require(process.env.DBGATE_API || "dbgate-api");
|
||||
require=null;
|
||||
async function run() {
|
||||
${script}
|
||||
await dbgateApi.finalizer.run();
|
||||
console.log('Finished job script');
|
||||
}
|
||||
dbgateApi.runScript(run);
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
const exceljs = require('exceljs');
|
||||
const xlsx = require('xlsx');
|
||||
const stream = require('stream');
|
||||
const _ = require('lodash');
|
||||
|
||||
@@ -8,28 +8,28 @@ async function loadWorkbook(fileName) {
|
||||
let workbook = loadedWorkbooks[fileName];
|
||||
if (workbook) return workbook;
|
||||
console.log(`Loading excel ${fileName}`);
|
||||
workbook = new exceljs.Workbook();
|
||||
await workbook.xlsx.readFile(fileName);
|
||||
workbook = xlsx.readFile(fileName);
|
||||
loadedWorkbooks[fileName] = workbook;
|
||||
return workbook;
|
||||
}
|
||||
|
||||
async function excelSheetReader({ fileName, sheetName, limitRows = undefined }) {
|
||||
const workbook = await loadWorkbook(fileName);
|
||||
const sheet = workbook.getWorksheet(sheetName);
|
||||
const sheet = workbook.Sheets[sheetName];
|
||||
|
||||
const pass = new stream.PassThrough({
|
||||
objectMode: true,
|
||||
});
|
||||
const header = sheet.getRow(1);
|
||||
const rows = xlsx.utils.sheet_to_json(sheet, { header: 1 });
|
||||
const header = rows[0];
|
||||
const structure = {
|
||||
columns: _.range(header.cellCount).map((index) => ({ columnName: header.getCell(index + 1).value })),
|
||||
columns: _.range(header.length).map((index) => ({ columnName: header[index] })),
|
||||
};
|
||||
pass.write(structure);
|
||||
for (let rowIndex = 2; rowIndex <= sheet.rowCount; rowIndex++) {
|
||||
if (limitRows && rowIndex > limitRows + 1) break;
|
||||
const row = sheet.getRow(rowIndex);
|
||||
const rowData = _.fromPairs(structure.columns.map((col, index) => [col.columnName, row.getCell(index + 1).value]));
|
||||
for (let rowIndex = 1; rowIndex < rows.length; rowIndex++) {
|
||||
if (limitRows && rowIndex > limitRows) break;
|
||||
const row = rows[rowIndex];
|
||||
const rowData = _.fromPairs(structure.columns.map((col, index) => [col.columnName, row[index]]));
|
||||
if (_.isEmpty(_.omitBy(rowData, (v) => v == null || v.toString().trim().length == 0))) continue;
|
||||
pass.write(rowData);
|
||||
}
|
||||
|
||||
56
packages/api/src/shell/excelSheetWriter.js
Normal file
56
packages/api/src/shell/excelSheetWriter.js
Normal file
@@ -0,0 +1,56 @@
|
||||
const xlsx = require('xlsx');
|
||||
const stream = require('stream');
|
||||
const finalizer = require('./finalizer');
|
||||
|
||||
const writingWorkbooks = {};
|
||||
|
||||
async function saveExcelFiles() {
|
||||
for (const file in writingWorkbooks) {
|
||||
xlsx.writeFile(writingWorkbooks[file], file);
|
||||
}
|
||||
}
|
||||
|
||||
finalizer.register(saveExcelFiles);
|
||||
|
||||
function createWorkbook(fileName) {
|
||||
let workbook = writingWorkbooks[fileName];
|
||||
if (workbook) return workbook;
|
||||
workbook = xlsx.utils.book_new();
|
||||
writingWorkbooks[fileName] = workbook;
|
||||
return workbook;
|
||||
}
|
||||
|
||||
class ExcelSheetWriterStream extends stream.Writable {
|
||||
constructor({ fileName, sheetName }) {
|
||||
super({ objectMode: true });
|
||||
this.rows = [];
|
||||
this.structure = null;
|
||||
this.fileName = fileName;
|
||||
this.sheetName = sheetName;
|
||||
}
|
||||
_write(chunk, enc, next) {
|
||||
if (this.structure) {
|
||||
this.rows.push(this.structure.columns.map((col) => chunk[col.columnName]));
|
||||
} else {
|
||||
this.structure = chunk;
|
||||
this.rows.push(chunk.columns.map((x) => x.columnName));
|
||||
}
|
||||
|
||||
next();
|
||||
}
|
||||
|
||||
_final(callback) {
|
||||
const workbook = createWorkbook(this.fileName);
|
||||
xlsx.utils.book_append_sheet(workbook, xlsx.utils.aoa_to_sheet(this.rows), this.sheetName || 'Sheet 1');
|
||||
callback();
|
||||
}
|
||||
}
|
||||
|
||||
async function excelSheetWriter({ fileName, sheetName }) {
|
||||
return new ExcelSheetWriterStream({
|
||||
fileName,
|
||||
sheetName,
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = excelSheetWriter;
|
||||
12
packages/api/src/shell/finalizer.js
Normal file
12
packages/api/src/shell/finalizer.js
Normal file
@@ -0,0 +1,12 @@
|
||||
const finalizers = [];
|
||||
|
||||
module.exports = {
|
||||
async run() {
|
||||
for (const func of finalizers) {
|
||||
await func();
|
||||
}
|
||||
},
|
||||
register(func) {
|
||||
finalizers.push(func);
|
||||
},
|
||||
};
|
||||
@@ -8,12 +8,14 @@ const copyStream = require('./copyStream');
|
||||
const fakeObjectReader = require('./fakeObjectReader');
|
||||
const consoleObjectWriter = require('./consoleObjectWriter');
|
||||
const excelSheetReader = require('./excelSheetReader');
|
||||
const excelSheetWriter = require('./excelSheetWriter');
|
||||
const jsonLinesWriter = require('./jsonLinesWriter');
|
||||
const jsonLinesReader = require('./jsonLinesReader');
|
||||
const jslDataReader = require('./jslDataReader');
|
||||
const archiveWriter = require('./archiveWriter');
|
||||
const archiveReader = require('./archiveReader');
|
||||
const collectorWriter = require('./collectorWriter');
|
||||
const finalizer = require('./finalizer');
|
||||
|
||||
module.exports = {
|
||||
queryReader,
|
||||
@@ -32,4 +34,6 @@ module.exports = {
|
||||
archiveWriter,
|
||||
archiveReader,
|
||||
collectorWriter,
|
||||
excelSheetWriter,
|
||||
finalizer,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user