Added a command to get suggestion data into excel

This commit is contained in:
2023-03-12 05:32:07 +01:00
parent 11d2e2bac8
commit df6d060863
4 changed files with 183 additions and 2 deletions
@@ -0,0 +1,57 @@
package com.alttd.util;
import net.dv8tion.jda.api.interactions.InteractionHook;
import net.dv8tion.jda.api.utils.FileUpload;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class ExcelWriter {
private final String fileName = "suggestions.xlsx";
private final String sheetName = "Sheet1";
private final Workbook workbook;
private final Sheet sheet;
private int currentRow = 0;
private boolean done = false;
public ExcelWriter() {
this.workbook = new XSSFWorkbook();
// Create a new sheet in the workbook
this.sheet = workbook.createSheet(sheetName);
}
public synchronized void addRow(String... data) {
if (done) {
Logger.warning("Tried to write to finished excel file");
return;
}
Row row = sheet.createRow(currentRow++);
int curCel = 0;
for (String entry : data) {
Cell cell = row.createCell(curCel++);
cell.setCellValue(entry);
}
}
public synchronized void saveAndSend(InteractionHook reply) {
done = true;
try {
File file = new File(fileName);
FileOutputStream outputStream = new FileOutputStream(file);
workbook.write(outputStream);
workbook.close();
reply.sendFiles(FileUpload.fromData(file)).queue(done -> {
//noinspection ResultOfMethodCallIgnored
file.delete();
});
} catch (IOException e) {
reply.sendMessageEmbeds(Util.genericErrorEmbed("Error", "Error while uploading excel file")).queue();
}
}
}