[update] Add Excel export functionality to DataDialog. Implement file selection and data export features, enhancing patient data management capabilities. Update pom.xml to include Apache POI dependencies for Excel handling.

This commit is contained in:
Lê Thành Đạt committed 2025-07-30 19:41:17 +07:00
1 parent bbf8c9fd10
commit 693ad5479c
5 files changed
+138 -3

No files matched your search

+10 -1
View File
@@ -306,6 +306,15 @@
<artifactId>javase</artifactId>
<version>3.5.3</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>5.2.5</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>5.2.5</version>
</dependency>
</dependencies>
</project>
@@ -21,6 +21,13 @@ import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Calendar;
import java.util.Date;
import BsK.client.ui.component.DataDialog.ExcelExporter;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;
public class DataDialog extends JDialog {
@@ -38,6 +45,8 @@ public class DataDialog extends JDialog {
private int totalPages = 1;
private int recordsPerPage = 20;
private int totalRecords = 0;
private boolean isExporting = false;
private File fileForExport = null;
private JPanel mainPanel;
private JPanel paginationPanel;
@@ -201,7 +210,7 @@ public class DataDialog extends JDialog {
AddDialog addDialog = new AddDialog((Frame) getParent());
addDialog.setVisible(true);
});
exportExcelButton.addActionListener(e -> JOptionPane.showMessageDialog(this, "Chức năng xuất Excel đang được phát triển", "Thông báo", JOptionPane.INFORMATION_MESSAGE));
exportExcelButton.addActionListener(e -> handleExportToExcel());
filterButton.addActionListener(e -> {
saveFiltersToLocalStorage(); // <-- SAVE STATE BEFORE FETCHING
@@ -300,6 +309,31 @@ public class DataDialog extends JDialog {
// --- MODIFIED: Process raw data into Patient DTOs and populate table correctly ---
private void handleGetCheckupDataResponse(GetCheckupDataResponse response) {
// --- START OF MODIFICATION ---
// Check if we are in export mode
if (isExporting) {
List<Patient> patientsToExport = new ArrayList<>();
if (response.getCheckupData() != null) {
for (String[] rowData : response.getCheckupData()) {
patientsToExport.add(new Patient(rowData));
}
}
try {
ExcelExporter.exportToExcel(patientsToExport, this.fileForExport);
JOptionPane.showMessageDialog(this, "Xuất file Excel thành công!\n" + this.fileForExport.getAbsolutePath(), "Thành công", JOptionPane.INFORMATION_MESSAGE);
} catch (IOException ex) {
JOptionPane.showMessageDialog(this, "Lỗi khi ghi file Excel: " + ex.getMessage(), "Lỗi", JOptionPane.ERROR_MESSAGE);
} finally {
// Reset the flag and file variable after exporting
isExporting = false;
fileForExport = null;
}
return; // IMPORTANT: Stop further execution to avoid updating the UI table
}
// --- END OF MODIFICATION ---
// This is your existing code for updating the table for pagination
SwingUtilities.invokeLater(() -> {
this.currentPage = response.getCurrentPage();
this.totalPages = response.getTotalPages();
@@ -346,6 +380,29 @@ public class DataDialog extends JDialog {
resultCountLabel.setText(String.format("Hiển thị %d đến %d của %d kết quả", startRecord, endRecord, totalRecords));
}
private void handleExportToExcel() {
JFileChooser fileChooser = new JFileChooser();
fileChooser.setDialogTitle("Lưu file Excel");
fileChooser.setFileFilter(new FileNameExtensionFilter("Excel Workbook (*.xlsx)", "xlsx"));
fileChooser.setSelectedFile(new File("DanhSachKhamBenh.xlsx"));
if (fileChooser.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) {
this.fileForExport = fileChooser.getSelectedFile();
// Ensure the file has a .xlsx extension
if (!this.fileForExport.getName().toLowerCase().endsWith(".xlsx")) {
this.fileForExport = new File(this.fileForExport.getParentFile(), this.fileForExport.getName() + ".xlsx");
}
// Set the flag to true
this.isExporting = true;
JOptionPane.showMessageDialog(this, "Đang chuẩn bị dữ liệu để xuất file...", "Thông báo", JOptionPane.INFORMATION_MESSAGE);
// Call fetchData with a special page number (-1) to signal "get all"
fetchData(-1);
}
}
private void updatePaginationControls() {
paginationPanel.removeAll();
@@ -0,0 +1,69 @@
package BsK.client.ui.component.DataDialog;
import BsK.common.entity.Patient;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
public class ExcelExporter {
public static void exportToExcel(List<Patient> patientList, File file) throws IOException {
// Create a new Excel workbook
try (XSSFWorkbook workbook = new XSSFWorkbook()) {
XSSFSheet sheet = workbook.createSheet("DanhSachKhamBenh");
// Define header titles
String[] headers = {
"Mã Khám", "Mã BN", "Họ", "Tên", "Năm Sinh", "Giới Tính", "Số Điện Thoại",
"Địa Chỉ", "Ngày Khám", "Loại Khám", "Bác Sĩ Khám", "CCCD/DDCN", "Cân Nặng (kg)",
"Chiều Cao (cm)", "Nhịp Tim (l/p)", "Huyết Áp (mmHg)", "Chẩn Đoán", "Kết Luận",
"Đề Nghị", "Ghi Chú", "Ngày Tái Khám"
};
// Create the header row
Row headerRow = sheet.createRow(0);
for (int i = 0; i < headers.length; i++) {
Cell cell = headerRow.createCell(i);
cell.setCellValue(headers[i]);
}
// Fill data rows
int rowNum = 1;
for (Patient patient : patientList) {
Row row = sheet.createRow(rowNum++);
row.createCell(0).setCellValue(patient.getCheckupId());
row.createCell(1).setCellValue(patient.getCustomerId());
row.createCell(2).setCellValue(patient.getCustomerLastName());
row.createCell(3).setCellValue(patient.getCustomerFirstName());
row.createCell(4).setCellValue(patient.getCustomerDob());
row.createCell(5).setCellValue(patient.getCustomerGender());
row.createCell(6).setCellValue(patient.getCustomerNumber());
row.createCell(7).setCellValue(patient.getCustomerAddress());
row.createCell(8).setCellValue(patient.getCheckupDate());
row.createCell(9).setCellValue(patient.getCheckupType());
row.createCell(10).setCellValue(patient.getDoctorName());
row.createCell(11).setCellValue(patient.getCccdDdcn());
row.createCell(12).setCellValue(patient.getCustomerWeight());
row.createCell(13).setCellValue(patient.getCustomerHeight());
row.createCell(14).setCellValue(patient.getHeartBeat());
row.createCell(15).setCellValue(patient.getBloodPressure());
row.createCell(16).setCellValue(patient.getDiagnosis());
row.createCell(17).setCellValue(patient.getConclusion());
row.createCell(18).setCellValue(patient.getSuggestion());
row.createCell(19).setCellValue(patient.getNotes());
row.createCell(20).setCellValue(patient.getReCheckupDate());
}
// Write the output to a file
try (FileOutputStream fileOut = new FileOutputStream(file)) {
workbook.write(fileOut);
}
}
}
}
@@ -4,7 +4,7 @@ import BsK.common.packet.Packet;
import BsK.common.packet.PacketSerializer;
import BsK.common.packet.req.*;
import BsK.common.packet.res.*;
import BsK.common.packet.res.ErrorResponse.Error;
import BsK.common.Error;
import BsK.common.entity.Status;
import BsK.common.util.date.DateUtils;
import BsK.server.Server;
Binary file not shown.