#1
After building pagination, filtering, and sorting, it’s time to let users export data and generate audit reports. This feature helps admins analyze or back up key records efficiently.

1. Add CSV Export Feature

Let’s start with CSV export. Add a new endpoint to your controller:
@GetMapping("/export/csv")
public void exportToCSV(HttpServletResponse response) throws IOException {
    response.setContentType("text/csv");
    response.setHeader("Content-Disposition", "attachment; filename=users.csv");

    List<User> users = userRepo.findAll();
    PrintWriter writer = response.getWriter();
    writer.println("ID,Username,Role");

    for (User user : users) {
        writer.println(user.getId() + "," + user.getUsername() + "," + user.getRole());
    }
    writer.flush();
    writer.close();

    dashboardService.logAction("Exported user data to CSV", "admin");
}
Now when you open /dashboard/export/csv, a CSV file will download automatically with all user data.

2. Add Excel Export (using Apache POI)

Add this dependency in pom.xml:
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>5.2.5</version>
</dependency>
Then create a simple Excel export method:
@GetMapping("/export/excel")
public void exportToExcel(HttpServletResponse response) throws IOException {
    response.setContentType("application/octet-stream");
    response.setHeader("Content-Disposition", "attachment; filename=users.xlsx");

    List<User> users = userRepo.findAll();
    Workbook workbook = new XSSFWorkbook();
    Sheet sheet = workbook.createSheet("Users");

    Row header = sheet.createRow(0);
    header.createCell(0).setCellValue("ID");
    header.createCell(1).setCellValue("Username");
    header.createCell(2).setCellValue("Role");

    int rowIdx = 1;
    for (User u : users) {
        Row row = sheet.createRow(rowIdx++);
        row.createCell(0).setCellValue(u.getId());
        row.createCell(1).setCellValue(u.getUsername());
        row.createCell(2).setCellValue(u.getRole());
    }

    workbook.write(response.getOutputStream());
    workbook.close();

    dashboardService.logAction("Exported user data to Excel", "admin");
}

3. Add Audit Log Export

To help with compliance or monitoring, you can also export the audit logs:
@GetMapping("/audit/export/csv")
public void exportAuditLogs(HttpServletResponse response) throws IOException {
    response.setContentType("text/csv");
    response.setHeader("Content-Disposition", "attachment; filename=audit_logs.csv");

    List<AuditLog> logs = logRepo.findAll(Sort.by(Sort.Direction.DESC, "timestamp"));
    PrintWriter writer = response.getWriter();
    writer.println("Action,Performed By,Timestamp");

    for (AuditLog log : logs) {
        writer.println(log.getAction() + "," + log.getPerformedBy() + "," + log.getTimestamp());
    }

    writer.flush();
    writer.close();
}
This provides a downloadable file containing all recent user actions — perfect for auditing or reviewing system behavior.

4. Add Export Buttons in the Dashboard

Update your dashboard.html to include download buttons:
<div>
  <a th:href="@{/dashboard/export/csv}">Export Users to CSV</a> |
  <a th:href="@{/dashboard/export/excel}">Export Users to Excel</a> |
  <a th:href="@{/dashboard/audit/export/csv}">Export Audit Logs</a>
</div>
Now the dashboard can export user data or audit records anytime.

5. Verify Audit Logs

Check your Firebird AUDIT_LOG table after exporting — each export action should be logged automatically via logAction().
Example entry:
Action: Exported user data to Excel  
Performed By: admin  
Timestamp: 2025-12-22 10:32:11  
#ads

image quote pre code