以下脚本从 mySql 进行查询并将结果存储在本地 Excel 中。我试图解决的问题是将其保存到 S3 存储桶而不是我的计算机上。我有一些使用 AWS 的经验,可以桥接与 AWS 的连接以从 S3 存储桶读取文件,但我很难解决这个问题。谢谢!
我找到了以下代码,但无法将其与我已经编写的代码集成。
const s3 = new aws.S3(/* put your s3 configuration here */);
const stream = new Stream.PassThrough();
const workbook = new exceljs.Workbook();
// Add images of cats to workbook
workbook.xlsx.write(stream)
.then(() => {
return s3.upload({
Key: PATH_IN_S3,
Bucket: BUCKET_NAME,
Body: stream,
ContentType: CONTENT_TYPE_EXCEL
}).promise();
})
.then(/* do whatever */)
.catch(/* handle error */);
Here is what is currently working locally:
const reconQuery = 'SELECT T1.Database_ID, Lease_Description, SUM(BR_Current_Month_Cash - BR_Current_Month_Cash_Client) total, SUM(CAM_Current_Month_Cash - CAM_Current_Month_Cash_Client) total2, SUM(TAX_Current_Month_Cash - TAX_Current_Month_Cash_Client) total3, SUM(Insurance_Current_Month_Cash - Insurance_Current_Month_Cash_Client) total4, SUM(Sales_Tax_Current_Month_Cash - Sales_Tax_Current_Month_Cash_Client) total5 FROM `lq 2` AS T1 INNER JOIN `cd 2` AS T2 ON T1.Database_ID = T2.Database_ID GROUP BY Database_ID'
connection.query(reconQuery,
function (err, reconQuery, field) {
const jsonReconData = JSON.parse(JSON.stringify(reconQuery));
let workbook = new excel.Workbook(); //creating workbook
let worksheet = workbook.addWorksheet('Month 2'); //creating worksheet
// WorkSheet Header
// WorkSheet Header
worksheet.columns = [
{ header: 'Database ID', key: 'Database_ID', width: 10 },
{ header: 'Lease Description', key: 'Lease_Description', width: 30 },
{ header: 'Base Rent Difference', key: 'total', width: 30 },
{ header: 'CAM Difference', key: 'total2', width: 30 },
{ header: 'Tax Difference', key: 'total3', width: 30 },
{ header: 'Insurance Difference', key: 'total4', width: 30 },
{ header: 'Sales Tax Difference', key: 'total5', width: 30 },
];
// Add Array Rows
worksheet.addRows(jsonReconData);
// Write to File
workbook.xlsx.writeFile("month2.xlsx")
.then(function () {
console.log("file saved!");
});
})
Run Code Online (Sandbox Code Playgroud)
我发现如果有人感兴趣的话:
const stream = new Stream.PassThrough();
const workbook = new excel.Workbook();
let worksheet = workbook.addWorksheet('Month 1'); //creating worksheet
// WorkSheet Header
worksheet.columns = [
{ header: 'Database ID', key: 'Database_ID', width: 10 },
{ header: 'Lease Description', key: 'Lease_Description', width: 30 },
{ header: 'Base Rent Difference', key: 'total', width: 30},
{ header: 'CAM Difference', key: 'total2', width: 30},
{ header: 'Tax Difference', key: 'total3', width: 30},
{ header: 'Insurance Difference', key: 'total4', width: 30},
{ header: 'Sales Tax Difference', key: 'total5', width: 30},
];
// Add Array Rows
worksheet.addRows(jsonReconData);
workbook.xlsx.write(stream)
.then(() => {
return s3.upload({
Key: 'Month1.xlsx',
Bucket: 'bucketname',
Body: stream,
ContentType: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
}).promise();
})
.catch(function(e) {
console.log(e.message)
}).then(function(){
console.log('after a catch the chain is restored');
}, function () {
console.log('Not fired due to the catch');
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3852 次 |
| 最近记录: |