One embarrassing solution would be to update current URL and then use native SharePoint Export to Excel button.
Open your items list and filter with one ID, you may notice that URL looks like: yourPage.aspx#InplviewHashYouCurrentView=FilterFields1%3DID-FilterValues1%3DXXX%253B%2523YYY. You would then update it within window.location.href by appending desired IDs.
A better and viable solution is a more robust out-of-the-box JS development. Let us suppose that we have our list items context (through ctx.ListData.Row objects array) and a function called getIDs that retrieves our desired IDs. We will use tableexport JS library to export data to Excel; which needs an HTML table element for export. We need to build our HTML table with our items filtered by our getIDs.
var IDs = getIDs(); // Array of int
var items = ctx.ListData.Row; // Array of objects
for(var i=0;i<items.length;i++) {
tableStr = '<table id="placeholder_table_export">';
if(IDs.indexOf(items[i]['ID']) != -1) { // Item needs to be retrieved
tableStr += '<tr><td>'+ items[i]['ID'] +'</td><td>'+ items[i]['Title'] +'</td></tr>';
}
tableStr += '</table>';
}
Next, once the above table is appended to your HTML, you would use TableExport to export that created table.
var excelExporter = new TableExport(placeholderTableExport.getElementById('#placeholder_table_export'), {
formats: ['xlsx'],
filename: filename,
trimWhitespace: false,
exportButtons: false
});
var exportedData = excelExporter.getExportData();
exportedData = exportedData[Object.keys(exportedData)[0]]['xlsx'];
excelExporter.export2file(exportedData.data, exportedData.mimeType,
exportedData.filename, exportedData.fileExtension);
(TableExport is easier to use, but since we need to init Excel export immediately, we have to use this workaround.)
I hope you get the idea.