package Visual_DVM_2021.Passes.All; import Common.Current; import Common.Global; import Common.Utils.Files.VDirectoryChooser; import Common.Utils.Utils; import TestingSystem.DVM.Tasks.TestRunTask; import TestingSystem.DVM.TasksPackage.TasksPackage; import TestingSystem.DVM.TasksPackage.TasksPackageState; import Visual_DVM_2021.Passes.Pass_2021; import org.apache.commons.io.FileUtils; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.*; import java.io.File; import java.io.FileOutputStream; import java.util.Vector; public class ExportTasksPackageToExcel extends Pass_2021 { //https://tproger.ru/translations/how-to-read-write-excel-file-java-poi-example File dir; File res; VDirectoryChooser directoryChooser = new VDirectoryChooser("Выбор папки для сохранения таблицы Excel"); //--- final int max_columns = 13; CellStyle header_style; CellStyle regular_style; Workbook book = null; Sheet sheet = null; //--- @Override public String getIconPath() { return "/icons/Excel.png"; } @Override public String getButtonText() { return ""; } @Override protected boolean canStart(Object... args) throws Exception { book= null; sheet = null; header_style=null; regular_style=null; //-- if (Current.Check(Log, Current.TasksPackage)) { target = Current.getTasksPackage(); if (!target.state.equals(TasksPackageState.Done)) { Log.Writeln_("Можно получить таблицу только завершенного пакета."); return false; } else { dir = directoryChooser.ShowDialog(); if (dir == null) { Log.Writeln_("Папка не выбрана."); return false; } res = new File(dir, Utils.getDateName("package_" + target.id) + ".xls"); return true; } } return false; } protected Row addRow(int row_num, boolean isHeader, Object... values) { Row row = sheet.createRow(row_num); int i = 0; for (Object value : values) { Cell cell = row.createCell(i); cell.setCellValue(value.toString()); cell.setCellStyle(isHeader? header_style:regular_style); //-- ++i; } return row; } @Override protected void body() throws Exception { File file = Utils.getTempFileName("table"); //-- book = new HSSFWorkbook(); sheet = book.createSheet("Результаты тестирования"); header_style = book.createCellStyle(); //--- Font font = book.createFont();//Create font font.setBold(true); header_style.setFont(font); //-- header_style.setBorderBottom(CellStyle.BORDER_THICK); header_style.setBorderLeft(CellStyle.BORDER_THICK); header_style.setBorderRight(CellStyle.BORDER_THICK); header_style.setBorderTop(CellStyle.BORDER_THICK); //-- regular_style=book.createCellStyle(); regular_style.setBorderBottom(CellStyle.BORDER_THIN); regular_style.setBorderLeft(CellStyle.BORDER_THIN); regular_style.setBorderRight(CellStyle.BORDER_THIN); regular_style.setBorderTop(CellStyle.BORDER_THIN); //-- Row header = addRow(0, true, "Группа", "Тест", "Язык", "Флаги", "Сборка", "Запуск", "Время компиляции (с)", "Матрица", "Окружение", "usr.par", "Время выполнения (с)", "Чистое время (с)", "Прогресс (%)"); //-- Vector tasks = new Vector<>(); for (TestRunTask task : Global.testingServer.account_db.testRunTasks.Data.values()) { if ((task.taskspackage_id == target.id) && (task.isVisible())) tasks.add(task); } //-- int i = 1; for (TestRunTask task : tasks) { Row row = addRow(i, false, task.group_description, task.test_description, task.language.getDescription(), task.flags, task.compilation_state.getDescription(), task.state.getDescription(), task.compilation_time, task.matrix, task.environments, task.usr_par, task.Time, task.CleanTime, task.progress ); ++i; } //-- for (i = 0; i < max_columns; ++i) sheet.autoSizeColumn(i); // Записываем всё в файл FileOutputStream stream = new FileOutputStream(file); book.write(stream); book.close(); stream.close(); ///-- FileUtils.copyFile(file, res); book = null; stream = null; file = null; } }